Skip to content

Commit 4a5461c

Browse files
authored
Document when we manage a span's lifecycle without scopes (#8628)
There are a few instrumentations where the instrumented code does not align with scopes. In such situations the span is activated, but the scope is thrown away. The span is then closed in another section of code using 'closeActive()'. This PR adds a 'activateSpanWithoutScope(span)' method to self-document this behaviour and explicitly tie it to the use of 'closeActive()'.
1 parent df4020c commit 4a5461c

File tree

8 files changed

+36
-15
lines changed

8 files changed

+36
-15
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/domain/TestImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package datadog.trace.civisibility.domain;
22

33
import static datadog.json.JsonMapper.toJson;
4-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpanWithoutScope;
55
import static datadog.trace.civisibility.Constants.CI_VISIBILITY_INSTRUMENTATION_NAME;
66

77
import datadog.trace.api.Config;
@@ -116,7 +116,7 @@ public TestImpl(
116116

117117
span = spanBuilder.start();
118118

119-
activateSpan(span);
119+
activateSpanWithoutScope(span);
120120

121121
span.setSpanType(InternalSpanTypes.TEST);
122122
span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_TEST);

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/domain/TestSuiteImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package datadog.trace.civisibility.domain;
22

33
import static datadog.json.JsonMapper.toJson;
4-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpanWithoutScope;
55
import static datadog.trace.civisibility.Constants.CI_VISIBILITY_INSTRUMENTATION_NAME;
66

