1717import io .opentelemetry .context .Context ;
1818import io .opentelemetry .context .Scope ;
1919import io .opentelemetry .instrumentation .api .semconv .http .HttpServerRoute ;
20- import io .opentelemetry .javaagent .bootstrap .Java8BytecodeBridge ;
2120import io .opentelemetry .javaagent .extension .instrumentation .TypeInstrumentation ;
2221import io .opentelemetry .javaagent .extension .instrumentation .TypeTransformer ;
22+ import javax .annotation .Nullable ;
2323import net .bytebuddy .asm .Advice ;
2424import net .bytebuddy .description .type .TypeDescription ;
2525import net .bytebuddy .matcher .ElementMatcher ;
@@ -46,39 +46,50 @@ public void transform(TypeTransformer transformer) {
4646 @ SuppressWarnings ("unused" )
4747 public static class InvokeActionOnlyAdvice {
4848
49- @ Advice .OnMethodEnter (suppress = Throwable .class )
50- public static void onEnter (
51- @ Advice .This ActionInvocation actionInvocation ,
52- @ Advice .Local ("otelContext" ) Context context ,
53- @ Advice .Local ("otelScope" ) Scope scope ) {
54- Context parentContext = Java8BytecodeBridge .currentContext ();
49+ public static class AdviceScope {
50+ private final Context context ;
51+ private final Scope scope ;
5552
56- HttpServerRoute .update (
57- parentContext ,
58- CONTROLLER ,
59- StrutsServerSpanNaming .SERVER_SPAN_NAME ,
60- actionInvocation .getProxy ());
53+ public AdviceScope (Context context , Scope scope ) {
54+ this .context = context ;
55+ this .scope = scope ;
56+ }
6157
62- if (!instrumenter ().shouldStart (parentContext , actionInvocation )) {
63- return ;
58+ @ Nullable
59+ public static AdviceScope start (ActionInvocation actionInvocation ) {
60+ Context parentContext = Context .current ();
61+ HttpServerRoute .update (
62+ parentContext ,
63+ CONTROLLER ,
64+ StrutsServerSpanNaming .SERVER_SPAN_NAME ,
65+ actionInvocation .getProxy ());
66+
67+ if (!instrumenter ().shouldStart (parentContext , actionInvocation )) {
68+ return null ;
69+ }
70+ Context context = instrumenter ().start (parentContext , actionInvocation );
71+ return new AdviceScope (context , context .makeCurrent ());
6472 }
6573
66- context = instrumenter ().start (parentContext , actionInvocation );
67- scope = context .makeCurrent ();
74+ public void end (ActionInvocation actionInvocation , @ Nullable Throwable throwable ) {
75+ scope .close ();
76+ instrumenter ().end (context , actionInvocation , null , throwable );
77+ }
78+ }
79+
80+ @ Advice .OnMethodEnter (suppress = Throwable .class )
81+ public static AdviceScope onEnter (@ Advice .This ActionInvocation actionInvocation ) {
82+ return AdviceScope .start (actionInvocation );
6883 }
6984
7085 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
7186 public static void stopSpan (
72- @ Advice .Thrown Throwable throwable ,
87+ @ Advice .Thrown @ Nullable Throwable throwable ,
7388 @ Advice .This ActionInvocation actionInvocation ,
74- @ Advice .Local ("otelContext" ) Context context ,
75- @ Advice .Local ("otelScope" ) Scope scope ) {
76- if (scope == null ) {
77- return ;
89+ @ Advice .Enter @ Nullable AdviceScope adviceScope ) {
90+ if (adviceScope != null ) {
91+ adviceScope .end (actionInvocation , throwable );
7892 }
79- scope .close ();
80-
81- instrumenter ().end (context , actionInvocation , null , throwable );
8293 }
8394 }
8495}
0 commit comments