|
14 | 14 | import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
15 | 15 |
|
16 | 16 | import io.dropwizard.views.View; |
| 17 | +import io.opentelemetry.api.trace.Span; |
17 | 18 | import io.opentelemetry.context.Context; |
18 | 19 | import io.opentelemetry.context.Scope; |
19 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
21 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 22 | +import javax.annotation.Nullable; |
22 | 23 | import net.bytebuddy.asm.Advice; |
23 | 24 | import net.bytebuddy.description.type.TypeDescription; |
24 | 25 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -47,37 +48,50 @@ public void transform(TypeTransformer transformer) { |
47 | 48 | @SuppressWarnings("unused") |
48 | 49 | public static class RenderAdvice { |
49 | 50 |
|
50 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
51 | | - public static void onEnter( |
52 | | - @Advice.Argument(0) View view, |
53 | | - @Advice.Local("otelContext") Context context, |
54 | | - @Advice.Local("otelScope") Scope scope) { |
| 51 | + public static class AdviceScope { |
| 52 | + private final Context context; |
| 53 | + private final Scope scope; |
| 54 | + |
| 55 | + private AdviceScope(Context context, Scope scope) { |
| 56 | + this.context = context; |
| 57 | + this.scope = scope; |
| 58 | + } |
55 | 59 |
|
56 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
| 60 | + @Nullable |
| 61 | + public static AdviceScope start(View view) { |
| 62 | + Context parentContext = Context.current(); |
57 | 63 |
|
58 | | - // don't start a new top-level span |
59 | | - if (!Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) { |
60 | | - return; |
| 64 | + // don't start a new top-level span |
| 65 | + if (!Span.fromContext(parentContext).getSpanContext().isValid()) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + if (!instrumenter().shouldStart(parentContext, view)) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + Context context = instrumenter().start(parentContext, view); |
| 73 | + return new AdviceScope(context, context.makeCurrent()); |
61 | 74 | } |
62 | | - if (!instrumenter().shouldStart(parentContext, view)) { |
63 | | - return; |
| 75 | + |
| 76 | + public void end(View view, @Nullable Throwable throwable) { |
| 77 | + scope.close(); |
| 78 | + instrumenter().end(context, view, null, throwable); |
64 | 79 | } |
| 80 | + } |
65 | 81 |
|
66 | | - context = instrumenter().start(parentContext, view); |
67 | | - scope = context.makeCurrent(); |
| 82 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 83 | + public static AdviceScope onEnter(@Advice.Argument(0) View view) { |
| 84 | + return AdviceScope.start(view); |
68 | 85 | } |
69 | 86 |
|
70 | 87 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
71 | 88 | public static void stopSpan( |
72 | 89 | @Advice.Argument(0) View view, |
73 | | - @Advice.Thrown Throwable throwable, |
74 | | - @Advice.Local("otelContext") Context context, |
75 | | - @Advice.Local("otelScope") Scope scope) { |
76 | | - if (scope == null) { |
77 | | - return; |
| 90 | + @Advice.Thrown @Nullable Throwable throwable, |
| 91 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 92 | + if (adviceScope != null) { |
| 93 | + adviceScope.end(view, throwable); |
78 | 94 | } |
79 | | - scope.close(); |
80 | | - instrumenter().end(context, view, null, throwable); |
81 | 95 | } |
82 | 96 | } |
83 | 97 | } |
0 commit comments