|
16 | 16 | import io.opentelemetry.context.Context; |
17 | 17 | import io.opentelemetry.context.Scope; |
18 | 18 | import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute; |
19 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
20 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
21 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 21 | +import javax.annotation.Nullable; |
22 | 22 | import net.bytebuddy.asm.Advice; |
23 | 23 | import net.bytebuddy.description.type.TypeDescription; |
24 | 24 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -46,39 +46,50 @@ public void transform(TypeTransformer transformer) { |
46 | 46 | @SuppressWarnings("unused") |
47 | 47 | public static class InvokeActionOnlyAdvice { |
48 | 48 |
|
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; |
55 | 52 |
|
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 | + } |
61 | 57 |
|
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()); |
64 | 72 | } |
65 | 73 |
|
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); |
68 | 83 | } |
69 | 84 |
|
70 | 85 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
71 | 86 | public static void stopSpan( |
72 | | - @Advice.Thrown Throwable throwable, |
| 87 | + @Advice.Thrown @Nullable Throwable throwable, |
73 | 88 | @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); |
78 | 92 | } |
79 | | - scope.close(); |
80 | | - |
81 | | - instrumenter().end(context, actionInvocation, null, throwable); |
82 | 93 | } |
83 | 94 | } |
84 | 95 | } |
0 commit comments