Skip to content

Commit

Permalink
feat(core): Extract span and scope implementations from tracer class
Browse files Browse the repository at this point in the history
Remove unused eligibleForDropping method
  • Loading branch information
PerfectSlayer committed Jan 31, 2025
1 parent f677ee0 commit 91d8276
Show file tree
Hide file tree
Showing 37 changed files with 892 additions and 744 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package datadog.trace.bootstrap.instrumentation.java.concurrent;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.bootstrap.instrumentation.java.concurrent.ContinuationClaim.CLAIMED;

import datadog.trace.bootstrap.ContextStore;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -32,7 +32,7 @@ private ConcurrentState() {}
public static <K> ConcurrentState captureScope(
ContextStore<K, ConcurrentState> contextStore, K key, AgentScope scope) {
if (scope != null && scope.isAsyncPropagating()) {
if (scope.span() instanceof AgentTracer.NoopAgentSpan) {
if (scope.span() == noopSpan()) {
return null;
}
final ConcurrentState state = contextStore.putIfAbsent(key, FACTORY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

Expand All @@ -13,7 +14,6 @@
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.InstrumentationContext;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils;
import datadog.trace.bootstrap.instrumentation.java.concurrent.State;
import java.util.Map;
Expand Down Expand Up @@ -72,11 +72,11 @@ public static AgentScope enter(
return null;
}
// If there is a noop span in the active scope, we can clean all the way to this scope
if (activeSpan() instanceof AgentTracer.NoopAgentSpan) {
if (activeSpan() == noopSpan()) {
return activeScope;
}
// Create an active scope with a noop span, and clean all the way to the previous scope
localScope = activateSpan(AgentTracer.NoopAgentSpan.INSTANCE, false);
localScope = activateSpan(noopSpan(), false);
return activeScope;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static java.util.Collections.singletonList;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

Expand All @@ -12,7 +13,6 @@
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
import java.util.Collection;
import java.util.EnumMap;
Expand Down Expand Up @@ -69,11 +69,11 @@ public static AgentScope enter() {
return null;
}
// If there is a noop span in the active scope, we can clean all the way to this scope
if (activeSpan() instanceof AgentTracer.NoopAgentSpan) {
if (activeSpan() == noopSpan()) {
return activeScope;
}
// Create an active scope with a noop span, and clean all the way to the previous scope
activateSpan(AgentTracer.NoopAgentSpan.INSTANCE, false);
activateSpan(noopSpan(), false);
return activeScope;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.DECORATE;
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.SPAN_CONTEXT_KEY;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -12,7 +13,6 @@
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import net.bytebuddy.asm.Advice;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeScope;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.DECORATE;
import static datadog.trace.instrumentation.aws.v0.OnErrorDecorator.SPAN_CONTEXT_KEY;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
Expand All @@ -10,7 +11,6 @@
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import net.bytebuddy.asm.Advice;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -76,7 +75,7 @@ public static AgentScope methodEnter(
final AgentScope scope = activeScope();
// check name in case TracingExecutionInterceptor failed to activate the span
if (scope != null
&& (scope.span() instanceof AgentTracer.NoopAgentSpan
&& (scope.span() == noopSpan()
|| AwsSdkClientDecorator.DECORATE
.spanName(requestExecutionContext.executionAttributes())
.equals(scope.span().getSpanName()))) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package datadog.trace.instrumentation.opentelemetry;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;

import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
Expand All @@ -16,16 +17,16 @@ public class TypeConverter {
private final OtelScope noopScopeWrapper;

public TypeConverter() {
noopSpanWrapper = new OtelSpan(AgentTracer.NoopAgentSpan.INSTANCE, this);
noopContextWrapper = new OtelSpanContext(AgentTracer.NoopContext.INSTANCE);
noopScopeWrapper = new OtelScope(AgentTracer.NoopAgentScope.INSTANCE);
noopSpanWrapper = new OtelSpan(noopSpan(), this);
noopContextWrapper = new OtelSpanContext(AgentSpanContext.noop());
noopScopeWrapper = new OtelScope(AgentScope.noop());
}

public AgentSpan toAgentSpan(final Span span) {
if (span instanceof OtelSpan) {
return ((OtelSpan) span).asAgentSpan();
}
return null == span ? null : AgentTracer.NoopAgentSpan.INSTANCE;
return null == span ? null : noopSpan();
}

public Span toSpan(final AgentSpan agentSpan) {
Expand All @@ -42,7 +43,7 @@ public Span toSpan(final AgentSpan agentSpan) {
attachableSpanWrapper.attachWrapper(spanWrapper);
return spanWrapper;
}
if (agentSpan == AgentTracer.NoopAgentSpan.INSTANCE) {
if (agentSpan == noopSpan()) {
return noopSpanWrapper;
}
return new OtelSpan(agentSpan, this);
Expand All @@ -62,7 +63,7 @@ public Scope toScope(final AgentScope scope) {
attachableScopeWrapper.attachWrapper(otScope);
return otScope;
}
if (scope == AgentTracer.NoopAgentScope.INSTANCE) {
if (scope == AgentScope.noop()) {
return noopScopeWrapper;
}
return new OtelScope(scope);
Expand All @@ -73,7 +74,7 @@ public SpanContext toSpanContext(final AgentSpanContext context) {
return null;
}
// avoid a new SpanContext wrapper allocation for the noop context
if (context == AgentTracer.NoopContext.INSTANCE) {
if (context == AgentSpanContext.noop()) {
return noopContextWrapper;
}
return new OtelSpanContext(context);
Expand All @@ -83,6 +84,6 @@ public AgentSpanContext toContext(final SpanContext spanContext) {
if (spanContext instanceof OtelSpanContext) {
return ((OtelSpanContext) spanContext).getDelegate();
}
return null == spanContext ? null : AgentTracer.NoopContext.INSTANCE;
return null == spanContext ? null : AgentSpanContext.noop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTraceId
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.bootstrap.instrumentation.api.AgentScope
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopPathwayContext
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
Expand All @@ -16,7 +18,7 @@ class TypeConverterTest extends AgentTestRunner {
TypeConverter typeConverter = new TypeConverter()

def "should avoid the noop span wrapper allocation"() {
def noopAgentSpan = AgentTracer.NoopAgentSpan.INSTANCE
def noopAgentSpan = AgentTracer.noopSpan()
expect:
typeConverter.toSpan(noopAgentSpan) is typeConverter.toSpan(noopAgentSpan)
}
Expand All @@ -33,13 +35,13 @@ class TypeConverterTest extends AgentTestRunner {
}

def "should avoid the noop context wrapper allocation"() {
def noopContext = AgentTracer.NoopContext.INSTANCE
def noopContext = AgentSpanContext.noop()
expect:
typeConverter.toSpanContext(noopContext) is typeConverter.toSpanContext(noopContext)
}

def "should avoid the noop scope wrapper allocation"() {
def noopScope = AgentTracer.NoopAgentScope.INSTANCE
def noopScope = AgentScope.noop()
expect:
typeConverter.toScope(noopScope) is typeConverter.toScope(noopScope)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package datadog.trace.instrumentation.opentracing31;

import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan;

import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.AttachableWrapper;
import datadog.trace.instrumentation.opentracing.LogHandler;
import io.opentracing.Scope;
Expand All @@ -19,16 +20,16 @@ public class TypeConverter {

public TypeConverter(final LogHandler logHandler) {
this.logHandler = logHandler;
noopSpanWrapper = new OTSpan(AgentTracer.NoopAgentSpan.INSTANCE, this, logHandler);
noopContextWrapper = new OTSpanContext(AgentTracer.NoopContext.INSTANCE);
noopScopeWrapper = new OTScopeManager.OTScope(AgentTracer.NoopAgentScope.INSTANCE, false, this);
noopSpanWrapper = new OTSpan(noopSpan(), this, logHandler);
noopContextWrapper = new OTSpanContext(AgentSpanContext.noop());
noopScopeWrapper = new OTScopeManager.OTScope(AgentScope.noop(), false, this);
}

public AgentSpan toAgentSpan(final Span span) {
if (span instanceof OTSpan) {
return ((OTSpan) span).asAgentSpan();
}
return null == span ? null : AgentTracer.NoopAgentSpan.INSTANCE;
return null == span ? null : noopSpan();
}

public OTSpan toSpan(final AgentSpan agentSpan) {
Expand All @@ -45,7 +46,7 @@ public OTSpan toSpan(final AgentSpan agentSpan) {
attachableSpanWrapper.attachWrapper(spanWrapper);
return spanWrapper;
}
if (agentSpan == AgentTracer.NoopAgentSpan.INSTANCE) {
if (agentSpan == noopSpan()) {
return noopSpanWrapper;
}
return new OTSpan(agentSpan, this, logHandler);
Expand All @@ -68,7 +69,7 @@ public Scope toScope(final AgentScope scope, final boolean finishSpanOnClose) {
attachableScopeWrapper.attachWrapper(otScope);
return otScope;
}
if (scope == AgentTracer.NoopAgentScope.INSTANCE) {
if (scope == AgentScope.noop()) {
return noopScopeWrapper;
}
return new OTScopeManager.OTScope(scope, finishSpanOnClose, this);
Expand All @@ -79,7 +80,7 @@ public SpanContext toSpanContext(final AgentSpanContext context) {
return null;
}
// avoid a new SpanContext wrapper allocation for the noop context
if (context == AgentTracer.NoopContext.INSTANCE) {
if (context == AgentSpanContext.noop()) {
return noopContextWrapper;
}
return new OTSpanContext(context);
Expand All @@ -89,6 +90,6 @@ public AgentSpanContext toContext(final SpanContext spanContext) {
if (spanContext instanceof OTSpanContext) {
return ((OTSpanContext) spanContext).getDelegate();
}
return null == spanContext ? null : AgentTracer.NoopContext.INSTANCE;
return null == spanContext ? null : AgentSpanContext.noop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.DDSpanId
import datadog.trace.api.DDTraceId
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext
import datadog.trace.bootstrap.instrumentation.api.AgentTracer
import datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopPathwayContext
import datadog.trace.bootstrap.instrumentation.api.ScopeSource
Expand All @@ -17,7 +18,7 @@ class TypeConverterTest extends AgentTestRunner {
TypeConverter typeConverter = new TypeConverter(new DefaultLogHandler())

def "should avoid the noop span wrapper allocation"() {
def noopAgentSpan = AgentTracer.NoopAgentSpan.INSTANCE
def noopAgentSpan = AgentTracer.noopSpan()
expect:
typeConverter.toSpan(noopAgentSpan) is typeConverter.toSpan(noopAgentSpan)
}
Expand All @@ -34,7 +35,7 @@ class TypeConverterTest extends AgentTestRunner {
}

def "should avoid the noop context wrapper allocation"() {
def noopContext = AgentTracer.NoopContext.INSTANCE
def noopContext = AgentSpanContext.noop()
expect:
typeConverter.toSpanContext(noopContext) is typeConverter.toSpanContext(noopContext)
}
Expand Down
Loading

0 comments on commit 91d8276

Please sign in to comment.