diff --git a/dd-java-agent/instrumentation/opentracing/api-0.31/src/test/groovy/OpenTracing31Test.groovy b/dd-java-agent/instrumentation/opentracing/api-0.31/src/test/groovy/OpenTracing31Test.groovy index 706636ca840..50675668882 100644 --- a/dd-java-agent/instrumentation/opentracing/api-0.31/src/test/groovy/OpenTracing31Test.groovy +++ b/dd-java-agent/instrumentation/opentracing/api-0.31/src/test/groovy/OpenTracing31Test.groovy @@ -28,6 +28,7 @@ import io.opentracing.util.GlobalTracer import spock.lang.Subject import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled class OpenTracing31Test extends AgentTestRunner { @@ -164,7 +165,7 @@ class OpenTracing31Test extends AgentTestRunner { span instanceof MutableSpan scope instanceof TraceScope !internalTracer.isAsyncPropagationEnabled() - (scope as TraceScope).capture() == null + (scope as TraceScope).capture() == noopContinuation() (tracer.scopeManager().active().span().delegate == span.delegate) when: diff --git a/dd-java-agent/instrumentation/opentracing/api-0.32/src/test/groovy/OpenTracing32Test.groovy b/dd-java-agent/instrumentation/opentracing/api-0.32/src/test/groovy/OpenTracing32Test.groovy index cc8baddfaf2..3015ddd1d87 100644 --- a/dd-java-agent/instrumentation/opentracing/api-0.32/src/test/groovy/OpenTracing32Test.groovy +++ b/dd-java-agent/instrumentation/opentracing/api-0.32/src/test/groovy/OpenTracing32Test.groovy @@ -33,6 +33,7 @@ import static datadog.trace.api.sampling.PrioritySampling.USER_KEEP import static datadog.trace.api.sampling.SamplingMechanism.AGENT_RATE import static datadog.trace.api.sampling.SamplingMechanism.DEFAULT import static datadog.trace.api.sampling.SamplingMechanism.MANUAL +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled class OpenTracing32Test extends AgentTestRunner { @@ -174,7 +175,7 @@ class OpenTracing32Test extends AgentTestRunner { span instanceof MutableSpan scope instanceof TraceScope !internalTracer.isAsyncPropagationEnabled() - (scope as TraceScope).capture() == null + (scope as TraceScope).capture() == noopContinuation() (tracer.scopeManager().active().span().delegate == span.delegate) when: diff --git a/dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScope.java b/dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScope.java index 72b3ef9ccd6..578cf5084f6 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScope.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScope.java @@ -5,6 +5,7 @@ import datadog.trace.api.scopemanager.ScopeListener; import datadog.trace.bootstrap.instrumentation.api.AgentScope; import datadog.trace.bootstrap.instrumentation.api.AgentSpan; +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper; import datadog.trace.bootstrap.instrumentation.api.ScopeSource; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; @@ -132,13 +133,14 @@ public final void setAsyncPropagation(final boolean value) { /** * The continuation returned must be closed or activated or the trace will not finish. * - * @return The new continuation, or null if this scope is not async propagating. + * @return The new continuation, or {@link AgentTracer#noopContinuation()} if this scope is not + * async propagating. */ @Override - public final ScopeContinuation capture() { + public final Continuation capture() { return isAsyncPropagating ? new ScopeContinuation(scopeManager, span, source()).register() - : null; + : AgentTracer.noopContinuation(); } @Override diff --git a/dd-trace-core/src/test/groovy/datadog/trace/core/scopemanager/ScopeManagerTest.groovy b/dd-trace-core/src/test/groovy/datadog/trace/core/scopemanager/ScopeManagerTest.groovy index 367aad65a21..e02b2074315 100644 --- a/dd-trace-core/src/test/groovy/datadog/trace/core/scopemanager/ScopeManagerTest.groovy +++ b/dd-trace-core/src/test/groovy/datadog/trace/core/scopemanager/ScopeManagerTest.groovy @@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicReference +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan import static datadog.trace.core.scopemanager.EVENT.ACTIVATE import static datadog.trace.core.scopemanager.EVENT.CLOSE @@ -249,7 +250,7 @@ class ScopeManagerTest extends DDCoreSpecification { writer == [] } - def "DDScope only creates continuations when propagation is set"() { + def "DDScope creates no-op continuations when propagation is not set"() { when: def span = tracer.buildSpan("test", "test").start() def scope = tracer.activateSpan(span) @@ -257,14 +258,14 @@ class ScopeManagerTest extends DDCoreSpecification { def continuation = scope.capture() then: - continuation == null + continuation == noopContinuation() when: tracer.setAsyncPropagationEnabled(true) continuation = scope.capture() then: - continuation != null + continuation != noopContinuation() && continuation != null cleanup: continuation.cancel() diff --git a/internal-api/build.gradle b/internal-api/build.gradle index 35d512693be..e5b9fee70c9 100644 --- a/internal-api/build.gradle +++ b/internal-api/build.gradle @@ -74,7 +74,6 @@ excludedClassesCoverage += [ "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentHistogram", "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentPropagation", "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopAgentTraceCollector", - "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopContinuation", "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopPathwayContext", "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopTraceConfig", "datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopTracerAPI", @@ -90,6 +89,7 @@ excludedClassesCoverage += [ "datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes", "datadog.trace.bootstrap.instrumentation.api.NoopAgentScope", "datadog.trace.bootstrap.instrumentation.api.NoopAgentSpan", + "datadog.trace.bootstrap.instrumentation.api.NoopContinuation", "datadog.trace.bootstrap.instrumentation.api.NoopScope", "datadog.trace.bootstrap.instrumentation.api.NoopSpan", "datadog.trace.bootstrap.instrumentation.api.NoopSpanContext", diff --git a/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java b/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java index ac5e54d8f7a..abf544406a6 100644 --- a/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java +++ b/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java @@ -203,6 +203,18 @@ public static AgentScope noopScope() { return NoopScope.INSTANCE; } + /** + * Returns the noop continuation instance. + * + *
This instance will always be the same, and can be safely tested using object identity (ie
+ * {@code ==}).
+ *
+ * @return the noop continuation instance.
+ */
+ public static AgentScope.Continuation noopContinuation() {
+ return NoopContinuation.INSTANCE;
+ }
+
public static final TracerAPI NOOP_TRACER = new NoopTracerAPI();
private static volatile TracerAPI provider = NOOP_TRACER;
@@ -619,28 +631,6 @@ public