55
66package io .opentelemetry .javaagent .instrumentation .joddhttp .v4_2 ;
77
8- import static io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge .currentContext ;
98import static io .opentelemetry .javaagent .instrumentation .joddhttp .v4_2 .JoddHttpSingletons .instrumenter ;
109import static net .bytebuddy .matcher .ElementMatchers .isMethod ;
1110import static net .bytebuddy .matcher .ElementMatchers .named ;
1514import io .opentelemetry .context .Scope ;
1615import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
1716import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
17+ import javax .annotation .Nullable ;
1818import jodd .http .HttpRequest ;
1919import jodd .http .HttpResponse ;
2020import net .bytebuddy .asm .Advice ;
@@ -38,31 +38,46 @@ public void transform(TypeTransformer transformer) {
3838 @ SuppressWarnings ("unused" )
3939 public static class RequestAdvice {
4040
41- @ Advice .OnMethodEnter (suppress = Throwable .class )
42- public static void methodEnter (
43- @ Advice .This HttpRequest request ,
44- @ Advice .Local ("otelContext" ) Context context ,
45- @ Advice .Local ("otelScope" ) Scope scope ) {
46- Context parentContext = currentContext ();
47- if (!instrumenter ().shouldStart (parentContext , request )) {
48- return ;
41+ public static class AdviceScope {
42+ private final Context context ;
43+ private final Scope scope ;
44+
45+ private AdviceScope (Context context , Scope scope ) {
46+ this .context = context ;
47+ this .scope = scope ;
48+ }
49+
50+ @ Nullable
51+ public static AdviceScope start (HttpRequest request ) {
52+ Context parentContext = Context .current ();
53+ if (!instrumenter ().shouldStart (parentContext , request )) {
54+ return null ;
55+ }
56+ Context context = instrumenter ().start (parentContext , request );
57+ return new AdviceScope (context , context .makeCurrent ());
4958 }
50- context = instrumenter ().start (parentContext , request );
51- scope = context .makeCurrent ();
59+
60+ public void end (HttpRequest request , HttpResponse response , Throwable throwable ) {
61+ scope .close ();
62+ instrumenter ().end (context , request , response , throwable );
63+ }
64+ }
65+
66+ @ Nullable
67+ @ Advice .OnMethodEnter (suppress = Throwable .class )
68+ public static AdviceScope methodEnter (@ Advice .This HttpRequest request ) {
69+ return AdviceScope .start (request );
5270 }
5371
5472 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
5573 public static void methodExit (
5674 @ Advice .This HttpRequest request ,
57- @ Advice .Return HttpResponse response ,
58- @ Advice .Thrown Throwable throwable ,
59- @ Advice .Local ("otelContext" ) Context context ,
60- @ Advice .Local ("otelScope" ) Scope scope ) {
61- if (scope == null ) {
62- return ;
75+ @ Advice .Return @ Nullable HttpResponse response ,
76+ @ Advice .Thrown @ Nullable Throwable throwable ,
77+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
78+ if (adviceScope != null ) {
79+ adviceScope .end (request , response , throwable );
6380 }
64- scope .close ();
65- instrumenter ().end (context , request , response , throwable );
6681 }
6782 }
6883}
0 commit comments