55
66package io .opentelemetry .javaagent .instrumentation .undertow ;
77
8+ import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .extendsClass ;
9+ import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .hasClassesNamed ;
810import static io .opentelemetry .javaagent .instrumentation .undertow .UndertowSingletons .helper ;
911import static net .bytebuddy .matcher .ElementMatchers .named ;
10- import static net .bytebuddy .matcher .ElementMatchers .namedOneOf ;
1112import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
1213
1314import io .opentelemetry .context .Context ;
15+ import io .opentelemetry .javaagent .bootstrap .CallDepth ;
1416import io .opentelemetry .javaagent .bootstrap .http .HttpServerResponseCustomizerHolder ;
1517import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1618import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
1719import io .undertow .server .HttpServerExchange ;
20+ import io .undertow .server .ServerConnection ;
1821import net .bytebuddy .asm .Advice ;
1922import net .bytebuddy .description .type .TypeDescription ;
2023import net .bytebuddy .matcher .ElementMatcher ;
2124
2225public class HttpServerConnectionInstrumentation implements TypeInstrumentation {
26+
27+ @ Override
28+ public ElementMatcher <ClassLoader > classLoaderOptimization () {
29+ return hasClassesNamed ("io.undertow.server.ServerConnection" );
30+ }
31+
2332 @ Override
2433 public ElementMatcher <TypeDescription > typeMatcher () {
25- return namedOneOf (
26- "io.undertow.server.protocol.http.HttpServerConnection" ,
27- "io.undertow.server.protocol.http2.Http2ServerConnection" );
34+ return extendsClass (named ("io.undertow.server.ServerConnection" ));
2835 }
2936
3037 @ Override
@@ -39,10 +46,22 @@ public void transform(TypeTransformer transformer) {
3946 public static class ResponseAdvice {
4047
4148 @ Advice .OnMethodEnter (suppress = Throwable .class )
42- public static void onEnter (@ Advice .Argument (0 ) HttpServerExchange exchange ) {
49+ public static void onEnter (
50+ @ Advice .Argument (0 ) HttpServerExchange exchange ,
51+ @ Advice .Local ("otelCallDepth" ) CallDepth callDepth ) {
52+ callDepth = CallDepth .forClass (ServerConnection .class );
53+ if (callDepth .getAndIncrement () > 0 ) {
54+ return ;
55+ }
56+
4357 Context context = helper ().getServerContext (exchange );
4458 HttpServerResponseCustomizerHolder .getCustomizer ()
4559 .customize (context , exchange , UndertowHttpResponseMutator .INSTANCE );
4660 }
61+
62+ @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
63+ public static void onExit (@ Advice .Local ("otelCallDepth" ) CallDepth callDepth ) {
64+ callDepth .decrementAndGet ();
65+ }
4766 }
4867}
0 commit comments