|
18 | 18 | import io.opentelemetry.javaagent.bootstrap.CallDepth; |
19 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 21 | +import javax.annotation.Nullable; |
21 | 22 | import net.bytebuddy.asm.Advice; |
22 | 23 | import net.bytebuddy.description.type.TypeDescription; |
23 | 24 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -51,42 +52,56 @@ public void transform(TypeTransformer transformer) { |
51 | 52 | @SuppressWarnings("unused") |
52 | 53 | public static class AnnotatedMethodAdvice { |
53 | 54 |
|
| 55 | + public static class AdviceScope { |
| 56 | + public CallDepth callDepth; |
| 57 | + public SpringWsRequest request; |
| 58 | + public Context context; |
| 59 | + public Scope scope; |
| 60 | + |
| 61 | + public AdviceScope( |
| 62 | + CallDepth callDepth, SpringWsRequest request, Context context, Scope scope) { |
| 63 | + this.callDepth = callDepth; |
| 64 | + this.request = request; |
| 65 | + this.context = context; |
| 66 | + this.scope = scope; |
| 67 | + } |
| 68 | + |
| 69 | + public void exit(@Nullable Throwable throwable) { |
| 70 | + if (callDepth.decrementAndGet() > 0) { |
| 71 | + return; |
| 72 | + } |
| 73 | + scope.close(); |
| 74 | + instrumenter().end(context, request, null, throwable); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Nullable |
54 | 79 | @Advice.OnMethodEnter(suppress = Throwable.class) |
55 | | - public static void startSpan( |
56 | | - @Advice.Origin("#t") Class<?> codeClass, |
57 | | - @Advice.Origin("#m") String methodName, |
58 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
59 | | - @Advice.Local("otelRequest") SpringWsRequest request, |
60 | | - @Advice.Local("otelContext") Context context, |
61 | | - @Advice.Local("otelScope") Scope scope) { |
62 | | - callDepth = CallDepth.forClass(PayloadRoot.class); |
| 80 | + public static AdviceScope startSpan( |
| 81 | + @Advice.Origin("#t") Class<?> codeClass, @Advice.Origin("#m") String methodName) { |
| 82 | + |
| 83 | + CallDepth callDepth = CallDepth.forClass(PayloadRoot.class); |
63 | 84 | if (callDepth.getAndIncrement() > 0) { |
64 | | - return; |
| 85 | + return new AdviceScope(callDepth, null, null, null); |
65 | 86 | } |
66 | 87 |
|
67 | 88 | Context parentContext = currentContext(); |
68 | | - request = SpringWsRequest.create(codeClass, methodName); |
| 89 | + SpringWsRequest request = SpringWsRequest.create(codeClass, methodName); |
69 | 90 | if (!instrumenter().shouldStart(parentContext, request)) { |
70 | | - return; |
| 91 | + return null; |
71 | 92 | } |
72 | 93 |
|
73 | | - context = instrumenter().start(parentContext, request); |
74 | | - scope = context.makeCurrent(); |
| 94 | + Context context = instrumenter().start(parentContext, request); |
| 95 | + return new AdviceScope(callDepth, request, context, parentContext.makeCurrent()); |
75 | 96 | } |
76 | 97 |
|
77 | 98 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
78 | 99 | public static void stopSpan( |
79 | | - @Advice.Thrown Throwable throwable, |
80 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
81 | | - @Advice.Local("otelRequest") SpringWsRequest request, |
82 | | - @Advice.Local("otelContext") Context context, |
83 | | - @Advice.Local("otelScope") Scope scope) { |
84 | | - if (callDepth.decrementAndGet() > 0 || scope == null) { |
85 | | - return; |
| 100 | + @Advice.Thrown @Nullable Throwable throwable, |
| 101 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 102 | + if (adviceScope != null) { |
| 103 | + adviceScope.exit(throwable); |
86 | 104 | } |
87 | | - |
88 | | - scope.close(); |
89 | | - instrumenter().end(context, request, null, throwable); |
90 | 105 | } |
91 | 106 | } |
92 | 107 | } |
0 commit comments