|
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
21 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
22 | 22 | import io.opentelemetry.javaagent.instrumentation.jaxws.common.JaxWsRequest; |
| 23 | +import javax.annotation.Nullable; |
23 | 24 | import javax.xml.ws.Provider; |
24 | 25 | import net.bytebuddy.asm.Advice; |
25 | 26 | import net.bytebuddy.description.type.TypeDescription; |
@@ -47,42 +48,52 @@ public void transform(TypeTransformer transformer) { |
47 | 48 | @SuppressWarnings("unused") |
48 | 49 | public static class InvokeAdvice { |
49 | 50 |
|
50 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
51 | | - public static void startSpan( |
52 | | - @Advice.This Object target, |
53 | | - @Advice.Origin("#m") String methodName, |
54 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
55 | | - @Advice.Local("otelRequest") JaxWsRequest request, |
56 | | - @Advice.Local("otelContext") Context context, |
57 | | - @Advice.Local("otelScope") Scope scope) { |
58 | | - callDepth = CallDepth.forClass(Provider.class); |
59 | | - if (callDepth.getAndIncrement() > 0) { |
60 | | - return; |
| 51 | + public static class AdviceScope { |
| 52 | + private final CallDepth callDepth; |
| 53 | + private final JaxWsRequest request; |
| 54 | + private final Context context; |
| 55 | + private final Scope scope; |
| 56 | + |
| 57 | + private AdviceScope(CallDepth callDepth, JaxWsRequest request, Context context, Scope scope) { |
| 58 | + this.callDepth = callDepth; |
| 59 | + this.request = request; |
| 60 | + this.context = context; |
| 61 | + this.scope = scope; |
| 62 | + } |
| 63 | + |
| 64 | + public static AdviceScope start(CallDepth callDepth, Object target, String methodName) { |
| 65 | + if (callDepth.getAndIncrement() > 0) { |
| 66 | + return new AdviceScope(callDepth, null, null, null); |
| 67 | + } |
| 68 | + Context parentContext = currentContext(); |
| 69 | + JaxWsRequest request = new JaxWsRequest(target.getClass(), methodName); |
| 70 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 71 | + return new AdviceScope(callDepth, null, null, null); |
| 72 | + } |
| 73 | + Context context = instrumenter().start(parentContext, request); |
| 74 | + return new AdviceScope(callDepth, request, context, context.makeCurrent()); |
61 | 75 | } |
62 | 76 |
|
63 | | - Context parentContext = currentContext(); |
64 | | - request = new JaxWsRequest(target.getClass(), methodName); |
65 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
66 | | - return; |
| 77 | + public void end(Throwable throwable) { |
| 78 | + if (callDepth.decrementAndGet() > 0 || scope == null) { |
| 79 | + return; |
| 80 | + } |
| 81 | + scope.close(); |
| 82 | + instrumenter().end(context, request, null, throwable); |
67 | 83 | } |
| 84 | + } |
68 | 85 |
|
69 | | - context = instrumenter().start(parentContext, request); |
70 | | - scope = context.makeCurrent(); |
| 86 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 87 | + public static AdviceScope startSpan( |
| 88 | + @Advice.This Object target, @Advice.Origin("#m") String methodName) { |
| 89 | + CallDepth callDepth = CallDepth.forClass(Provider.class); |
| 90 | + return AdviceScope.start(callDepth, target, methodName); |
71 | 91 | } |
72 | 92 |
|
73 | 93 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
74 | 94 | public static void stopSpan( |
75 | | - @Advice.Thrown Throwable throwable, |
76 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
77 | | - @Advice.Local("otelRequest") JaxWsRequest request, |
78 | | - @Advice.Local("otelContext") Context context, |
79 | | - @Advice.Local("otelScope") Scope scope) { |
80 | | - if (callDepth.decrementAndGet() > 0 || scope == null) { |
81 | | - return; |
82 | | - } |
83 | | - |
84 | | - scope.close(); |
85 | | - instrumenter().end(context, request, null, throwable); |
| 95 | + @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) { |
| 96 | + adviceScope.end(throwable); |
86 | 97 | } |
87 | 98 | } |
88 | 99 | } |
0 commit comments