|
13 | 13 |
|
14 | 14 | import io.opentelemetry.context.Context; |
15 | 15 | import io.opentelemetry.context.Scope; |
16 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
17 | 16 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
18 | 17 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 18 | +import javax.annotation.Nullable; |
19 | 19 | import net.bytebuddy.asm.Advice; |
20 | 20 | import net.bytebuddy.description.type.TypeDescription; |
21 | 21 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -43,36 +43,53 @@ public void transform(TypeTransformer transformer) { |
43 | 43 | @SuppressWarnings("unused") |
44 | 44 | public static class EventAdvice { |
45 | 45 |
|
46 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
47 | | - public static void onEnter( |
48 | | - @Advice.This ComponentPageElementImpl componentPageElementImpl, |
49 | | - @Advice.Argument(0) String eventType, |
50 | | - @Advice.Local("otelRequest") TapestryRequest request, |
51 | | - @Advice.Local("otelContext") Context context, |
52 | | - @Advice.Local("otelScope") Scope scope) { |
53 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
54 | | - |
55 | | - request = new TapestryRequest(eventType, componentPageElementImpl.getCompleteId()); |
56 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
57 | | - return; |
| 46 | + public static class AdviceScope { |
| 47 | + private final TapestryRequest request; |
| 48 | + private final Context context; |
| 49 | + private final Scope scope; |
| 50 | + |
| 51 | + private AdviceScope(TapestryRequest request, Context context, Scope scope) { |
| 52 | + this.request = request; |
| 53 | + this.context = context; |
| 54 | + this.scope = scope; |
| 55 | + } |
| 56 | + |
| 57 | + @Nullable |
| 58 | + public static AdviceScope start( |
| 59 | + ComponentPageElementImpl componentPageElement, String eventType) { |
| 60 | + Context parentContext = Context.current(); |
| 61 | + |
| 62 | + TapestryRequest request = |
| 63 | + new TapestryRequest(eventType, componentPageElement.getCompleteId()); |
| 64 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + Context context = instrumenter().start(parentContext, request); |
| 69 | + return new AdviceScope(request, context, context.makeCurrent()); |
58 | 70 | } |
59 | 71 |
|
60 | | - context = instrumenter().start(parentContext, request); |
61 | | - scope = context.makeCurrent(); |
| 72 | + public void end(Throwable throwable) { |
| 73 | + scope.close(); |
| 74 | + |
| 75 | + instrumenter().end(context, request, null, throwable); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Nullable |
| 80 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 81 | + public static AdviceScope onEnter( |
| 82 | + @Advice.This ComponentPageElementImpl componentPageElementImpl, |
| 83 | + @Advice.Argument(0) String eventType) { |
| 84 | + return AdviceScope.start(componentPageElementImpl, eventType); |
62 | 85 | } |
63 | 86 |
|
64 | 87 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
65 | 88 | public static void onExit( |
66 | | - @Advice.Thrown Throwable throwable, |
67 | | - @Advice.Local("otelRequest") TapestryRequest request, |
68 | | - @Advice.Local("otelContext") Context context, |
69 | | - @Advice.Local("otelScope") Scope scope) { |
70 | | - if (scope == null) { |
71 | | - return; |
| 89 | + @Advice.Thrown Throwable throwable, @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 90 | + if (adviceScope != null) { |
| 91 | + adviceScope.end(throwable); |
72 | 92 | } |
73 | | - scope.close(); |
74 | | - |
75 | | - instrumenter().end(context, request, null, throwable); |
76 | 93 | } |
77 | 94 | } |
78 | 95 | } |
0 commit comments