Skip to content

Commit

Permalink
Fold setAsyncPropagation into preceding activateSpan (#8032)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls authored Nov 28, 2024
1 parent d949e28 commit 1ef67e5
Show file tree
Hide file tree
Showing 65 changed files with 82 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ public TestImpl(

span = spanBuilder.start();

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
activateSpan(span, true);

span.setSpanType(InternalSpanTypes.TEST);
span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_TEST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public TestSuiteImpl(
testDecorator.afterStart(span);

if (!parallelized) {
final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
activateSpan(span, true);
}

metricCollector.add(CiVisibilityCountMetric.EVENT_CREATED, 1, instrumentation, EventType.SUITE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ protected Integer compute() {
} else {
int next = parent + 1;
AgentSpan span = startSpan(Integer.toString(next));
try (AgentScope scope = activateSpan(span)) {
// shouldn't be necessary with a decent API
scope.setAsyncPropagation(true);
try (AgentScope scope = activateSpan(span, true)) {
LinearTask child = new LinearTask(next, depth);
return child.fork().join();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public static AgentScope createSpan(final HttpRequest request) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, request, request, extractedContext);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
return scope;
return activateSpan(span, true);
}

public static void finishSpan(final AgentSpan span, final HttpResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ public static AgentScope nameSpan(
DECORATE.afterStart(span);
span.setResourceName(DECORATE.className(clazz));

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
return scope;
return activateSpan(span, true);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public static class HandleAdvice {
DECORATE.afterStart(span);
DECORATE.onRequest(span, request, request, parentContext);

scope = activateSpan(span);
scope.setAsyncPropagation(true);
scope = activateSpan(span, true);

request.setAttribute(DD_SPAN_ATTRIBUTE, span);
request.setAttribute(CorrelationIdentifier.getTraceIdKey(), GlobalTracer.get().getTraceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,22 @@
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;
import org.glassfish.grizzly.filterchain.BaseFilter;
import org.glassfish.grizzly.filterchain.FilterChainContext;

public class FilterAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope onEnter(
@Advice.This BaseFilter it, @Advice.Argument(0) final FilterChainContext ctx) {
if (ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE) == null || activeSpan() != null) {
public static AgentScope onEnter(@Advice.Argument(0) final FilterChainContext ctx) {
Object span = ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE);
if (span == null || activeSpan() != null) {
return null;
}
AgentScope scope =
activateSpan((AgentSpan) ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE));
scope.setAsyncPropagation(true);
return scope;
return activateSpan((AgentSpan) span, true);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.This BaseFilter it, @Advice.Enter final AgentScope scope) {
public static void onExit(@Advice.Enter final AgentScope scope) {
if (scope != null) {
scope.setAsyncPropagation(false);
scope.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public static NextAction onHttpCodecFilterExit(
HttpResponsePacket httpResponse = httpRequest.getResponse();
AgentSpan.Context.Extracted context = DECORATE.extract(httpRequest);
AgentSpan span = DECORATE.startSpan(httpRequest, context);
AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
AgentScope scope = activateSpan(span, true);
DECORATE.afterStart(span);
ctx.getAttributes().setAttribute(DD_SPAN_ATTRIBUTE, span);
ctx.getAttributes().setAttribute(DD_RESPONSE_ATTRIBUTE, httpResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ public static AgentScope onEnter(
span, InstrumentationContext.get(IgniteCache.class, Ignite.class).get(that));
DECORATE.onOperation(span, that.getName(), methodName);

final AgentScope scope = activateSpan(span);
// Enable async propagation, so the newly spawned task will be associated back with this
// original trace.
scope.setAsyncPropagation(true);
return scope;
return activateSpan(span, true);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down Expand Up @@ -149,11 +147,9 @@ public static AgentScope onEnter(
span, InstrumentationContext.get(IgniteCache.class, Ignite.class).get(that));
DECORATE.onOperation(span, that.getName(), methodName, key);

final AgentScope scope = activateSpan(span);
// Enable async propagation, so the newly spawned task will be associated back with this
// original trace.
scope.setAsyncPropagation(true);
return scope;
return activateSpan(span, true);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public static AgentScope createGenericSpan(@Advice.This final ContainerRequestCo
// can only be aborted inside the filter method
}

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);

DECORATE.afterStart(span);
DECORATE.onJakartaRsSpan(span, parent, filterClass, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ public static AgentScope nameSpan(
DECORATE.onJakartaRsSpan(span, parent, target.getClass(), method);
DECORATE.afterStart(span);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);

if (contextStore != null && asyncResponse != null) {
contextStore.put(asyncResponse, span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static AgentScope createOrUpdateAbortSpan(
parent = activeSpan();
span = startSpan(JAKARTA_RS_REQUEST_ABORT);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);

DECORATE.afterStart(span);
DECORATE.onJakartaRsSpan(span, parent, resourceClass, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public void run() {
return;
}
AgentSpan span = startSpan(String.valueOf(depth));
try (AgentScope scope = activateSpan(span)) {
scope.setAsyncPropagation(true);
try (AgentScope scope = activateSpan(span, true)) {
executor.execute(new RecursiveThreadPoolExecution(executor, maxDepth, depth + 1));
} finally {
span.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public void run() {
return;
}
AgentSpan span = startSpan(String.valueOf(depth));
try (AgentScope scope = activateSpan(span)) {
scope.setAsyncPropagation(true);
try (AgentScope scope = activateSpan(span, true)) {
if (depth % 2 == 0) {
executor.submit(
new RecursiveThreadPoolMixedSubmissionAndExecution(executor, maxDepth, depth + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public void run() {
return;
}
AgentSpan span = startSpan(String.valueOf(depth));
try (AgentScope scope = activateSpan(span)) {
scope.setAsyncPropagation(true);
try (AgentScope scope = activateSpan(span, true)) {
executor.submit(new RecursiveThreadPoolSubmission(executor, maxDepth, depth + 1));
} finally {
span.finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ protected Integer compute() {
} else {
int next = parent + 1;
AgentSpan span = startSpan(Integer.toString(next));
try (AgentScope scope = activateSpan(span)) {
// shouldn't be necessary with a decent API
scope.setAsyncPropagation(true);
try (AgentScope scope = activateSpan(span, true)) {
LinearTask child = new LinearTask(next, depth);
return child.fork().join();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ public static AgentScope nameSpan(
DECORATE.onJaxRsSpan(span, parent, target.getClass(), method);
DECORATE.afterStart(span);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
return scope;
return activateSpan(span, true);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public static AgentScope createGenericSpan(@Advice.This final ContainerRequestCo
// can only be aborted inside the filter method
}

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);

DECORATE.afterStart(span);
DECORATE.onJaxRsSpan(span, parent, filterClass, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ public static AgentScope nameSpan(
DECORATE.onJaxRsSpan(span, parent, target.getClass(), method);
DECORATE.afterStart(span);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);

if (contextStore != null && asyncResponse != null) {
contextStore.put(asyncResponse, span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static AgentScope createOrUpdateAbortSpan(
parent = activeSpan();
span = startSpan(JAX_RS_REQUEST_ABORT);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);

DECORATE.afterStart(span);
DECORATE.onJaxRsSpan(span, parent, resourceClass, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public static AgentScope start(@Advice.This final DataSource ds) {

span.setResourceName(DECORATE.spanNameForMethod(ds.getClass(), "getConnection"));

final AgentScope agentScope = activateSpan(span);
agentScope.setAsyncPropagation(true);
return agentScope;
return activateSpan(span, true);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static AgentScope onEnter(

final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(req);
span = DECORATE.startSpan(req, extractedContext);
final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);
span.setMeasured(true);
DECORATE.afterStart(span);
DECORATE.onRequest(span, req, req, extractedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public static void onExit(

final AgentSpan.Context.Extracted extractedContext = JettyDecorator.DECORATE.extract(req);
final AgentSpan span = JettyDecorator.DECORATE.startSpan(req, extractedContext);
try (final AgentScope scope = activateSpan(span)) {
scope.setAsyncPropagation(true);
try (final AgentScope scope = activateSpan(span, true)) {
span.setMeasured(true);
JettyDecorator.DECORATE.afterStart(span);
JettyDecorator.DECORATE.onRequest(span, req, req, extractedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public static AgentScope onEnter(

final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(req);
span = DECORATE.startSpan(req, extractedContext);
final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);
DECORATE.afterStart(span);
DECORATE.onRequest(span, req, req, extractedContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ public static AgentScope onEnter(

final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(req);
span = DECORATE.startSpan(req, extractedContext);
final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);
DECORATE.afterStart(span);
DECORATE.onRequest(span, req, req, extractedContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ public static AgentScope onEnter(

final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(req);
span = DECORATE.startSpan(req, extractedContext);
final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);
DECORATE.afterStart(span);
DECORATE.onRequest(span, req, req, extractedContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public static AgentScope onEnter(
DECORATE.afterStart(span);
DECORATE.onRequest(span, req, req, extractedContext);

final AgentScope scope = activateSpan(span);
scope.setAsyncPropagation(true);
final AgentScope scope = activateSpan(span, true);
req.setAttribute(DD_SPAN_ATTRIBUTE, span);
req.setAttribute(CorrelationIdentifier.getTraceIdKey(), GlobalTracer.get().getTraceId());
req.setAttribute(CorrelationIdentifier.getSpanIdKey(), GlobalTracer.get().getSpanId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public void onCompletion(final RecordMetadata metadata, final Exception exceptio
span.finish();
if (callback != null) {
if (parent != null) {
try (final AgentScope scope = activateSpan(parent)) {
scope.setAsyncPropagation(true);
try (final AgentScope scope = activateSpan(parent, true)) {
callback.onCompletion(metadata, exception);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void onCompletion(final RecordMetadata metadata, final Exception exceptio
span.finish();
if (callback != null) {
if (parent != null) {
try (final AgentScope scope = activateSpan(parent)) {
scope.setAsyncPropagation(true);
try (final AgentScope scope = activateSpan(parent, true)) {
callback.onCompletion(metadata, exception);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public static class HandleRequestAdvice {
final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(request);
request.setAttribute(DD_EXTRACTED_CONTEXT_ATTRIBUTE, extractedContext);
final AgentSpan span = DECORATE.startSpan(request, extractedContext);
scope = activateSpan(span);
scope.setAsyncPropagation(true);
scope = activateSpan(span, true);

DECORATE.afterStart(span);
DECORATE.onRequest(span, request, request, extractedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public static class HandleRequestAdvice {
final AgentSpan.Context.Extracted extractedContext = DECORATE.extract(request);
request.setAttribute(DD_EXTRACTED_CONTEXT_ATTRIBUTE, extractedContext);
final AgentSpan span = DECORATE.startSpan(request, extractedContext);
scope = activateSpan(span);
scope.setAsyncPropagation(true);
scope = activateSpan(span, true);

DECORATE.afterStart(span);
DECORATE.onRequest(span, request, request, extractedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent
}

// We want the callback in the scope of the parent, not the client span
try (final AgentScope scope = activateSpan(parent)) {
scope.setAsyncPropagation(true);
try (final AgentScope scope = activateSpan(parent, true)) {
ctx.sendUpstream(msg);
}
}
Expand Down Expand Up @@ -77,8 +76,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws
}

// We want the callback in the scope of the parent, not the client span
try (final AgentScope scope = activateSpan(parent)) {
scope.setAsyncPropagation(true);
try (final AgentScope scope = activateSpan(parent, true)) {
super.exceptionCaught(ctx, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent
if (span == null) {
ctx.sendUpstream(msg); // superclass does not throw
} else {
try (final AgentScope scope = activateSpan(span)) {
scope.setAsyncPropagation(true);
try (final AgentScope scope = activateSpan(span, true)) {
ctx.sendUpstream(msg); // superclass does not throw
}
}
Expand All @@ -51,12 +50,10 @@ public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent
channelTraceContext.reset();
channelTraceContext.setRequestHeaders(headers);

try (final AgentScope scope = activateSpan(span)) {
try (final AgentScope scope = activateSpan(span, true)) {
DECORATE.afterStart(span);
DECORATE.onRequest(span, ctx.getChannel(), request, context);

scope.setAsyncPropagation(true);

channelTraceContext.setServerSpan(span);

Flow.Action.RequestBlockingAction rba = span.getRequestBlockingAction();
Expand Down
Loading

0 comments on commit 1ef67e5

Please sign in to comment.