|
25 | 25 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
26 | 26 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
27 | 27 | import io.opentelemetry.javaagent.instrumentation.jaxws.common.JaxWsRequest; |
| 28 | +import javax.annotation.Nullable; |
28 | 29 | import javax.jws.WebService; |
29 | 30 | import net.bytebuddy.asm.Advice; |
30 | 31 | import net.bytebuddy.description.type.TypeDescription; |
@@ -62,42 +63,52 @@ public void transform(TypeTransformer transformer) { |
62 | 63 | @SuppressWarnings("unused") |
63 | 64 | public static class JwsAnnotationsAdvice { |
64 | 65 |
|
65 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
66 | | - public static void startSpan( |
67 | | - @Advice.This Object target, |
68 | | - @Advice.Origin("#m") String methodName, |
69 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
70 | | - @Advice.Local("otelRequest") JaxWsRequest request, |
71 | | - @Advice.Local("otelContext") Context context, |
72 | | - @Advice.Local("otelScope") Scope scope) { |
73 | | - callDepth = CallDepth.forClass(WebService.class); |
74 | | - if (callDepth.getAndIncrement() > 0) { |
75 | | - return; |
| 66 | + public static class AdviceScope { |
| 67 | + private final CallDepth callDepth; |
| 68 | + private final JaxWsRequest request; |
| 69 | + private final Context context; |
| 70 | + private final Scope scope; |
| 71 | + |
| 72 | + private AdviceScope(CallDepth callDepth, JaxWsRequest request, Context context, Scope scope) { |
| 73 | + this.callDepth = callDepth; |
| 74 | + this.request = request; |
| 75 | + this.context = context; |
| 76 | + this.scope = scope; |
| 77 | + } |
| 78 | + |
| 79 | + public static AdviceScope start(CallDepth callDepth, Object target, String methodName) { |
| 80 | + if (callDepth.getAndIncrement() > 0) { |
| 81 | + return new AdviceScope(callDepth, null, null, null); |
| 82 | + } |
| 83 | + Context parentContext = currentContext(); |
| 84 | + JaxWsRequest request = new JaxWsRequest(target.getClass(), methodName); |
| 85 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 86 | + return new AdviceScope(callDepth, null, null, null); |
| 87 | + } |
| 88 | + Context context = instrumenter().start(parentContext, request); |
| 89 | + return new AdviceScope(callDepth, request, context, context.makeCurrent()); |
76 | 90 | } |
77 | 91 |
|
78 | | - Context parentContext = currentContext(); |
79 | | - request = new JaxWsRequest(target.getClass(), methodName); |
80 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
81 | | - return; |
| 92 | + public void end(Throwable throwable) { |
| 93 | + if (callDepth.decrementAndGet() > 0 || scope == null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + scope.close(); |
| 97 | + instrumenter().end(context, request, null, throwable); |
82 | 98 | } |
| 99 | + } |
83 | 100 |
|
84 | | - context = instrumenter().start(parentContext, request); |
85 | | - scope = context.makeCurrent(); |
| 101 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 102 | + public static AdviceScope startSpan( |
| 103 | + @Advice.This Object target, @Advice.Origin("#m") String methodName) { |
| 104 | + CallDepth callDepth = CallDepth.forClass(WebService.class); |
| 105 | + return AdviceScope.start(callDepth, target, methodName); |
86 | 106 | } |
87 | 107 |
|
88 | 108 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
89 | 109 | public static void stopSpan( |
90 | | - @Advice.Thrown Throwable throwable, |
91 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
92 | | - @Advice.Local("otelRequest") JaxWsRequest request, |
93 | | - @Advice.Local("otelContext") Context context, |
94 | | - @Advice.Local("otelScope") Scope scope) { |
95 | | - if (callDepth.decrementAndGet() > 0 || scope == null) { |
96 | | - return; |
97 | | - } |
98 | | - |
99 | | - scope.close(); |
100 | | - instrumenter().end(context, request, null, throwable); |
| 110 | + @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter AdviceScope adviceScope) { |
| 111 | + adviceScope.end(throwable); |
101 | 112 | } |
102 | 113 | } |
103 | 114 | } |
0 commit comments