|
8 | 8 | import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
9 | 9 | import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
10 | 10 | import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 11 | +import static io.opentelemetry.javaagent.instrumentation.play.v2_4.Play24Singletons.instrumenter; |
| 12 | +import static io.opentelemetry.javaagent.instrumentation.play.v2_4.Play24Singletons.updateSpan; |
11 | 13 | import static net.bytebuddy.matcher.ElementMatchers.named; |
12 | 14 | import static net.bytebuddy.matcher.ElementMatchers.returns; |
13 | 15 | import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
14 | 16 |
|
| 17 | +import io.opentelemetry.context.Context; |
| 18 | +import io.opentelemetry.context.Scope; |
15 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
16 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
17 | 21 | import javax.annotation.Nullable; |
@@ -65,4 +69,45 @@ public static void stopTraceOnResponse( |
65 | 69 | actionScope.end(throwable, responseFuture, (Action<?>) thisAction, req); |
66 | 70 | } |
67 | 71 | } |
| 72 | + |
| 73 | + public static class AdviceScope { |
| 74 | + |
| 75 | + private final Context context; |
| 76 | + private final Scope scope; |
| 77 | + |
| 78 | + public AdviceScope(Context context, Scope scope) { |
| 79 | + this.context = context; |
| 80 | + this.scope = scope; |
| 81 | + } |
| 82 | + |
| 83 | + @Nullable |
| 84 | + public static AdviceScope start(Context parentContext) { |
| 85 | + if (!instrumenter().shouldStart(parentContext, null)) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + Context context = instrumenter().start(parentContext, null); |
| 90 | + return new AdviceScope(context, context.makeCurrent()); |
| 91 | + } |
| 92 | + |
| 93 | + public void closeScope() { |
| 94 | + if (scope != null) { |
| 95 | + scope.close(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void end( |
| 100 | + Throwable throwable, Future<Result> responseFuture, Action<?> thisAction, Request<?> req) { |
| 101 | + closeScope(); |
| 102 | + updateSpan(context, req); |
| 103 | + |
| 104 | + if (throwable == null) { |
| 105 | + // span finished in RequestCompleteCallback |
| 106 | + responseFuture.onComplete( |
| 107 | + new RequestCompleteCallback(context), thisAction.executionContext()); |
| 108 | + } else { |
| 109 | + instrumenter().end(context, null, null, throwable); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
68 | 113 | } |
0 commit comments