77
import datadog.trace.api.Config;
@@ -130,7 +130,7 @@ public TestSuiteImpl(
130130
testDecorator.afterStart(span);
131131

132132
if (!parallelized) {
133-
activateSpan(span);
133+
activateSpanWithoutScope(span);
134134
}
135135

136136
metricCollector.add(CiVisibilityCountMetric.EVENT_CREATED, 1, instrumentation, EventType.SUITE);

dd-java-agent/instrumentation/aws-java-sdk-1.11.0/src/main/java/datadog/trace/instrumentation/aws/v0/TracingRequestHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static datadog.trace.api.datastreams.DataStreamsContext.create;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.XRAY_TRACING_CONCERN;
5-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
5+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpanWithoutScope;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.blackholeSpan;
77
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
88
import static datadog.trace.core.datastreams.TagsProcessor.DIRECTION_IN;
@@ -55,7 +55,7 @@ public void beforeRequest(final Request<?> request) {
5555
if (!AWS_LEGACY_TRACING && isPollingRequest(request.getOriginalRequest())) {
5656
// SQS messages spans are created by aws-java-sqs-1.0 - replace client scope with no-op,
5757
// so we can tell when receive call is complete without affecting the rest of the trace
58-
activateSpan(blackholeSpan());
58+
activateSpanWithoutScope(blackholeSpan());
5959
} else {
6060
span = requestSpanStore.remove(request.getOriginalRequest());
6161
if (span != null) {
@@ -78,9 +78,9 @@ public void beforeRequest(final Request<?> request) {
7878
}
7979
// This scope will be closed by AwsHttpClientInstrumentation
8080
if (AWS_LEGACY_TRACING) {
81-
activateSpan(span);
81+
activateSpanWithoutScope(span);
8282
} else {
83-
activateSpan(blackholeSpan());
83+
activateSpanWithoutScope(blackholeSpan());
8484
}
8585
}
8686
}

dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java/datadog/trace/instrumentation/aws/v2/TracingExecutionInterceptor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static datadog.trace.bootstrap.instrumentation.api.AgentPropagation.XRAY_TRACING_CONCERN;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
5+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpanWithoutScope;
56
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.blackholeSpan;
67
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
78
import static datadog.trace.instrumentation.aws.v2.AwsSdkClientDecorator.AWS_LEGACY_TRACING;
@@ -93,7 +94,7 @@ public void beforeTransmission(
9394
if (span != null) {
9495
// This scope will be closed by AwsHttpClientInstrumentation since ExecutionInterceptor API
9596
// doesn't provide a way to run code in same thread after transmission has been scheduled.
96-
activateSpan(span);
97+
activateSpanWithoutScope(span);
9798
}
9899
}
99100

dd-java-agent/instrumentation/karate/src/main/java/datadog/trace/instrumentation/karate/KarateTracingHook.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import datadog.trace.api.civisibility.telemetry.tag.SkipReason;
2323
import datadog.trace.api.civisibility.telemetry.tag.TestFrameworkInstrumentation;
2424
import datadog.trace.bootstrap.ContextStore;
25-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
2625
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
2726
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
2827
import datadog.trace.bootstrap.instrumentation.api.Tags;
@@ -198,7 +197,7 @@ public boolean beforeStep(Step step, ScenarioRuntime sr) {
198197
return true;
199198
}
200199
AgentSpan span = AgentTracer.startSpan("karate", KARATE_STEP_SPAN_NAME);
201-
AgentScope scope = AgentTracer.activateSpan(span);
200+
AgentTracer.activateSpanWithoutScope(span);
202201
String stepName = step.getPrefix() + " " + step.getText();
203202
span.setResourceName(stepName);
204203
span.setTag(Tags.COMPONENT, "karate");

dd-java-agent/instrumentation/undertow/undertow-2.0/src/main/java/datadog/trace/instrumentation/undertow/HttpRequestParserInstrumentation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
55
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
7-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.closeActive;
87
import static datadog.trace.instrumentation.undertow.UndertowDecorator.DECORATE;
98
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
109
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -92,7 +91,7 @@ public static void afterRequestParse(
9291
if (scope != null) {
9392
scope.close();
9493
} else {
95-
closeActive();
94+
// span was already active, scope will be closed by HandlerInstrumentation
9695
}
9796
}
9897
}

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,12 @@ public AgentScope activateManualSpan(final AgentSpan span) {
916916
return scopeManager.activateManualSpan(span);
917917
}
918918

919+
@Override
920+
@SuppressWarnings("resource")
921+
public void activateSpanWithoutScope(AgentSpan span) {
922+
scopeManager.activateSpan(span);
923+
}
924+
919925
@Override
920926
public AgentScope.Continuation captureActiveSpan() {
921927
return scopeManager.captureActiveSpan();

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ public static AgentScope activateSpan(final AgentSpan span) {
8787
return get().activateSpan(span);
8888
}
8989

90+
/**
91+
* Activate a span which will be closed by {@link #closeActive()} instead of a scope.
92+
*
93+
* @deprecated This should only be used when the instrumented code doesn't align with a scope.
94+
*/
95+
@Deprecated
96+
public static void activateSpanWithoutScope(final AgentSpan span) {
97+
get().activateSpanWithoutScope(span);
98+
}
99+
90100
/**
91101
* When asynchronous propagation is enabled, prevent the currently active trace from reporting
92102
* until the returned Continuation is either activated (and the returned scope is closed) or the
@@ -142,8 +152,8 @@ public static void rollbackActiveToCheckpoint() {
142152
/**
143153
* Closes the scope for the currently active span.
144154
*
145-
* @deprecated This should only be used when an instrumentation does not have access to the
146-
* original scope returned by {@link #activateSpan}.
155+
* @deprecated This should only be used when the span was previously activated with {@link
156+
* #activateSpanWithoutScope} because the instrumented code didn't align with a scope.
147157
*/
148158
@Deprecated
149159
public static void closeActive() {
@@ -338,6 +348,9 @@ AgentSpan startSpan(
338348
/** Activate a span from outside auto-instrumentation, i.e. a manual or custom span. */
339349
AgentScope activateManualSpan(AgentSpan span);
340350

351+
/** Activate a span which will be closed by {@link #closeActive()} instead of a scope. */
352+
void activateSpanWithoutScope(AgentSpan span);
353+
341354
@Override
342355
AgentScope.Continuation captureActiveSpan();
343356

@@ -483,6 +496,9 @@ public AgentScope activateManualSpan(final AgentSpan span) {
483496
return NoopScope.INSTANCE;
484497
}
485498

499+
@Override
500+
public void activateSpanWithoutScope(final AgentSpan span) {}
501+
486502
@Override
487503
public AgentScope.Continuation captureActiveSpan() {
488504
return NoopContinuation.INSTANCE;

0 commit comments

Comments
 (0)