|
1 | 1 | package datadog.trace.instrumentation.pekko.concurrent;
|
2 | 2 |
|
3 | 3 | import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
|
4 |
| -import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; |
5 |
| -import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activeSpan; |
6 | 4 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.checkpointActiveForRollback;
|
7 |
| -import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.noopSpan; |
8 | 5 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.rollbackActiveToCheckpoint;
|
9 | 6 | import static java.util.Collections.singletonMap;
|
10 | 7 | import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
|
14 | 11 | import datadog.trace.agent.tooling.InstrumenterModule;
|
15 | 12 | import datadog.trace.bootstrap.InstrumentationContext;
|
16 | 13 | import datadog.trace.bootstrap.instrumentation.api.AgentScope;
|
17 |
| -import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
18 | 14 | import datadog.trace.bootstrap.instrumentation.java.concurrent.AdviceUtils;
|
19 | 15 | import datadog.trace.bootstrap.instrumentation.java.concurrent.State;
|
20 | 16 | import java.util.Map;
|
@@ -58,40 +54,29 @@ public void methodAdvice(MethodTransformer transformer) {
|
58 | 54 | */
|
59 | 55 | public static class InvokeAdvice {
|
60 | 56 | @Advice.OnMethodEnter(suppress = Throwable.class)
|
61 |
| - public static void enter( |
62 |
| - @Advice.Argument(value = 0) Envelope envelope, |
63 |
| - @Advice.Local(value = "taskScope") AgentScope taskScope) { |
64 |
| - checkpointActiveForRollback(); |
65 |
| - // note: task scope may be the same as the scope we want to roll back to, |
66 |
| - // so we must remember to close it on exit to balance the activation count |
67 |
| - taskScope = |
| 57 | + public static AgentScope enter(@Advice.Argument(value = 0) Envelope envelope) { |
| 58 | + |
| 59 | + // do this before checkpointing, as the envelope's task scope may already be active |
| 60 | + AgentScope taskScope = |
68 | 61 | AdviceUtils.startTaskScope(
|
69 | 62 | InstrumentationContext.get(Envelope.class, State.class), envelope);
|
70 |
| - // There was a scope created from the envelope, so use that |
71 |
| - if (taskScope != null) { |
72 |
| - return; |
73 |
| - } |
74 |
| - AgentSpan activeSpan = activeSpan(); |
75 |
| - // If there is no active scope, we can clean all the way to the bottom |
76 |
| - if (activeSpan == null) { |
77 |
| - return; |
78 |
| - } |
79 |
| - // If there is a noop span in the active scope, we can clean all the way to this scope |
80 |
| - if (activeSpan == noopSpan()) { |
81 |
| - return; |
82 |
| - } |
83 |
| - // Create an active scope with a noop span, and clean all the way to the previous scope |
84 |
| - activateSpan(noopSpan()); |
| 63 | + |
| 64 | + // remember the currently active scope so we can roll back to this point |
| 65 | + checkpointActiveForRollback(); |
| 66 | + |
| 67 | + return taskScope; |
85 | 68 | }
|
86 | 69 |
|
87 | 70 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
|
88 |
| - public static void exit(@Advice.Local(value = "taskScope") AgentScope taskScope) { |
| 71 | + public static void exit(@Advice.Enter AgentScope taskScope) { |
| 72 | + |
| 73 | + // Clean up any leaking scopes from pekko-streams/pekko-http etc. |
| 74 | + rollbackActiveToCheckpoint(); |
| 75 | + |
| 76 | + // close envelope's task scope if we previously started it |
89 | 77 | if (taskScope != null) {
|
90 |
| - // then we have invoked an Envelope and need to mark the work complete |
91 | 78 | taskScope.close();
|
92 | 79 | }
|
93 |
| - // Clean up any leaking scopes from pekko-streams/pekko-http etc. |
94 |
| - rollbackActiveToCheckpoint(); |
95 | 80 | }
|
96 | 81 | }
|
97 | 82 | }
|
0 commit comments