Skip to content

Commit 79ed9a2

Browse files
committed
Return noop continuation when not async propagating
1 parent 2964589 commit 79ed9a2

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

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

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)