8000 Propagate grizzly http span in filters if nothing is active by amarziali · Pull Request #9016 · DataDog/dd-trace-java · GitHub
[go: up one dir, main page]

Skip to content

Propagate grizzly http span in filters if nothing is active #9016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package datadog.trace.instrumentation.grizzlyhttp232;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan;
import static datadog.trace.bootstrap.instrumentation.decorator.HttpServerDecorator.DD_SPAN_ATTRIBUTE;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPrivate;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
Expand All @@ -9,7 +12,11 @@
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import java.util.Collections;
import net.bytebuddy.asm.Advice;
import org.glassfish.grizzly.filterchain.FilterChainContext;

@AutoService(InstrumenterModule.class)
public class DefaultFilterChainInstrumentation extends InstrumenterModule.Tracing
Expand Down Expand Up @@ -51,5 +58,34 @@ public void methodAdvice(MethodTransformer transformer) {
.and(takesArgument(0, named("org.glassfish.grizzly.filterchain.FilterChainContext")))
.and(takesArgument(1, named("java.lang.Throwable"))),
packageName + ".DefaultFilterChainAdvice");
transformer.applyAdvice(
isMethod()
.and(named("executeFilter"))
.and(takesArgument(2, named("org.glassfish.grizzly.filterchain.FilterChainContext"))),
getClass().getName() + "$PropagateServerSpanAdvice");
}

public static class PropagateServerSpanAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope onEnter(@Advice.Argument(2) final FilterChainContext ctx) {
final AgentSpan active = activeSpan();
// don't activate a span if already one is active
if (active != null) {
return null;
}
final Object span = ctx.getAttributes().getAttribute(DD_SPAN_ATTRIBUTE);
if (span instanceof AgentSpan) {
// activate the http server span when nothing is already active
return activateSpan((AgentSpan) span);
}
return null;
}

@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onExit(@Advice.Enter final AgentScope scope) {
if (scope != null) {
scope.close();
}
}
}
}
0