Skip to content

Commit 2a4a5ad

Browse files
committed
feat(core): Extract span and scope implementations from tracer class
Remove unused eligibleForDropping method
1 parent f677ee0 commit 2a4a5ad

File tree

44 files changed

+943
-774
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+943
-774
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package datadog.trace.bootstrap.instrumentation.api;
2+
3+
/**
4+
* A helper for accessing methods that rely on new Java 8 bytecode features such as calling a static
5+
* interface methods. In instrumentation, we may need to call these methods in code that is inlined
6+
* into an instrumented class, however many times the instrumented class has been compiled to a
7+
* previous version of bytecode, and so we cannot inline calls to static interface methods, as those
8+
* were not supported prior to Java 8 and will lead to a class verification error.
9+
*/
10+
public class Java8BytecodeBridge {
11+
/** @see AgentScope#noop() */
12+
public static AgentScope noopScope() {
13+
return AgentScope.noop();
14+
}
15+
16+
/** @see AgentSpanContext#noop() */
17+
public static AgentSpanContext noopSpanContext() {
18+
return AgentSpanContext.noop();
19+
}
20+
21+
/** @see AgentTracer#noopSpan() */
22+
public static AgentSpan noopSpan() {
23+
return AgentTracer.noopSpan();
24+
}
25+
26+
/** @see AgentTracer#blackholeSpan() */
27+
public static AgentSpan blackHoleSpan() {
28+
return AgentTracer.blackholeSpan();
29+
}
30+
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/java/concurrent/ConcurrentState.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package datadog.trace.bootstrap.instrumentation.java.concurrent;
22

3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
34
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ContinuationClaim.CLAIMED;
45

56
import datadog.trace.bootstrap.ContextStore;
67
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
7-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
88
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
@@ -32,7 +32,7 @@ private ConcurrentState() {}
3232
public static <K> ConcurrentState captureScope(
3333
ContextStore<K, ConcurrentState> contextStore, K key, AgentScope scope) {
3434
if (scope != null && scope.isAsyncPropagating()) {
35-
if (scope.span() instanceof AgentTracer.NoopAgentSpan) {
35+
if (scope.span() == noopSpan()) {
3636
return null;
3737
}
3838
final ConcurrentState state = contextStore.putIfAbsent(key, FACTORY);

dd-java-agent/instrumentation/akka-concurrent/src/main/java/datadog/trace/instrumentation/akka/concurrent/AkkaActorCellInstrumentation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
55
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
7+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
78
import static java.util.Collections.singletonMap;
89
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
910

@@ -13,7 +14,6 @@
1314
import datadog.trace.agent.tooling.InstrumenterModule;
1415
import datadog.trace.bootstrap.InstrumentationContext;
1516
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
16-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1717
import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils;
1818
import datadog.trace.bootstrap.instrumentation.java.concurrent.State;
1919
import java.util.Map;
@@ -72,11 +72,11 @@ public static AgentScope enter(
7272
return null;
7373
}
7474
// If there is a noop span in the active scope, we can clean all the way to this scope
75-
if (activeSpan() instanceof AgentTracer.NoopAgentSpan) {
75+
if (activeSpan() == noopSpan()) {
7676
return activeScope;
7777
}
7878
// Create an active scope with a noop span, and clean all the way to the previous scope
79-
localScope = activateSpan(AgentTracer.NoopAgentSpan.INSTANCE, false);
79+
localScope = activateSpan(noopSpan(), false);
8080
return activeScope;
8181
}
8282

dd-java-agent/instrumentation/akka-concurrent/src/main/java/datadog/trace/instrumentation/akka/concurrent/AkkaMailboxInstrumentation.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
55
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
7+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
78
import static java.util.Collections.singletonList;
89
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
910

@@ -12,7 +13,6 @@
1213
import datadog.trace.agent.tooling.Instrumenter;
1314
import datadog.trace.agent.tooling.InstrumenterModule;
1415
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
15-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1616
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
1717
import java.util.Collection;
1818
import java.util.EnumMap;
@@ -69,11 +69,11 @@ public static AgentScope enter() {
6969
return null;
7070
}
7171
// If there is a noop span in the active scope, we can clean all the way to this scope
72-
if (activeSpan() instanceof AgentTracer.NoopAgentSpan) {
72+
if (activeSpan() == noopSpan()) {
7373
return activeScope;
7474
}
7575
// Create an active scope with a noop span, and clean all the way to the previous scope
76-
activateSpan(AgentTracer.NoopAgentSpan.INSTANCE, false);
76+
activateSpan(noopSpan(), false);
7777
return activeScope;
7878
}
7979

dd-java-agent/instrumentation/aws-java-sdk-1.11.0/src/main/java/datadog/trace/instrumentation/aws/v0/AWSHttpClientInstrumentation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
5+
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.noopSpan;
56
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.DECORATE;
67
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.SPAN_CONTEXT_KEY;
78
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -12,7 +13,6 @@
1213
import datadog.trace.agent.tooling.Instrumenter;
1314
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1415
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
15-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1616
import net.bytebuddy.asm.Advice;
1717

1818
/**
@@ -50,7 +50,7 @@ public static void methodExit(
5050
// check name in case TracingRequestHandler failed to activate the span
5151
if (scope != null
5252
&& (AwsNameCache.spanName(request).equals(scope.span().getSpanName())
53-
|| scope.span() instanceof AgentTracer.NoopAgentSpan)) {
53+
|| scope.span() == noopSpan())) {
5454
scope.close();
5555
}
5656

dd-java-agent/instrumentation/aws-java-sdk-1.11.0/src/main/java/datadog/trace/instrumentation/aws/v0/RequestExecutorInstrumentation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
44
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
5+
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.noopSpan;
56
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.DECORATE;
67
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.SPAN_CONTEXT_KEY;
78
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
@@ -10,7 +11,6 @@
1011
import datadog.trace.agent.tooling.Instrumenter;
1112
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1213
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
13-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1414
import net.bytebuddy.asm.Advice;
1515

1616
/**
@@ -47,7 +47,7 @@ public static void methodExit(
4747
// check name in case TracingRequestHandler failed to activate the span
4848
if (scope != null
4949
&& (AwsNameCache.spanName(request).equals(scope.span().getSpanName())
50-
|| scope.span() instanceof AgentTracer.NoopAgentSpan)) {
50+
|| scope.span() == noopSpan())) {
5151
scope.close();
5252
}
5353

dd-java-agent/instrumentation/aws-java-sdk-2.2/src/main/java/datadog/trace/instrumentation/aws/v2/AwsHttpClientInstrumentation.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
77
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
88
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
9-
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
9+
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.noopSpan;
1010
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1111
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
1212
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -15,7 +15,6 @@
1515
import datadog.trace.agent.tooling.Instrumenter;
1616
import datadog.trace.agent.tooling.InstrumenterModule;
1717
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
18-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1918
import net.bytebuddy.asm.Advice;
2019
import net.bytebuddy.description.type.TypeDescription;
2120
import net.bytebuddy.matcher.ElementMatcher;
@@ -76,7 +75,7 @@ public static AgentScope methodEnter(
7675
final AgentScope scope = activeScope();
7776
// check name in case TracingExecutionInterceptor failed to activate the span
7877
if (scope != null
79-
&& (scope.span() instanceof AgentTracer.NoopAgentSpan
78+
&& (scope.span() == noopSpan()
8079
|| AwsSdkClientDecorator.DECORATE
8180
.spanName(requestExecutionContext.executionAttributes())
8281
.equals(scope.span().getSpanName()))) {

dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/NettyChannelHandlerContextInstrumentation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
77
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
8+
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.noopScope;
89
import static datadog.trace.instrumentation.netty40.AttributeKeys.SPAN_ATTRIBUTE_KEY;
910
import static datadog.trace.instrumentation.netty40.NettyChannelPipelineInstrumentation.ADDITIONAL_INSTRUMENTATION_NAMES;
1011
import static datadog.trace.instrumentation.netty40.NettyChannelPipelineInstrumentation.INSTRUMENTATION_NAME;
@@ -16,7 +17,6 @@
1617
import datadog.trace.agent.tooling.InstrumenterModule;
1718
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1819
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
19-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
2020
import datadog.trace.instrumentation.netty40.client.NettyHttpClientDecorator;
2121
import datadog.trace.instrumentation.netty40.server.NettyHttpServerDecorator;
2222
import io.netty.channel.ChannelHandlerContext;
@@ -72,7 +72,7 @@ public static AgentScope scopeSpan(@Advice.This final ChannelHandlerContext ctx)
7272
final AgentSpan channelSpan = ctx.channel().attr(SPAN_ATTRIBUTE_KEY).get();
7373
if (channelSpan == null || channelSpan == activeSpan()) {
7474
// don't modify the scope
75-
return AgentTracer.NoopAgentScope.INSTANCE;
75+
return noopScope();
7676
}
7777
return activateSpan(channelSpan);
7878
}

dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelHandlerContextInstrumentation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
66
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
77
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
8+
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.noopScope;
89
import static datadog.trace.instrumentation.netty41.AttributeKeys.SPAN_ATTRIBUTE_KEY;
910
import static datadog.trace.instrumentation.netty41.NettyChannelPipelineInstrumentation.ADDITIONAL_INSTRUMENTATION_NAMES;
1011
import static datadog.trace.instrumentation.netty41.NettyChannelPipelineInstrumentation.INSTRUMENTATION_NAME;
@@ -16,7 +17,6 @@
1617
import datadog.trace.agent.tooling.InstrumenterModule;
1718
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1819
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
19-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
2020
import datadog.trace.instrumentation.netty41.client.NettyHttpClientDecorator;
2121
import datadog.trace.instrumentation.netty41.server.NettyHttpServerDecorator;
2222
import io.netty.channel.ChannelHandlerContext;
@@ -72,7 +72,7 @@ public static AgentScope scopeSpan(@Advice.This final ChannelHandlerContext ctx)
7272
final AgentSpan channelSpan = ctx.channel().attr(SPAN_ATTRIBUTE_KEY).get();
7373
if (channelSpan == null || channelSpan == activeSpan()) {
7474
// don't modify the scope
75-
return AgentTracer.NoopAgentScope.INSTANCE;
75+
return noopScope();
7676
}
7777
return activateSpan(channelSpan);
7878
}

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package datadog.trace.instrumentation.opentelemetry;
22

3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
4+
35
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
46
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
57
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
6-
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
78
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
89
import io.opentelemetry.context.Scope;
910
import io.opentelemetry.trace.Span;
@@ -16,16 +17,16 @@ public class TypeConverter {
1617
private final OtelScope noopScopeWrapper;
1718

1819
public TypeConverter() {
19-
noopSpanWrapper = new OtelSpan(AgentTracer.NoopAgentSpan.INSTANCE, this);
20-
noopContextWrapper = new OtelSpanContext(AgentTracer.NoopContext.INSTANCE);
21-
noopScopeWrapper = new OtelScope(AgentTracer.NoopAgentScope.INSTANCE);
20+
noopSpanWrapper = new OtelSpan(noopSpan(), this);
21+
noopContextWrapper = new OtelSpanContext(AgentSpanContext.noop());
22+
noopScopeWrapper = new OtelScope(AgentScope.noop());
2223
}
2324

2425
public AgentSpan toAgentSpan(final Span span) {
2526
if (span instanceof OtelSpan) {
2627
return ((OtelSpan) span).asAgentSpan();
2728
}
28-
return null == span ? null : AgentTracer.NoopAgentSpan.INSTANCE;
29+
return null == span ? null : noopSpan();
2930
}
3031

3132
public Span toSpan(final AgentSpan agentSpan) {
@@ -42,7 +43,7 @@ public Span toSpan(final AgentSpan agentSpan) {
4243
attachableSpanWrapper.attachWrapper(spanWrapper);
4344
return spanWrapper;
4445
}
45-
if (agentSpan == AgentTracer.NoopAgentSpan.INSTANCE) {
46+
if (agentSpan == noopSpan()) {
4647
return noopSpanWrapper;
4748
}
4849
return new OtelSpan(agentSpan, this);
@@ -62,7 +63,7 @@ public Scope toScope(final AgentScope scope) {
6263
attachableScopeWrapper.attachWrapper(otScope);
6364
return otScope;
6465
}
65-
if (scope == AgentTracer.NoopAgentScope.INSTANCE) {
66+
if (scope == AgentScope.noop()) {
6667
return noopScopeWrapper;
6768
}
6869
return new OtelScope(scope);
@@ -73,7 +74,7 @@ public SpanContext toSpanContext(final AgentSpanContext context) {
7374
return null;
7475
}
7576
// avoid a new SpanContext wrapper allocation for the noop context
76-
if (context == AgentTracer.NoopContext.INSTANCE) {
77+
if (context == AgentSpanContext.noop()) {
7778
return noopContextWrapper;
7879
}
7980
return new OtelSpanContext(context);
@@ -83,6 +84,6 @@ public AgentSpanContext toContext(final SpanContext spanContext) {
8384
if (spanContext instanceof OtelSpanContext) {
8485
return ((OtelSpanContext) spanContext).getDelegate();
8586
}
86-
return null == spanContext ? null : AgentTracer.NoopContext.INSTANCE;
87+
return null == spanContext ? null : AgentSpanContext.noop();
8788
}
8889
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-0.3/src/test/groovy/TypeConverterTest.groovy

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import datadog.trace.agent.test.AgentTestRunner
22
import datadog.trace.api.DDSpanId
33
import datadog.trace.api.DDTraceId
44
import datadog.trace.api.sampling.PrioritySampling
5+
import datadog.trace.bootstrap.instrumentation.api.AgentScope
6+
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext
57
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
68
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopPathwayContext
79
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
@@ -16,7 +18,7 @@ class TypeConverterTest extends AgentTestRunner {
1618
TypeConverter typeConverter = new TypeConverter()
1719

1820
def "should avoid the noop span wrapper allocation"() {
19-
def noopAgentSpan = AgentTracer.NoopAgentSpan.INSTANCE
21+
def noopAgentSpan = AgentTracer.noopSpan()
2022
expect:
2123
typeConverter.toSpan(noopAgentSpan) is typeConverter.toSpan(noopAgentSpan)
2224
}
@@ -33,13 +35,13 @@ class TypeConverterTest extends AgentTestRunner {
3335
}
3436

3537
def "should avoid the noop context wrapper allocation"() {
36-
def noopContext = AgentTracer.NoopContext.INSTANCE
38+
def noopContext = AgentSpanContext.noop()
3739
expect:
3840
typeConverter.toSpanContext(noopContext) is typeConverter.toSpanContext(noopContext)
3941
}
4042

4143
def "should avoid the noop scope wrapper allocation"() {
42-
def noopScope = AgentTracer.NoopAgentScope.INSTANCE
44+
def noopScope = AgentScope.noop()
4345
expect:
4446
typeConverter.toScope(noopScope) is typeConverter.toScope(noopScope)
4547
}

0 commit comments

Comments
 (0)