-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
airbyte-metrics/metrics-lib/src/main/java/io/airbyte/metrics/lib/TracingHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.metrics.lib; | ||
|
||
import io.airbyte.metrics.lib.ApmTraceConstants.Tags; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Helps Annotating Traces. | ||
*/ | ||
public class TracingHelper { | ||
|
||
/** | ||
* Add ConnectionId ta the current Trace. | ||
*/ | ||
public static void addConnection(final UUID connectionId) { | ||
if (connectionId != null) { | ||
ApmTraceUtils.addTagsToTrace(Map.of(Tags.CONNECTION_ID_KEY, connectionId)); | ||
} | ||
} | ||
|
||
/** | ||
* Add WorkspaceId ta the current Trace. | ||
*/ | ||
public static void addWorkspace(final UUID workspaceId) { | ||
if (workspaceId != null) { | ||
ApmTraceUtils.addTagsToTrace(Map.of(Tags.WORKSPACE_ID_KEY, workspaceId)); | ||
} | ||
} | ||
|
||
/** | ||
* Add SourceId and DestinationId ta the current Trace. | ||
*/ | ||
public static void addSourceDestination(final UUID sourceId, final UUID destinationId) { | ||
final Map<String, Object> tags = new HashMap<>(); | ||
if (sourceId != null) { | ||
tags.put(Tags.SOURCE_ID_KEY, sourceId); | ||
} | ||
if (destinationId != null) { | ||
tags.put(Tags.DESTINATION_ID_KEY, destinationId); | ||
} | ||
ApmTraceUtils.addTagsToTrace(tags); | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
airbyte-metrics/metrics-lib/src/test/java/io/airbyte/metrics/lib/TracingHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.metrics.lib; | ||
|
||
import static io.airbyte.metrics.lib.ApmTraceUtils.formatTag; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import io.airbyte.metrics.lib.ApmTraceConstants.Tags; | ||
import io.opentracing.Span; | ||
import io.opentracing.Tracer; | ||
import io.opentracing.util.GlobalTracerTestUtil; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TracingHelperTest { | ||
|
||
Span span; | ||
Tracer tracer; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
span = mock(Span.class); | ||
tracer = mock(Tracer.class); | ||
when(tracer.activeSpan()).thenReturn(span); | ||
GlobalTracerTestUtil.setGlobalTracerUnconditionally(tracer); | ||
} | ||
|
||
@Test | ||
void testAddConnection() { | ||
TracingHelper.addConnection(null); | ||
verifySpanNotSet(Tags.CONNECTION_ID_KEY); | ||
|
||
final UUID connectionId = UUID.randomUUID(); | ||
TracingHelper.addConnection(connectionId); | ||
verifySpanSetTag(Tags.CONNECTION_ID_KEY, connectionId); | ||
} | ||
|
||
@Test | ||
void testAddSourceDestination() { | ||
final UUID sourceId = UUID.randomUUID(); | ||
final UUID destinationId = UUID.randomUUID(); | ||
|
||
TracingHelper.addSourceDestination(null, null); | ||
verifySpanNotSet(Tags.SOURCE_ID_KEY); | ||
verifySpanNotSet(Tags.DESTINATION_ID_KEY); | ||
|
||
TracingHelper.addSourceDestination(sourceId, null); | ||
verifySpanSetTag(Tags.SOURCE_ID_KEY, sourceId); | ||
verifySpanNotSet(Tags.DESTINATION_ID_KEY); | ||
reset(span); | ||
|
||
TracingHelper.addSourceDestination(null, destinationId); | ||
verifySpanNotSet(Tags.SOURCE_ID_KEY); | ||
verifySpanSetTag(Tags.DESTINATION_ID_KEY, destinationId); | ||
reset(span); | ||
|
||
TracingHelper.addSourceDestination(sourceId, destinationId); | ||
verifySpanSetTag(Tags.SOURCE_ID_KEY, sourceId); | ||
verifySpanSetTag(Tags.DESTINATION_ID_KEY, destinationId); | ||
} | ||
|
||
@Test | ||
void testAddWorkspace() { | ||
TracingHelper.addWorkspace(null); | ||
verifySpanNotSet(Tags.WORKSPACE_ID_KEY); | ||
|
||
final UUID workspaceId = UUID.randomUUID(); | ||
TracingHelper.addWorkspace(workspaceId); | ||
verifySpanSetTag(Tags.WORKSPACE_ID_KEY, workspaceId); | ||
} | ||
|
||
private void verifySpanNotSet(final String tag) { | ||
verify(span, never()).setTag(formatTag(tag), eq(anyString())); | ||
} | ||
|
||
private void verifySpanSetTag(final String tag, final Object value) { | ||
verify(span).setTag(formatTag(tag), value.toString()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters