|
3 | 3 | import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
4 | 4 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.checkpointActiveForRollback; |
5 | 5 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.rollbackActiveToCheckpoint; |
| 6 | +import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext; |
6 | 7 | import static java.util.Collections.singletonMap; |
7 | 8 | import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
8 | 9 |
|
9 | 10 | import akka.dispatch.Envelope; |
10 | 11 | import com.google.auto.service.AutoService; |
| 12 | +import datadog.context.Context; |
11 | 13 | import datadog.trace.agent.tooling.Instrumenter; |
12 | 14 | import datadog.trace.agent.tooling.InstrumenterModule; |
| 15 | +import datadog.trace.api.InstrumenterConfig; |
13 | 16 | import datadog.trace.bootstrap.InstrumentationContext; |
14 | 17 | import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
15 | 18 | import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils; |
@@ -54,24 +57,34 @@ public void methodAdvice(MethodTransformer transformer) { |
54 | 57 | */ |
55 | 58 | public static class InvokeAdvice { |
56 | 59 | @Advice.OnMethodEnter(suppress = Throwable.class) |
57 | | - public static AgentScope enter(@Advice.Argument(value = 0) Envelope envelope) { |
| 60 | + public static Context enter( |
| 61 | + @Advice.Argument(value = 0) Envelope envelope, |
| 62 | + @Advice.Local("taskScope") AgentScope taskScope) { |
58 | 63 |
|
59 | 64 | // do this before checkpointing, as the envelope's task scope may already be active |
60 | | - AgentScope taskScope = |
| 65 | + taskScope = |
61 | 66 | AdviceUtils.startTaskScope( |
62 | 67 | InstrumentationContext.get(Envelope.class, State.class), envelope); |
63 | 68 |
|
64 | | - // remember the currently active scope so we can roll back to this point |
65 | | - checkpointActiveForRollback(); |
66 | | - |
67 | | - return taskScope; |
| 69 | + if (InstrumenterConfig.get().isLegacyContextManagerEnabled()) { |
| 70 | + // remember the currently active scope so we can roll back to this point |
| 71 | + checkpointActiveForRollback(); |
| 72 | + return null; |
| 73 | + } else { |
| 74 | + return getCurrentContext().swap(); |
| 75 | + } |
68 | 76 | } |
69 | 77 |
|
70 | 78 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
71 | | - public static void exit(@Advice.Enter AgentScope taskScope) { |
| 79 | + public static void exit( |
| 80 | + @Advice.Local("taskScope") AgentScope taskScope, @Advice.Enter Context checkpointContext) { |
72 | 81 |
|
73 | | - // Clean up any leaking scopes from akka-streams/akka-http etc. |
74 | | - rollbackActiveToCheckpoint(); |
| 82 | + if (checkpointContext == null) { |
| 83 | + // Clean up any leaking scopes from akka-streams/akka-http etc. |
| 84 | + rollbackActiveToCheckpoint(); |
| 85 | + } else { |
| 86 | + checkpointContext.swap(); |
| 87 | + } |
75 | 88 |
|
76 | 89 | // close envelope's task scope if we previously started it |
77 | 90 | if (taskScope != null) { |
|
0 commit comments