Skip to content

Commit 93244c3

Browse files
authored
Propagate grizzly http span in filters if nothing is active (#9016)
1 parent fb63569 commit 93244c3

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

dd-java-agent/instrumentation/grizzly-http-2.3.20/src/main/java/datadog/trace/instrumentation/grizzlyhttp232/DefaultFilterChainInstrumentation.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package datadog.trace.instrumentation.grizzlyhttp232;
22

33
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
4+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
5+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
6+
import static datadog.trace.bootstrap.instrumentation.decorator.HttpServerDecorator.DD_SPAN_ATTRIBUTE;
47
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
58
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
69
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -9,7 +12,11 @@
912
import datadog.trace.agent.tooling.Instrumenter;
1013
import datadog.trace.agent.tooling.InstrumenterModule;
1114
import datadog.trace.api.InstrumenterConfig;
15+
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
16+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1217
import java.util.Collections;
18+
import net.bytebuddy.asm.Advice;
19+
import org.glassfish.grizzly.filterchain.FilterChainContext;
1320

1421
@AutoService(InstrumenterModule.class)
1522
public class DefaultFilterChainInstrumentation extends InstrumenterModule.Tracing
@@ -51,5 +58,34 @@ public void methodAdvice(MethodTransformer transformer) {
5158
.and(takesArgument(0, named("org.glassfish.grizzly.filterchain.FilterChainContext")))
5259
.and(takesArgument(1, named("java.lang.Throwable"))),
5360
packageName + ".DefaultFilterChainAdvice");
61+
transformer.applyAdvice(
62+
isMethod()
63+
.and(named("executeFilter"))
64+
.and(takesArgument(2, named("org.glassfish.grizzly.filterchain.FilterChainContext"))),
65+
getClass().getName() + "$PropagateServerSpanAdvice");
66+
}
67+
68+
public static class PropagateServerSpanAdvice {
69+
@Advice.OnMethodEnter(suppress = Throwable.class)
70+
public static AgentScope onEnter(@Advice.Argument(2) final FilterChainContext ctx) {
71+
final AgentSpan active = activeSpan();
72+
// don't activate a span if already one is active
73+
if (active != null) {
74+
return null;
75+
}
76+
final Object span = ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE);
77+
if (span instanceof AgentSpan) {
78+
// activate the http server span when nothing is already active
79+
return activateSpan((AgentSpan) span);
80+
}
81+
return null;
82+
}
83+
84+
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
85+
public static void onExit(@Advice.Enter final AgentScope scope) {
86+
if (scope != null) {
87+
scope.close();
88+
}
89+
}
5490
}
5591
}

0 commit comments

Comments
 (0)