Skip to content

Commit cb0dcc4

Browse files
authored
Return noop continuation when not async propagating (#8365)
1 parent 652b465 commit cb0dcc4

File tree

8 files changed

+52
-32
lines changed

8 files changed

+52
-32
lines changed

dd-java-agent/instrumentation/opentracing/api-0.31/src/test/groovy/OpenTracing31Test.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import io.opentracing.util.GlobalTracer
2828
import spock.lang.Subject
2929

3030
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
31+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation
3132
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled
3233

3334
class OpenTracing31Test extends AgentTestRunner {
@@ -164,7 +165,7 @@ class OpenTracing31Test extends AgentTestRunner {
164165
span instanceof MutableSpan
165166
scope instanceof TraceScope
166167
!internalTracer.isAsyncPropagationEnabled()
167-
(scope as TraceScope).capture() == null
168+
(scope as TraceScope).capture() == noopContinuation()
168169
(tracer.scopeManager().active().span().delegate == span.delegate)
169170

170171
when:

dd-java-agent/instrumentation/opentracing/api-0.32/src/test/groovy/OpenTracing32Test.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP
3333
import static datadog.trace.api.sampling.SamplingMechanism.AGENT_RATE
3434
import static datadog.trace.api.sampling.SamplingMechanism.DEFAULT
3535
import static datadog.trace.api.sampling.SamplingMechanism.MANUAL
36+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation
3637
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled
3738

3839
class OpenTracing32Test extends AgentTestRunner {
@@ -174,7 +175,7 @@ class OpenTracing32Test extends AgentTestRunner {
174175
span instanceof MutableSpan
175176
scope instanceof TraceScope
176177
!internalTracer.isAsyncPropagationEnabled()
177-
(scope as TraceScope).capture() == null
178+
(scope as TraceScope).capture() == noopContinuation()
178179
(tracer.scopeManager().active().span().delegate == span.delegate)
179180

180181
when:

dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScope.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datadog.trace.api.scopemanager.ScopeListener;
66
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
77
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
8+
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
89
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
910
import datadog.trace.bootstrap.instrumentation.api.ScopeSource;
1011
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
@@ -132,13 +133,14 @@ public final void setAsyncPropagation(final boolean value) {
132133
/**
133134
* The continuation returned must be closed or activated or the trace will not finish.
134135
*
135-
* @return The new continuation, or null if this scope is not async propagating.
136+
* @return The new continuation, or {@link AgentTracer#noopContinuation()} if this scope is not
137+
* async propagating.
136138
*/
137139
@Override
138-
public final ScopeContinuation capture() {
140+
public final Continuation capture() {
139141
return isAsyncPropagating
140142
? new ScopeContinuation(scopeManager, span, source()).register()
141-
: null;
143+
: AgentTracer.noopContinuation();
142144
}
143145

144146
@Override

dd-trace-core/src/test/groovy/datadog/trace/core/scopemanager/ScopeManagerTest.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit
2626
import java.util.concurrent.atomic.AtomicInteger
2727
import java.util.concurrent.atomic.AtomicReference
2828

29+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation
2930
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan
3031
import static datadog.trace.core.scopemanager.EVENT.ACTIVATE
3132
import static datadog.trace.core.scopemanager.EVENT.CLOSE
@@ -249,22 +250,22 @@ class ScopeManagerTest extends DDCoreSpecification {
249250
writer == []
250251
}
251252

252-
def "DDScope only creates continuations when propagation is set"() {
253+
def "DDScope creates no-op continuations when propagation is not set"() {
253254
when:
254255
def span = tracer.buildSpan("test", "test").start()
255256
def scope = tracer.activateSpan(span)
256257
tracer.setAsyncPropagationEnabled(false)
257258
def continuation = scope.capture()
258259

259260
then:
260-
continuation == null
261+
continuation == noopContinuation()
261262

262263
when:
263264
tracer.setAsyncPropagationEnabled(true)
264265
continuation = scope.capture()
265266

266267
then:
267-
continuation != null
268+
continuation != noopContinuation() && continuation != null
268269

269270
cleanup:
270271
continuation.cancel()

internal-api/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ excludedClassesCoverage += [
7474
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentHistogram",
7575
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentPropagation",
7676
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentTraceCollector",
77-
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopContinuation",
7877
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopPathwayContext",
7978
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopTraceConfig",
8079
"datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopTracerAPI",
@@ -90,6 +89,7 @@ excludedClassesCoverage += [
9089
"datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes",
9190
"datadog.trace.bootstrap.instrumentation.api.NoopAgentScope",
9291
"datadog.trace.bootstrap.instrumentation.api.NoopAgentSpan",
92+
"datadog.trace.bootstrap.instrumentation.api.NoopContinuation",
9393
"datadog.trace.bootstrap.instrumentation.api.NoopScope",
9494
"datadog.trace.bootstrap.instrumentation.api.NoopSpan",
9595
"datadog.trace.bootstrap.instrumentation.api.NoopSpanContext",

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

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ public static AgentScope noopScope() {
203203
return NoopScope.INSTANCE;
204204
}
205205

206+
/**
207+
* Returns the noop continuation instance.
208+
*
209+
* <p>This instance will always be the same, and can be safely tested using object identity (ie
210+
* {@code ==}).
211+
*
212+
* @return the noop continuation instance.
213+
*/
214+
public static AgentScope.Continuation noopContinuation() {
215+
return NoopContinuation.INSTANCE;
216+
}
217+
206218
public static final TracerAPI NOOP_TRACER = new NoopTracerAPI();
207219

208220
private static volatile TracerAPI provider = NOOP_TRACER;
@@ -619,28 +631,6 @@ public <C> AgentSpanContext.Extracted extract(final C carrier, final ContextVisi
619631
}
620632
}
621633

622-
static class NoopContinuation implements AgentScope.Continuation {
623-
static final NoopContinuation INSTANCE = new NoopContinuation();
624-
625-
@Override
626-
public AgentScope.Continuation hold() {
627-
return this;
628-
}
629-
630-
@Override
631-
public AgentScope activate() {
632-
return NoopScope.INSTANCE;
633-
}
634-
635-
@Override
636-
public AgentSpan getSpan() {
637-
return NoopSpan.INSTANCE;
638-
}
639-
640-
@Override
641-
public void cancel() {}
642-
}
643-
644634
public static class NoopAgentTraceCollector implements AgentTraceCollector {
645635
public static final NoopAgentTraceCollector INSTANCE = new NoopAgentTraceCollector();
646636

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package datadog.trace.bootstrap.instrumentation.api;
2+
3+
final class NoopContinuation implements AgentScope.Continuation {
4+
static final NoopContinuation INSTANCE = new NoopContinuation();
5+
6+
private NoopContinuation() {}
7+
8+
@Override
9+
public AgentScope.Continuation hold() {
10+
return this;
11+
}
12+
13+
@Override
14+
public AgentScope activate() {
15+
return NoopScope.INSTANCE;
16+
}
17+
18+
@Override
19+
public AgentSpan getSpan() {
20+
return NoopSpan.INSTANCE;
21+
}
22+
23+
@Override
24+
public void cancel() {}
25+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public byte source() {
1717

1818
@Override
1919
public Continuation capture() {
20-
return AgentTracer.NoopContinuation.INSTANCE;
20+
return NoopContinuation.INSTANCE;
2121
}
2222

2323
@Override

0 commit comments

Comments
 (0)