|
1 | 1 | package datadog.trace.instrumentation.grizzlyhttp232; |
2 | 2 |
|
3 | 3 | 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; |
4 | 7 | import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
5 | 8 | import static net.bytebuddy.matcher.ElementMatchers.isPrivate; |
6 | 9 | import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
|
9 | 12 | import datadog.trace.agent.tooling.Instrumenter; |
10 | 13 | import datadog.trace.agent.tooling.InstrumenterModule; |
11 | 14 | import datadog.trace.api.InstrumenterConfig; |
| 15 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 16 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
12 | 17 | import java.util.Collections; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import org.glassfish.grizzly.filterchain.FilterChainContext; |
13 | 20 |
|
14 | 21 | @AutoService(InstrumenterModule.class) |
15 | 22 | public class DefaultFilterChainInstrumentation extends InstrumenterModule.Tracing |
@@ -51,5 +58,34 @@ public void methodAdvice(MethodTransformer transformer) { |
51 | 58 | .and(takesArgument(0, named("org.glassfish.grizzly.filterchain.FilterChainContext"))) |
52 | 59 | .and(takesArgument(1, named("java.lang.Throwable"))), |
53 | 60 | 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 | + } |
54 | 90 | } |
55 | 91 | } |
0 commit comments