Skip to content

Commit 1486bda

Browse files
committed
Reduce use of activeScope() in instrumentation
1 parent 19d8832 commit 1486bda

File tree

14 files changed

+50
-70
lines changed

14 files changed

+50
-70
lines changed

dd-java-agent/instrumentation/couchbase/couchbase-2.6/src/main/java/datadog/trace/instrumentation/couchbase/client/CouchbaseCoreInstrumentation.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package datadog.trace.instrumentation.couchbase.client;
22

33
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
4-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
55
import static java.util.Collections.singletonMap;
66
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
77
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@@ -13,7 +13,6 @@
1313
import datadog.trace.agent.tooling.InstrumenterModule;
1414
import datadog.trace.bootstrap.ContextStore;
1515
import datadog.trace.bootstrap.InstrumentationContext;
16-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1716
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1817
import java.util.Map;
1918
import net.bytebuddy.asm.Advice;
@@ -52,18 +51,15 @@ public static class CouchbaseCoreAdvice {
5251
@Advice.OnMethodExit(suppress = Throwable.class)
5352
public static void addOperationIdToSpan(@Advice.Argument(0) final CouchbaseRequest request) {
5453

55-
final AgentScope scope = activeScope();
56-
if (scope != null) {
57-
// The scope from the initial rxJava subscribe is not available to the networking layer
54+
final AgentSpan span = activeSpan();
55+
if (span != null) {
56+
// The context from the initial rxJava subscribe is not available to the networking layer
5857
// To transfer the span, the span is added to the context store
5958

6059
final ContextStore<CouchbaseRequest, AgentSpan> contextStore =
6160
InstrumentationContext.get(CouchbaseRequest.class, AgentSpan.class);
6261

63-
AgentSpan span = contextStore.get(request);
64-
65-
if (span == null) {
66-
span = scope.span();
62+
if (contextStore.get(request) == null) {
6763
contextStore.put(request, span);
6864

6965
if (request.operationId() != null) {

dd-java-agent/instrumentation/cucumber/src/main/java/datadog/trace/instrumentation/cucumber/CucumberInstrumentation.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.google.auto.service.AutoService;
99
import datadog.trace.agent.tooling.Instrumenter;
1010
import datadog.trace.agent.tooling.InstrumenterModule;
11+
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1112
import io.cucumber.core.backend.StepDefinition;
1213
import net.bytebuddy.asm.Advice;
1314
import net.bytebuddy.description.type.TypeDescription;
@@ -46,14 +47,14 @@ public void methodAdvice(MethodTransformer transformer) {
4647

4748
public static class CucumberAdvice {
4849
@Advice.OnMethodEnter
49-
public static void onCucumberStepStart(
50+
public static AgentScope onCucumberStepStart(
5051
@Advice.This StepDefinition step, @Advice.Argument(0) Object[] arguments) {
51-
CucumberStepDecorator.DECORATE.onStepStart(step, arguments);
52+
return CucumberStepDecorator.DECORATE.onStepStart(step, arguments);
5253
}
5354

5455
@Advice.OnMethodExit
55-
public static void onCucumberStepFinish(@Advice.This StepDefinition step) {
56-
CucumberStepDecorator.DECORATE.onStepFinish(step);
56+
public static void onCucumberStepFinish(@Advice.Enter AgentScope scope) {
57+
CucumberStepDecorator.DECORATE.onStepFinish(scope);
5758
}
5859

5960
// Cucumber 5.0.0 and above

dd-java-agent/instrumentation/cucumber/src/main/java/datadog/trace/instrumentation/cucumber/CucumberStepDecorator.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ protected CharSequence component() {
2626
return "cucumber";
2727
}
2828

29-
public void onStepStart(StepDefinition step, Object[] arguments) {
29+
public AgentScope onStepStart(StepDefinition step, Object[] arguments) {
3030
AgentSpan span = AgentTracer.startSpan("cucumber", "cucumber.step");
31-
AgentScope scope = AgentTracer.activateSpan(span);
3231
afterStart(span);
3332

3433
span.setResourceName(step.getPattern());
@@ -38,20 +37,14 @@ public void onStepStart(StepDefinition step, Object[] arguments) {
3837
if (arguments != null && arguments.length > 0) {
3938
span.setTag("step.arguments", Arrays.toString(arguments));
4039
}
41-
}
42-
43-
public void onStepFinish(StepDefinition step) {
44-
AgentSpan span = AgentTracer.activeSpan();
45-
if (span == null) {
46-
return;
47-
}
4840

49-
AgentScope scope = AgentTracer.activeScope();
50-
if (scope != null) {
51-
scope.close();
52-
}
41+
return AgentTracer.activateSpan(span);
42+
}
5343

44+
public void onStepFinish(AgentScope scope) {
45+
AgentSpan span = scope.span();
5446
beforeFinish(span);
5547
span.finish();
48+
scope.close();
5649
}
5750
}

dd-java-agent/instrumentation/enable-wallclock-profiling/src/main/java/datadog/trace/instrumentation/wallclock/EnableWallclockProfilingInstrumentation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import datadog.trace.agent.tooling.InstrumenterModule;
1515
import datadog.trace.api.InstrumenterConfig;
1616
import datadog.trace.bootstrap.config.provider.ConfigProvider;
17-
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
17+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1818
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1919
import java.util.Arrays;
2020
import net.bytebuddy.asm.Advice;
@@ -86,8 +86,8 @@ public static final class EnableWallclockSampling {
8686

8787
@Advice.OnMethodEnter(suppress = Throwable.class)
8888
public static boolean before() {
89-
AgentScope active = AgentTracer.activeScope();
90-
if (active == null) {
89+
AgentSpan span = AgentTracer.activeSpan();
90+
if (span == null) {
9191
AgentTracer.get().getProfilingContext().onAttach();
9292
return true;
9393
}

dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/client/ClientStreamListenerImplInstrumentation.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
44
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
55
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
6-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
6+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
77
import static datadog.trace.instrumentation.grpc.client.GrpcClientDecorator.DECORATE;
88
import static net.bytebuddy.matcher.ElementMatchers.isConstructor;
99
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -66,9 +66,8 @@ public static final class Construct {
6666
@Advice.OnMethodExit
6767
public static void capture(@Advice.This ClientStreamListener listener) {
6868
// instrumentation of ClientCallImpl::start ensures this scope is present and valid
69-
AgentScope scope = activeScope();
70-
if (null != scope) {
71-
AgentSpan span = scope.span();
69+
AgentSpan span = activeSpan();
70+
if (null != span) {
7271
InstrumentationContext.get(ClientStreamListener.class, AgentSpan.class).put(listener, span);
7372
}
7473
}

dd-java-agent/instrumentation/http-url-connection/src/test/groovy/HttpUrlConnectionConnectFirstTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datadog.trace.agent.test.naming.TestingGenericHttpNamingConventions
22
import spock.lang.Timeout
33

4-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan
55

66
@Timeout(5)
77
class HttpUrlConnectionConnectFirstTest extends HttpUrlConnectionTest implements TestingGenericHttpNamingConventions.ClientV0{
@@ -15,7 +15,7 @@ class HttpUrlConnectionConnectFirstTest extends HttpUrlConnectionTest implements
1515
connection.setRequestProperty("Connection", "close")
1616
connection.connectTimeout = CONNECT_TIMEOUT_MS
1717
connection.readTimeout = READ_TIMEOUT_MS
18-
def parentSpan = activeScope()
18+
def parentSpan = activeSpan()
1919
connection.connect() // test connect before getting stream
2020
def stream
2121
try {
@@ -24,7 +24,7 @@ class HttpUrlConnectionConnectFirstTest extends HttpUrlConnectionTest implements
2424
stream = connection.errorStream
2525
ex.printStackTrace()
2626
}
27-
assert activeScope() == parentSpan
27+
assert activeSpan() == parentSpan
2828
stream?.readLines()
2929
stream?.close()
3030
callback?.call()

dd-java-agent/instrumentation/http-url-connection/src/test/groovy/HttpUrlConnectionTest.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import sun.net.www.protocol.https.HttpsURLConnectionImpl
1010

1111
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
1212
import static datadog.trace.api.config.TraceInstrumentationConfig.HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN
13-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope
13+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan
1414

1515
@Timeout(5)
1616
abstract class HttpUrlConnectionTest extends HttpClientTest {
@@ -28,15 +28,15 @@ abstract class HttpUrlConnectionTest extends HttpClientTest {
2828
connection.useCaches = true
2929
connection.connectTimeout = CONNECT_TIMEOUT_MS
3030
connection.readTimeout = READ_TIMEOUT_MS
31-
def parentSpan = activeScope()
31+
def parentSpan = activeSpan()
3232
def stream
3333
try {
3434
stream = connection.inputStream
3535
} catch (Exception ex) {
3636
stream = connection.errorStream
3737
ex.printStackTrace()
3838
}
39-
assert activeScope() == parentSpan
39+
assert activeSpan() == parentSpan
4040
stream?.readLines()
4141
stream?.close()
4242
callback?.call()
@@ -66,7 +66,7 @@ abstract class HttpUrlConnectionTest extends HttpClientTest {
6666
runUnderTrace("someTrace") {
6767
HttpURLConnection connection = url.openConnection()
6868
connection.useCaches = useCaches
69-
assert activeScope() != null
69+
assert activeSpan() != null
7070
def stream = connection.inputStream
7171
def lines = stream.readLines()
7272
stream.close()
@@ -76,7 +76,7 @@ abstract class HttpUrlConnectionTest extends HttpClientTest {
7676
// call again to ensure the cycling is ok
7777
connection = url.openConnection()
7878
connection.useCaches = useCaches
79-
assert activeScope() != null
79+
assert activeSpan() != null
8080
assert connection.getResponseCode() == STATUS // call before input stream to test alternate behavior
8181
connection.inputStream
8282
stream = connection.inputStream // one more to ensure state is working
@@ -157,7 +157,7 @@ abstract class HttpUrlConnectionTest extends HttpClientTest {
157157
HttpURLConnection connection = url.openConnection()
158158
connection.useCaches = useCaches
159159
connection.addRequestProperty("is-dd-server", "false")
160-
assert activeScope() != null
160+
assert activeSpan() != null
161161
def stream = connection.inputStream
162162
connection.inputStream // one more to ensure state is working
163163
def lines = stream.readLines()
@@ -169,7 +169,7 @@ abstract class HttpUrlConnectionTest extends HttpClientTest {
169169
connection = url.openConnection()
170170
connection.useCaches = useCaches
171171
connection.addRequestProperty("is-dd-server", "false")
172-
assert activeScope() != null
172+
assert activeSpan() != null
173173
assert connection.getResponseCode() == STATUS // call before input stream to test alternate behavior
174174
stream = connection.inputStream
175175
lines = stream.readLines()
@@ -247,7 +247,7 @@ abstract class HttpUrlConnectionTest extends HttpClientTest {
247247
HttpURLConnection connection = url.openConnection()
248248
connection.setRequestProperty("Connection", "close")
249249
connection.addRequestProperty("is-dd-server", "false")
250-
assert activeScope() != null
250+
assert activeSpan() != null
251251
assert connection.getResponseCode() == STATUS
252252
return connection
253253
}

dd-java-agent/instrumentation/http-url-connection/src/test/groovy/HttpUrlConnectionUseCachesFalseTest.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import datadog.trace.agent.test.naming.TestingGenericHttpNamingConventions
22
import spock.lang.Timeout
33

4-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan
55

66
@Timeout(5)
77
class HttpUrlConnectionUseCachesFalseTest extends HttpUrlConnectionTest implements TestingGenericHttpNamingConventions.ClientV0 {
@@ -16,15 +16,15 @@ class HttpUrlConnectionUseCachesFalseTest extends HttpUrlConnectionTest implemen
1616
connection.useCaches = false
1717
connection.connectTimeout = CONNECT_TIMEOUT_MS
1818
connection.readTimeout = READ_TIMEOUT_MS
19-
def parentSpan = activeScope()
19+
def parentSpan = activeSpan()
2020
def stream
2121
try {
2222
stream = connection.inputStream
2323
} catch (Exception ex) {
2424
stream = connection.errorStream
2525
ex.printStackTrace()
2626
}
27-
assert activeScope() == parentSpan
27+
assert activeSpan() == parentSpan
2828
stream?.readLines()
2929
stream?.close()
3030
callback?.call()

dd-java-agent/instrumentation/http-url-connection/src/test/groovy/UrlConnectionTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import datadog.trace.bootstrap.instrumentation.api.Tags
66
import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
77
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
88
import static datadog.trace.api.config.TraceInstrumentationConfig.HTTP_CLIENT_HOST_SPLIT_BY_DOMAIN
9-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope
9+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan
1010

1111
abstract class UrlConnectionTest extends VersionedNamingTestBase {
1212

@@ -17,7 +17,7 @@ abstract class UrlConnectionTest extends VersionedNamingTestBase {
1717
URLConnection connection = url.openConnection()
1818
connection.setConnectTimeout(10000)
1919
connection.setReadTimeout(10000)
20-
assert activeScope() != null
20+
assert activeSpan() != null
2121
connection.inputStream
2222
}
2323

dd-java-agent/instrumentation/java-concurrent/java-completablefuture/src/test/java/Fanout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
1+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
22

33
import datadog.trace.api.Trace;
44
import java.util.concurrent.CompletableFuture;
@@ -52,11 +52,11 @@ public void executeTwoLevels() {
5252
}
5353

5454
private void untracedWork() {
55-
assert null != activeScope();
55+
assert null != activeSpan();
5656
}
5757

5858
@Trace
5959
private void tracedWork() {
60-
assert null != activeScope();
60+
assert null != activeSpan();
6161
}
6262
}

0 commit comments

Comments
 (0)