Skip to content

Commit ddbd69a

Browse files
authored
Move continuation capture methods from scope to tracer (#8371)
replaces TraceScope.capture() with Tracer.captureActiveSpan()
1 parent c9b57a0 commit ddbd69a

File tree

16 files changed

+123
-100
lines changed

16 files changed

+123
-100
lines changed

dd-java-agent/instrumentation/opentelemetry/opentelemetry-0.3/src/main/java/datadog/trace/instrumentation/opentelemetry/OtelScope.java

-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ public class OtelScope implements Scope, TraceScope {
1111
this.delegate = delegate;
1212
}
1313

14-
@Override
15-
public Continuation capture() {
16-
return delegate.capture();
17-
}
18-
1914
@Override
2015
public void close() {
2116
delegate.close();

dd-java-agent/instrumentation/opentracing/api-0.31/src/main/java/datadog/trace/instrumentation/opentracing31/OTScopeManager.java

-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ public Span span() {
6363
return converter.toSpan(delegate.span());
6464
}
6565

66-
@Override
67-
public Continuation capture() {
68-
return delegate.capture();
69-
}
70-
7166
public boolean isFinishSpanOnClose() {
7267
return finishSpanOnClose;
7368
}

dd-java-agent/instrumentation/opentracing/api-0.32/src/main/java/datadog/trace/instrumentation/opentracing32/OTScopeManager.java

-5
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ public Span span() {
7373
return converter.toSpan(delegate.span());
7474
}
7575

76-
@Override
77-
public Continuation capture() {
78-
return delegate.capture();
79-
}
80-
8176
public boolean isFinishSpanOnClose() {
8277
return finishSpanOnClose;
8378
}

dd-java-agent/instrumentation/play-ws-1/src/main/java/datadog/trace/instrumentation/playws1/AsyncHandlerWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.trace.instrumentation.playws1;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.capture;
3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureSpan;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled;
55
import static datadog.trace.instrumentation.playws.PlayWSClientDecorator.DECORATE;
66

@@ -22,7 +22,7 @@ public class AsyncHandlerWrapper implements AsyncHandler {
2222
public AsyncHandlerWrapper(final AsyncHandler delegate, final AgentSpan span) {
2323
this.delegate = delegate;
2424
this.span = span;
25-
continuation = capture();
25+
continuation = captureSpan(span);
2626
}
2727

2828
@Override

dd-java-agent/instrumentation/play-ws-2.1/src/main/java/datadog/trace/instrumentation/playws21/AsyncHandlerWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.trace.instrumentation.playws21;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.capture;
3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureSpan;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled;
55
import static datadog.trace.instrumentation.playws.PlayWSClientDecorator.DECORATE;
66

@@ -27,7 +27,7 @@ public class AsyncHandlerWrapper implements AsyncHandler {
2727
public AsyncHandlerWrapper(final AsyncHandler delegate, final AgentSpan span) {
2828
this.delegate = delegate;
2929
this.span = span;
30-
continuation = capture();
30+
continuation = captureSpan(span);
3131
}
3232

3333
@Override

dd-java-agent/instrumentation/play-ws-2/src/main/java/datadog/trace/instrumentation/playws2/AsyncHandlerWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.trace.instrumentation.playws2;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.capture;
3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureSpan;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.setAsyncPropagationEnabled;
55
import static datadog.trace.instrumentation.playws.PlayWSClientDecorator.DECORATE;
66

@@ -26,7 +26,7 @@ public class AsyncHandlerWrapper implements AsyncHandler {
2626
public AsyncHandlerWrapper(final AsyncHandler delegate, final AgentSpan span) {
2727
this.delegate = delegate;
2828
this.span = span;
29-
continuation = capture();
29+
continuation = captureSpan(span);
3030
}
3131

3232
@Override

dd-java-agent/instrumentation/servicetalk/src/test/groovy/ContextPreservingInstrumentationTest.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ContextPreservingInstrumentationTest extends AgentTestRunner {
113113
*/
114114
private class ParentContext {
115115
final ContextMap contextMap = AsyncContext.context().copy()
116-
final AgentScope.Continuation spanContinuation = AgentTracer.capture()
116+
final AgentScope.Continuation spanContinuation = AgentTracer.captureActiveSpan()
117117

118118
def releaseParentSpan() {
119119
spanContinuation.cancel()

dd-java-agent/instrumentation/zio/zio-2.0/src/main/java/datadog/trace/instrumentation/zio/v2_0/FiberContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package datadog.trace.instrumentation.zio.v2_0;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.capture;
3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
44

55
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
66
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
@@ -16,7 +16,7 @@ private FiberContext(ScopeState state) {
1616
this.state = state;
1717
this.scope = null;
1818
this.oldState = null;
19-
this.continuation = capture();
19+
this.continuation = captureActiveSpan();
2020
}
2121

2222
public static FiberContext create() {

dd-trace-api/src/main/java/datadog/trace/api/GlobalTracer.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@ public String getSpanId() {
2626
}
2727

2828
@Override
29-
public boolean isAsyncPropagationEnabled() {
29+
public boolean addTraceInterceptor(TraceInterceptor traceInterceptor) {
3030
return false;
3131
}
3232

3333
@Override
34-
public void setAsyncPropagationEnabled(boolean asyncPropagationEnabled) {}
34+
public TraceScope muteTracing() {
35+
return NoopTraceScope.INSTANCE;
36+
}
3537

3638
@Override
37-
public boolean addTraceInterceptor(TraceInterceptor traceInterceptor) {
38-
return false;
39+
public TraceScope.Continuation captureActiveSpan() {
40+
return NoopTraceScope.NoopContinuation.INSTANCE;
3941
}
4042

4143
@Override
42-
public TraceScope muteTracing() {
43-
return NoopTraceScope.INSTANCE;
44+
public boolean isAsyncPropagationEnabled() {
45+
return false;
4446
}
47+
48+
@Override
49+
public void setAsyncPropagationEnabled(boolean asyncPropagationEnabled) {}
4550
};
4651

4752
private static final Collection<Callback> installationCallbacks = new ArrayList<>();

dd-trace-api/src/main/java/datadog/trace/api/Tracer.java

+25-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,31 @@ public interface Tracer {
1414
*/
1515
String getSpanId();
1616

17+
/**
18+
* Add a new interceptor to the tracer. Interceptors with duplicate priority to existing ones are
19+
* ignored.
20+
*
21+
* @param traceInterceptor
22+
* @return false if an interceptor with same priority exists.
23+
*/
24+
boolean addTraceInterceptor(TraceInterceptor traceInterceptor);
25+
26+
TraceScope muteTracing();
27+
28+
/**
29+
* When asynchronous propagation is enabled, prevent the currently active trace from reporting
30+
* until the returned Continuation is either activated (and the returned scope is closed) or the
31+
* continuation is canceled.
32+
*
33+
* <p>Should be called on the parent thread.
34+
*
35+
* @deprecated Unstable API. Might be removed at any time.
36+
* @return Continuation of the active span, no-op continuation if there's no active span or
37+
* asynchronous propagation is disabled.
38+
*/
39+
@Deprecated
40+
TraceScope.Continuation captureActiveSpan();
41+
1742
/**
1843
* Checks whether asynchronous propagation is enabled, meaning this context will propagate across
1944
* asynchronous boundaries.
@@ -36,15 +61,4 @@ public interface Tracer {
3661
*/
3762
@Deprecated
3863
void setAsyncPropagationEnabled(boolean asyncPropagationEnabled);
39-
40-
/**
41-
* Add a new interceptor to the tracer. Interceptors with duplicate priority to existing ones are
42-
* ignored.
43-
*
44-
* @param traceInterceptor
45-
* @return false if an interceptor with same priority exists.
46-
*/
47-
boolean addTraceInterceptor(TraceInterceptor traceInterceptor);
48-
49-
TraceScope muteTracing();
5064
}

dd-trace-api/src/main/java/datadog/trace/context/NoopTraceScope.java

-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ public void cancel() {}
2424

2525
private NoopTraceScope() {}
2626

27-
@Override
28-
public Continuation capture() {
29-
return NoopContinuation.INSTANCE;
30-
}
31-
3227
@Override
3328
public void close() {}
3429
}

dd-trace-api/src/main/java/datadog/trace/context/TraceScope.java

+43-36
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,11 @@
66

77
/** An object which can propagate a datadog trace across multiple threads. */
88
public interface TraceScope extends Closeable {
9-
/**
10-
* Prevent the trace attached to this TraceScope from reporting until the returned Continuation is
11-
* either activated (and the returned scope is closed), or canceled.
12-
*
13-
* <p>Should be called on the parent thread.
14-
*/
15-
Continuation capture();
16-
17-
/** @deprecated Replaced by {@code capture().hold()}. */
18-
@Deprecated
19-
default Continuation captureConcurrent() {
20-
return capture().hold();
21-
}
229

2310
/** Close the activated context and allow any underlying spans to finish. */
2411
@Override
2512
void close();
2613

27-
/**
28-
* @deprecated Replaced by {@link Tracer#isAsyncPropagationEnabled()}.
29-
* <p>Calling this method will check whether asynchronous propagation is active <strong>for
30-
* the active scope</strong>, not this scope instance.
31-
* @return {@code true} if asynchronous propagation is enabled <strong>for the active
32-
* scope</strong>, {@code false} otherwise.
33-
*/
34-
@Deprecated
35-
default boolean isAsyncPropagating() {
36-
return GlobalTracer.get().isAsyncPropagationEnabled();
37-
}
38-
39-
/**
40-
* @deprecated Replaced by {@link Tracer#setAsyncPropagationEnabled(boolean)}}.
41-
* <p>Calling this method will enable or disable asynchronous propagation <strong>for the
42-
* active scope</strong>, not this scope instance.
43-
* @param value {@code true} to enable asynchronous propagation, {@code false} to disable it.
44-
*/
45-
@Deprecated
46-
default void setAsyncPropagation(boolean value) {
47-
GlobalTracer.get().setAsyncPropagationEnabled(value);
48-
}
49-
5014
/**
5115
* Used to pass async context between workers. A trace will not be reported until all spans and
5216
* continuations are resolved. You must call activate (and close on the returned scope) or cancel
@@ -76,4 +40,47 @@ interface Continuation {
7640
/** Allow trace to stop waiting on this continuation for reporting. */
7741
void cancel();
7842
}
43+
44+
/**
45+
* @deprecated Replaced by {@link Tracer#captureActiveSpan()}.
46+
* <p>When asynchronous propagation is enabled, prevent the <strong>currently active
47+
* trace</strong>, which may differ from this scope instance, from reporting until the
48+
* returned Continuation is either activated (and the returned scope is closed) or the
49+
* continuation is canceled. Should be called on the parent thread.
50+
* @return Continuation of the active span, no-op continuation if there's no active span or
51+
* asynchronous propagation is disabled.
52+
*/
53+
@Deprecated
54+
default Continuation capture() {
55+
return GlobalTracer.get().captureActiveSpan();
56+
}
57+
58+
/** @deprecated Replaced by {@code capture().hold()}. */
59+
@Deprecated
60+
default Continuation captureConcurrent() {
61+
return capture().hold();
62+
}
63+
64+
/**
65+
* @deprecated Replaced by {@link Tracer#isAsyncPropagationEnabled()}.
66+
* <p>Calling this method will check whether asynchronous propagation is enabled <strong>for
67+
* the active scope</strong>, not this scope instance.
68+
* @return {@code true} if asynchronous propagation is enabled <strong>for the active
69+
* scope</strong>, {@code false} otherwise.
70+
*/
71+
@Deprecated
72+
default boolean isAsyncPropagating() {
73+
return GlobalTracer.get().isAsyncPropagationEnabled();
74+
}
75+
76+
/**
77+
* @deprecated Replaced by {@link Tracer#setAsyncPropagationEnabled(boolean)}}.
78+
* <p>Calling this method will enable or disable asynchronous propagation <strong>for the
79+
* active scope</strong>, not this scope instance.
80+
* @param value {@code true} to enable asynchronous propagation, {@code false} to disable it.
81+
*/
82+
@Deprecated
83+
default void setAsyncPropagation(boolean value) {
84+
GlobalTracer.get().setAsyncPropagationEnabled(value);
85+
}
7986
}

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

+10
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,16 @@ public AgentScope activateSpan(AgentSpan span, ScopeSource source, boolean isAsy
929929
return scopeManager.activate(span, source, isAsyncPropagating);
930930
}
931931

932+
@Override
933+
public AgentScope.Continuation captureActiveSpan() {
934+
AgentScope activeScope = this.scopeManager.active();
935+
if (null != activeScope) {
936+
return activeScope.capture();
937+
} else {
938+
return AgentTracer.noopContinuation();
939+
}
940+
}
941+
932942
@Override
933943
public AgentScope.Continuation captureSpan(final AgentSpan span) {
934944
return scopeManager.captureSpan(span);

dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -430,23 +430,28 @@ public String getSpanId() {
430430
}
431431

432432
@Override
433-
public boolean isAsyncPropagationEnabled() {
434-
return tracer.isAsyncPropagationEnabled();
433+
public boolean addTraceInterceptor(final TraceInterceptor traceInterceptor) {
434+
return tracer.addTraceInterceptor(traceInterceptor);
435435
}
436436

437437
@Override
438-
public void setAsyncPropagationEnabled(boolean asyncPropagationEnabled) {
439-
tracer.setAsyncPropagationEnabled(asyncPropagationEnabled);
438+
public TraceScope muteTracing() {
439+
return tracer.muteTracing();
440440
}
441441

442442
@Override
443-
public boolean addTraceInterceptor(final TraceInterceptor traceInterceptor) {
444-
return tracer.addTraceInterceptor(traceInterceptor);
443+
public TraceScope.Continuation captureActiveSpan() {
444+
return tracer.captureActiveSpan();
445445
}
446446

447447
@Override
448-
public TraceScope muteTracing() {
449-
return tracer.muteTracing();
448+
public boolean isAsyncPropagationEnabled() {
449+
return tracer.isAsyncPropagationEnabled();
450+
}
451+
452+
@Override
453+
public void setAsyncPropagationEnabled(boolean asyncPropagationEnabled) {
454+
tracer.setAsyncPropagationEnabled(asyncPropagationEnabled);
450455
}
451456

452457
@Override

dd-trace-ot/src/main/java/datadog/opentracing/OTScopeManager.java

-5
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,6 @@ public int hashCode() {
9191
return delegate.hashCode();
9292
}
9393

94-
@Override
95-
public Continuation capture() {
96-
return delegate.capture();
97-
}
98-
9994
boolean isFinishSpanOnClose() {
10095
return finishSpanOnClose;
10196
}

0 commit comments

Comments
 (0)