|
18 | 18 | import io.opentelemetry.context.Scope; |
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; |
@@ -47,32 +48,46 @@ public void transform(TypeTransformer transformer) { |
47 | 48 | @SuppressWarnings("unused") |
48 | 49 | public static class ExecuteMethodAdvice { |
49 | 50 |
|
50 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
51 | | - public static void methodEnter( |
52 | | - @Advice.Argument(1) HttpMethod httpMethod, |
53 | | - @Advice.Local("otelContext") Context context, |
54 | | - @Advice.Local("otelScope") Scope scope) { |
55 | | - Context parentContext = currentContext(); |
56 | | - if (!instrumenter().shouldStart(parentContext, httpMethod)) { |
57 | | - return; |
| 51 | + public static class AdviceScope { |
| 52 | + private final Context context; |
| 53 | + private final Scope scope; |
| 54 | + private final HttpMethod httpMethod; |
| 55 | + |
| 56 | + private AdviceScope(Context context, Scope scope, HttpMethod httpMethod) { |
| 57 | + this.context = context; |
| 58 | + this.scope = scope; |
| 59 | + this.httpMethod = httpMethod; |
| 60 | + } |
| 61 | + |
| 62 | + @Nullable |
| 63 | + public static AdviceScope start(HttpMethod httpMethod) { |
| 64 | + Context parentContext = currentContext(); |
| 65 | + if (!instrumenter().shouldStart(parentContext, httpMethod)) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + Context context = instrumenter().start(parentContext, httpMethod); |
| 69 | + return new AdviceScope(context, context.makeCurrent(), httpMethod); |
| 70 | + } |
| 71 | + |
| 72 | + public void end(Throwable throwable) { |
| 73 | + scope.close(); |
| 74 | + instrumenter().end(context, httpMethod, httpMethod, throwable); |
58 | 75 | } |
| 76 | + } |
59 | 77 |
|
60 | | - context = instrumenter().start(parentContext, httpMethod); |
61 | | - scope = context.makeCurrent(); |
| 78 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 79 | + public static AdviceScope methodEnter(@Advice.Argument(1) HttpMethod httpMethod) { |
| 80 | + return AdviceScope.start(httpMethod); |
62 | 81 | } |
63 | 82 |
|
64 | 83 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
65 | 84 | public static void methodExit( |
66 | | - @Advice.Argument(1) HttpMethod httpMethod, |
67 | | - @Advice.Thrown Throwable throwable, |
68 | | - @Advice.Local("otelContext") Context context, |
69 | | - @Advice.Local("otelScope") Scope scope) { |
70 | | - if (scope == null) { |
71 | | - return; |
72 | | - } |
| 85 | + @Advice.Thrown @Nullable Throwable throwable, |
| 86 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
73 | 87 |
|
74 | | - scope.close(); |
75 | | - instrumenter().end(context, httpMethod, httpMethod, throwable); |
| 88 | + if (adviceScope != null) { |
| 89 | + adviceScope.end(throwable); |
| 90 | + } |
76 | 91 | } |
77 | 92 | } |
78 | 93 | } |
0 commit comments