Skip to content

Commit e0f227e

Browse files
committed
Replace TraceScope.capture() with Tracer.captureActiveSpan()
1 parent 46d5b5c commit e0f227e

File tree

8 files changed

+86
-40
lines changed

8 files changed

+86
-40
lines changed

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.captureSpan(AgentTracer.activeSpan())
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-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package datadog.trace.instrumentation.zio.v2_0;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
4-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureSpan;
3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan;
54

65
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
7-
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
86
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
97
import datadog.trace.bootstrap.instrumentation.api.ScopeState;
108

@@ -18,8 +16,7 @@ private FiberContext(ScopeState state) {
1816
this.state = state;
1917
this.scope = null;
2018
this.oldState = null;
21-
AgentSpan span = activeSpan();
22-
this.continuation = null != span ? captureSpan(span) : null;
19+
this.continuation = captureActiveSpan();
2320
}
2421

2522
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

+23-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ 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+
* Prevent the currently active trace from reporting until the returned Continuation is either
30+
* activated (and the returned scope is closed), or canceled.
31+
*
32+
* <p>Should be called on the parent thread.
33+
*
34+
* @deprecated Unstable API. Might be removed at any time.
35+
* @return Continuation of the active span, no-op continuation if there's no active span.
36+
*/
37+
@Deprecated
38+
TraceScope.Continuation captureActiveSpan();
39+
1740
/**
1841
* Checks whether asynchronous propagation is enabled, meaning this context will propagate across
1942
* asynchronous boundaries.
@@ -36,15 +59,4 @@ public interface Tracer {
3659
*/
3760
@Deprecated
3861
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();
5062
}

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@
66

77
/** An object which can propagate a datadog trace across multiple threads. */
88
public interface TraceScope extends Closeable {
9+
10+
/** Close the activated context and allow any underlying spans to finish. */
11+
@Override
12+
void close();
13+
914
/**
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.
15+
* @deprecated Replaced by {@link Tracer#captureActiveSpan()}.
16+
* <p>Prevent the currently active trace from reporting until the returned Continuation is
17+
* either activated (and the returned scope is closed), or canceled. Should be called on the
18+
* parent thread.
19+
* @return Continuation of the active span, no-op continuation if there's no active span.
1420
*/
15-
Continuation capture();
21+
@Deprecated
22+
default Continuation capture() {
23+
return GlobalTracer.get().captureActiveSpan();
24+
}
1625

1726
/** @deprecated Replaced by {@code capture().hold()}. */
1827
@Deprecated
1928
default Continuation captureConcurrent() {
2029
return capture().hold();
2130
}
2231

23-
/** Close the activated context and allow any underlying spans to finish. */
24-
@Override
25-
void close();
26-
2732
/**
2833
* @deprecated Replaced by {@link Tracer#isAsyncPropagationEnabled()}.
2934
* <p>Calling this method will check whether asynchronous propagation is active <strong>for

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

+10
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,16 @@ public AgentScope activateSpan(AgentSpan span, ScopeSource source, boolean isAsy
932932
return scopeManager.activate(span, source, isAsyncPropagating);
933933
}
934934

935+
@Override
936+
public AgentScope.Continuation captureActiveSpan() {
937+
AgentSpan span = scopeManager.activeSpan();
938+
if (null != span) {
939+
return scopeManager.captureSpan(span);
940+
} else {
941+
return AgentTracer.noopContinuation();
942+
}
943+
}
944+
935945
@Override
936946
public AgentScope.Continuation captureSpan(final AgentSpan span) {
937947
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

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentTracer.java

+12
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public static AgentScope activateSpan(final AgentSpan span, final boolean isAsyn
9494
return get().activateSpan(span, ScopeSource.INSTRUMENTATION, isAsyncPropagating);
9595
}
9696

97+
public static AgentScope.Continuation captureActiveSpan() {
98+
return get().captureActiveSpan();
99+
}
100+
97101
public static AgentScope.Continuation captureSpan(final AgentSpan span) {
98102
return get().captureSpan(span);
99103
}
@@ -286,6 +290,9 @@ AgentSpan startSpan(
286290

287291
AgentScope activateSpan(AgentSpan span, ScopeSource source, boolean isAsyncPropagating);
288292

293+
@Override
294+
AgentScope.Continuation captureActiveSpan();
295+
289296
AgentScope.Continuation captureSpan(AgentSpan span);
290297

291298
void closePrevious(boolean finishSpan);
@@ -425,6 +432,11 @@ public AgentScope activateSpan(
425432
return NoopScope.INSTANCE;
426433
}
427434

435+
@Override
436+
public AgentScope.Continuation captureActiveSpan() {
437+
return NoopContinuation.INSTANCE;
438+
}
439+
428440
@Override
429441
public AgentScope.Continuation captureSpan(final AgentSpan span) {
430442
return NoopContinuation.INSTANCE;

0 commit comments

Comments
 (0)