5
5
6
6
package io .opentelemetry .javaagent .instrumentation .undertow ;
7
7
8
+ import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .extendsClass ;
9
+ import static io .opentelemetry .javaagent .extension .matcher .AgentElementMatchers .hasClassesNamed ;
8
10
import static io .opentelemetry .javaagent .instrumentation .undertow .UndertowSingletons .helper ;
9
11
import static net .bytebuddy .matcher .ElementMatchers .named ;
10
- import static net .bytebuddy .matcher .ElementMatchers .namedOneOf ;
11
12
import static net .bytebuddy .matcher .ElementMatchers .takesArgument ;
12
13
13
14
import io .opentelemetry .context .Context ;
15
+ import io .opentelemetry .javaagent .bootstrap .CallDepth ;
14
16
import io .opentelemetry .javaagent .bootstrap .http .HttpServerResponseCustomizerHolder ;
15
17
import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
16
18
import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
17
19
import io .undertow .server .HttpServerExchange ;
20
+ import io .undertow .server .ServerConnection ;
18
21
import net .bytebuddy .asm .Advice ;
19
22
import net .bytebuddy .description .type .TypeDescription ;
20
23
import net .bytebuddy .matcher .ElementMatcher ;
21
24
22
25
public class HttpServerConnectionInstrumentation implements TypeInstrumentation {
26
+
27
+ @ Override
28
+ public ElementMatcher <ClassLoader > classLoaderOptimization () {
29
+ return hasClassesNamed ("io.undertow.server.ServerConnection" );
30
+ }
31
+
23
32
@ Override
24
33
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" ));
28
35
}
29
36
30
37
@ Override
@@ -39,10 +46,22 @@ public void transform(TypeTransformer transformer) {
39
46
public static class ResponseAdvice {
40
47
41
48
@ 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
+
43
57
Context context = helper ().getServerContext (exchange );
44
58
HttpServerResponseCustomizerHolder .getCustomizer ()
45
59
.customize (context , exchange , UndertowHttpResponseMutator .INSTANCE );
46
60
}
61
+
62
+ @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
63
+ public static void onExit (@ Advice .Local ("otelCallDepth" ) CallDepth callDepth ) {
64
+ callDepth .decrementAndGet ();
65
+ }
47
66
}
48
67
}
0 commit comments