-
Notifications
You must be signed in to change notification settings - Fork 228
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
7 changed files
with
136 additions
and
20 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
38 changes: 38 additions & 0 deletions
38
airbyte-commons-worker/src/main/java/io/airbyte/workers/context/AttemptContext.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,38 @@ | ||
/* | ||
* Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.workers.context; | ||
|
||
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.ATTEMPT_NUMBER_KEY; | ||
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.CONNECTION_ID_KEY; | ||
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.JOB_ID_KEY; | ||
|
||
import io.airbyte.metrics.lib.ApmTraceUtils; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Context of an Attempt. | ||
*/ | ||
public record AttemptContext(UUID connectionId, Long jobId, Integer attemptNumber) { | ||
|
||
/** | ||
* Update the current trace with the ids from the context. | ||
*/ | ||
public void addTagsToTrace() { | ||
final Map<String, Object> tags = new HashMap<>(); | ||
if (connectionId != null) { | ||
tags.put(CONNECTION_ID_KEY, connectionId); | ||
} | ||
if (jobId != null) { | ||
tags.put(JOB_ID_KEY, jobId); | ||
} | ||
if (attemptNumber != null) { | ||
tags.put(ATTEMPT_NUMBER_KEY, attemptNumber); | ||
} | ||
ApmTraceUtils.addTagsToTrace(tags); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
airbyte-commons-worker/src/test/java/io/airbyte/workers/context/AttemptContextTest.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,71 @@ | ||
/* | ||
* Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
*/ | ||
|
||
package io.airbyte.workers.context; | ||
|
||
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.ATTEMPT_NUMBER_KEY; | ||
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.CONNECTION_ID_KEY; | ||
import static io.airbyte.metrics.lib.ApmTraceConstants.Tags.JOB_ID_KEY; | ||
import static io.airbyte.metrics.lib.ApmTraceUtils.formatTag; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.reset; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
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 AttemptContextTest { | ||
|
||
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 addTagsToTrace() { | ||
final UUID connectionId = UUID.randomUUID(); | ||
final Long jobId = 5L; | ||
final Integer attemptNumber = 3; | ||
new AttemptContext(connectionId, jobId, attemptNumber).addTagsToTrace(); | ||
|
||
verifySpanSetTag(CONNECTION_ID_KEY, connectionId); | ||
verifySpanSetTag(JOB_ID_KEY, jobId); | ||
verifySpanSetTag(ATTEMPT_NUMBER_KEY, attemptNumber); | ||
} | ||
|
||
@Test | ||
void addTagsToTraceShouldIgnoreNullValues() { | ||
final UUID connectionId = UUID.randomUUID(); | ||
final Long jobId = 3L; | ||
final Integer attemptNumber = 7; | ||
|
||
new AttemptContext(connectionId, null, null).addTagsToTrace(); | ||
verifySpanSetTag(CONNECTION_ID_KEY, connectionId); | ||
reset(span); | ||
|
||
new AttemptContext(null, jobId, null).addTagsToTrace(); | ||
verifySpanSetTag(JOB_ID_KEY, jobId); | ||
reset(span); | ||
|
||
new AttemptContext(null, null, attemptNumber).addTagsToTrace(); | ||
verifySpanSetTag(ATTEMPT_NUMBER_KEY, attemptNumber); | ||
reset(span); | ||
} | ||
|
||
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
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
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
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