Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -249,22 +250,22 @@ 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)
tracer.setAsyncPropagationEnabled(false)
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()
Expand Down
2 changes: 1 addition & 1 deletion internal-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ public static AgentScope noopScope() {
return NoopScope.INSTANCE;
}

/**
* Returns the noop continuation instance.
*
* <p>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;
Expand Down Expand Up @@ -619,28 +631,6 @@ public <C> AgentSpanContext.Extracted extract(final C carrier, final ContextVisi
}
}

static class NoopContinuation implements AgentScope.Continuation {
static final NoopContinuation INSTANCE = new NoopContinuation();

@Override
public AgentScope.Continuation hold() {
return this;
}

@Override
public AgentScope activate() {
return NoopScope.INSTANCE;
}

@Override
public AgentSpan getSpan() {
return NoopSpan.INSTANCE;
}

@Override
public void cancel() {}
}

public static class NoopAgentTraceCollector implements AgentTraceCollector {
public static final NoopAgentTraceCollector INSTANCE = new NoopAgentTraceCollector();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package datadog.trace.bootstrap.instrumentation.api;

final class NoopContinuation implements AgentScope.Continuation {
static final NoopContinuation INSTANCE = new NoopContinuation();

private NoopContinuation() {}

@Override
public AgentScope.Continuation hold() {
return this;
}

@Override
public AgentScope activate() {
return NoopScope.INSTANCE;
}

@Override
public AgentSpan getSpan() {
return NoopSpan.INSTANCE;
}

@Override
public void cancel() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public byte source() {

@Override
public Continuation capture() {
return AgentTracer.NoopContinuation.INSTANCE;
return NoopContinuation.INSTANCE;
}

@Override
Expand Down
Loading