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
@@ -1,10 +1,10 @@
package datadog.trace.bootstrap.instrumentation.java.concurrent;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.RUNNABLE;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.exclude;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import java.util.concurrent.RunnableFuture;
Expand All @@ -20,7 +20,7 @@ public static <T extends Runnable> Runnable wrap(T task) {
return task;
}
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != Context.root()) {
if (task instanceof Comparable) {
return new ComparableRunnable(task, continuation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.instrumentation.apachehttpasyncclient;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.instrumentation.apachehttpasyncclient.ApacheHttpAsyncClientDecorator.DECORATE;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down Expand Up @@ -33,7 +33,7 @@ public void completed(final T result) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish(); // Finish span before calling delegate

if (parentContinuation == noopContinuation()) {
if (parentContinuation.context() == Context.root()) {
completeDelegate(result);
} else {
try (final ContextScope scope = parentContinuation.resume()) {
Expand All @@ -49,7 +49,7 @@ public void failed(final Exception ex) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish(); // Finish span before calling delegate

if (parentContinuation == noopContinuation()) {
if (parentContinuation.context() == Context.root()) {
failDelegate(ex);
} else {
try (final ContextScope scope = parentContinuation.resume()) {
Expand All @@ -64,7 +64,7 @@ public void cancelled() {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish(); // Finish span before calling delegate

if (parentContinuation == noopContinuation()) {
if (parentContinuation.context() == Context.root()) {
cancelDelegate();
} else {
try (final ContextScope scope = parentContinuation.resume()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.instrumentation.apachehttpclient5;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.instrumentation.apachehttpclient5.ApacheHttpClientDecorator.DECORATE;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down Expand Up @@ -36,7 +36,7 @@ public void completed(final T result) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish(); // Finish span before calling delegate

if (parentContinuation == noopContinuation()) {
if (parentContinuation.context() == Context.root()) {
completeDelegate(result);
} else {
try (final ContextScope scope = parentContinuation.resume()) {
Expand All @@ -52,7 +52,7 @@ public void failed(final Exception ex) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish(); // Finish span before calling delegate

if (parentContinuation == noopContinuation()) {
if (parentContinuation.context() == Context.root()) {
failDelegate(ex);
} else {
try (final ContextScope scope = parentContinuation.resume()) {
Expand All @@ -67,7 +67,7 @@ public void cancelled() {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish(); // Finish span before calling delegate

if (parentContinuation == noopContinuation()) {
if (parentContinuation.context() == Context.root()) {
cancelDelegate();
} else {
try (final ContextScope scope = parentContinuation.resume()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datadog.trace.instrumentation.jetty12;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.RUNNABLE;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.exclude;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;

Expand All @@ -30,7 +30,7 @@ public static Runnable wrapIfNeeded(final Runnable task) {
return task;
}
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != Context.root()) {
return new JettyRunnableWrapper(task, continuation);
}
return task; // don't wrap unless there is a scope to propagate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package datadog.trace.instrumentation.mongo;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;

import com.mongodb.internal.async.SingleResultCallback;
import datadog.context.Context;
Comment thread
mcculls marked this conversation as resolved.
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
Expand Down Expand Up @@ -56,7 +56,7 @@ private ContextContinuation getAndResetContinuation() {

public static SingleResultCallback<Object> wrapIfRequired(SingleResultCallback<Object> callback) {
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != Context.root()) {
Comment thread
mcculls marked this conversation as resolved.
return new CallbackWrapper<>(continuation, callback);
}
return callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.rootContext;
import static datadog.trace.instrumentation.netty38.NettyChannelPipelineInstrumentation.ADDITIONAL_INSTRUMENTATION_NAMES;
import static datadog.trace.instrumentation.netty38.NettyChannelPipelineInstrumentation.INSTRUMENTATION_NAME;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static class ChannelConnectAdvice extends AbstractNettyAdvice {
@Advice.OnMethodEnter
public static void addConnectContinuation(@Advice.This final Channel channel) {
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != rootContext()) {
final ContextStore<Channel, ChannelTraceContext> contextStore =
InstrumentationContext.get(Channel.class, ChannelTraceContext.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.rootContext;
import static datadog.trace.instrumentation.netty40.AttributeKeys.CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.returns;
Expand Down Expand Up @@ -232,7 +232,7 @@ public static class ConnectAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void addParentSpan(@Advice.This final ChannelPipeline pipeline) {
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != rootContext()) {
final Attribute<ContextContinuation> attribute =
pipeline.channel().attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY);
if (!attribute.compareAndSet(null, continuation)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.rootContext;
import static datadog.trace.instrumentation.netty41.AttributeKeys.CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY;
import static datadog.trace.instrumentation.netty41.AttributeKeys.HTTP2_CONNECTION_CODEC_ATTRIBUTE_KEY;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand Down Expand Up @@ -257,7 +257,7 @@ public static class ConnectAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static boolean addParentSpan(@Advice.This final ChannelPipeline pipeline) {
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != rootContext()) {
final Attribute<ContextContinuation> attribute =
pipeline.channel().attr(CONNECT_PARENT_CONTINUATION_ATTRIBUTE_KEY);
if (!attribute.compareAndSet(null, continuation)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.instrumentation.netty4.promise;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import io.netty.util.concurrent.Future;
Expand All @@ -17,7 +17,7 @@ public static GenericFutureListener wrapIfNeeded(final GenericFutureListener lis
return listener;
}
ContextContinuation continuation = captureActiveSpan();
if (continuation == noopContinuation()) {
if (continuation.context() == Context.root()) {
return listener;
}
if (listener instanceof GenericProgressiveFutureListener) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datadog.context.Context
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTags
Expand Down Expand Up @@ -28,7 +29,6 @@ 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

class OpenTracing31Test extends InstrumentationSpecification {

Expand Down Expand Up @@ -165,7 +165,7 @@ class OpenTracing31Test extends InstrumentationSpecification {
span instanceof MutableSpan
scope instanceof TraceScope
!internalTracer.isAsyncPropagationEnabled()
(scope as TraceScope).capture() == noopContinuation()
(scope as TraceScope).capture().context() == Context.root()
(tracer.scopeManager().active().span().delegate == span.delegate)

when:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datadog.context.Context
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTags
Expand Down Expand Up @@ -34,7 +35,6 @@ 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

class OpenTracing32Test extends InstrumentationSpecification {

Expand Down Expand Up @@ -176,7 +176,7 @@ class OpenTracing32Test extends InstrumentationSpecification {
span instanceof MutableSpan
scope instanceof TraceScope
!internalTracer.isAsyncPropagationEnabled()
(scope as TraceScope).capture() == noopContinuation()
(scope as TraceScope).capture().context() == Context.root()
(tracer.scopeManager().active().span().delegate == span.delegate)

when:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datadog.trace.instrumentation.springscheduling;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan;
import static datadog.trace.instrumentation.springscheduling.SpringSchedulingDecorator.DECORATE;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
Expand Down Expand Up @@ -35,7 +35,7 @@ public Object[] getArguments() {
@Override
public Object proceed() throws Throwable {
CharSequence spanName = DECORATE.spanNameForMethod(delegate.getMethod());
if (continuation != noopContinuation()) {
if (continuation.context() != Context.root()) {
return invokeWithContinuation(spanName);
} else {
return invokeWithSpan(spanName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.rootContext;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext;
import static datadog.trace.instrumentation.synapse3.SynapseClientDecorator.DECORATE;
import static datadog.trace.instrumentation.synapse3.SynapseClientDecorator.SYNAPSE_CONTINUATION_KEY;
Expand Down Expand Up @@ -56,7 +56,7 @@ public static final class NewClientWorkerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void createWorker(@Advice.Argument(2) final TargetResponse response) {
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != rootContext()) {
response.getConnection().getContext().setAttribute(SYNAPSE_CONTINUATION_KEY, continuation);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.rootContext;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext;
import static datadog.trace.instrumentation.synapse3.SynapseServerDecorator.DECORATE;
import static datadog.trace.instrumentation.synapse3.SynapseServerDecorator.SYNAPSE_CONTEXT_KEY;
Expand Down Expand Up @@ -60,7 +60,7 @@ public static final class NewServerWorkerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void createWorker(@Advice.Argument(0) final SourceRequest request) {
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != rootContext()) {
request.getConnection().getContext().setAttribute(SYNAPSE_CONTINUATION_KEY, continuation);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datadog.trace.instrumentation.undertow;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.RUNNABLE;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.exclude;

import datadog.context.Context;
import datadog.context.ContextContinuation;
import datadog.context.ContextScope;
import io.undertow.server.HttpServerExchange;
Expand Down Expand Up @@ -34,7 +34,7 @@ public static Runnable wrapIfNeeded(final Runnable task, HttpServerExchange exch
return task;
}
ContextContinuation continuation = captureActiveSpan();
if (continuation != noopContinuation()) {
if (continuation.context() != Context.root()) {
return new UndertowRunnableWrapper(task, exchange, continuation);
}
return task; // don't wrap unless there is a span to propagate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTraceCollector;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.NoopContinuation;
import datadog.trace.bootstrap.instrumentation.api.ProfilerContext;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import datadog.trace.core.monitor.HealthMetrics;
Expand All @@ -49,6 +50,9 @@ public final class ContinuableScopeManager {

static final Logger log = LoggerFactory.getLogger(ContinuableScopeManager.class);
static final RatelimitedLogger ratelimitedLog = new RatelimitedLogger(log, 1, MINUTES);

private static final NoopContinuation ROOT_CONTINUATION = NoopContinuation.INSTANCE;

static final long iterationKeepAlive =
SECONDS.toMillis(Config.get().getScopeIterationKeepAlive());
volatile ConcurrentMap<ScopeStack, ContinuableScope> rootIterationScopes;
Expand Down Expand Up @@ -112,7 +116,7 @@ public AgentScope.Continuation captureActiveSpan() {
return captureSpan(activeScope.context, activeScope.source(), span);
}
}
return AgentTracer.noopContinuation();
return ROOT_CONTINUATION;
}

public ContextContinuation captureSpan(final AgentSpan span) {
Expand Down Expand Up @@ -408,12 +412,16 @@ public Context swap(@NonNull Context context) {
}

public ContextContinuation capture(@NonNull Context context) {
if (context == Context.root()) {
return ROOT_CONTINUATION;
}

// respect async propagation flag for Context.current().capture()
ContinuableScope activeScope = scopeStack().active();
if (activeScope != null
&& activeScope.context == context
&& !activeScope.isAsyncPropagating()) {
return AgentTracer.noopContinuation();
&& !activeScope.isAsyncPropagating()
&& activeScope.context == context) {
return ROOT_CONTINUATION;
}
AgentSpan span = AgentSpan.fromContext(context);
AgentTraceCollector traceCollector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package datadog.trace.core.scopemanager;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopContinuation;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.core.scopemanager.ScopeManagerForkedTest.EVENT.ACTIVATE;
import static datadog.trace.core.scopemanager.ScopeManagerForkedTest.EVENT.CLOSE;
Expand Down Expand Up @@ -205,12 +204,12 @@ void ddScopeCreatesNoOpContinuationsWhenPropagationIsNotSet() {
tracer.setAsyncPropagationEnabled(false);
ContextContinuation continuation = tracer.captureActiveSpan();

assertSame(noopContinuation(), continuation);
assertSame(Context.root(), continuation.context());

tracer.setAsyncPropagationEnabled(true);
continuation = tracer.captureActiveSpan();

assertNotSame(noopContinuation(), continuation);
assertNotSame(Context.root(), continuation.context());
assertNotNull(continuation);

continuation.release();
Expand Down
Loading
Loading