|
2 | 2 |
|
3 | 3 | import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.captureActiveSpan; |
4 | 4 |
|
| 5 | +import datadog.context.Context; |
5 | 6 | import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
6 | | -import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
7 | | -import datadog.trace.bootstrap.instrumentation.api.ScopeState; |
8 | 7 |
|
9 | 8 | public class FiberContext { |
10 | | - private final ScopeState state; |
| 9 | + private final Context context; |
11 | 10 | private AgentScope.Continuation continuation; |
12 | 11 | private AgentScope scope; |
13 | | - private ScopeState oldState; |
| 12 | + private Context oldContext; |
14 | 13 |
|
15 | | - private FiberContext(ScopeState state) { |
16 | | - this.state = state; |
17 | | - this.scope = null; |
18 | | - this.oldState = null; |
| 14 | + private FiberContext(Context context) { |
| 15 | + this.context = context; |
19 | 16 | this.continuation = captureActiveSpan(); |
| 17 | + this.scope = null; |
| 18 | + this.oldContext = null; |
20 | 19 | } |
21 | 20 |
|
22 | 21 | public static FiberContext create() { |
23 | | - final ScopeState state = AgentTracer.get().newScopeState(); |
24 | | - return new FiberContext(state); |
| 22 | + return new FiberContext(Context.root()); |
25 | 23 | } |
26 | 24 |
|
27 | 25 | public void onEnd() { |
28 | | - if (this.scope != null) { |
29 | | - this.scope.close(); |
30 | | - this.scope = null; |
| 26 | + if (scope != null) { |
| 27 | + scope.close(); |
| 28 | + scope = null; |
31 | 29 | } |
32 | 30 | if (continuation != null) { |
33 | 31 | continuation.cancel(); |
34 | 32 | continuation = null; |
35 | 33 | } |
36 | | - |
37 | | - if (this.oldState != null) { |
38 | | - this.oldState.activate(); |
39 | | - this.oldState = null; |
| 34 | + if (oldContext != null) { |
| 35 | + oldContext.swap(); |
| 36 | + oldContext = null; |
40 | 37 | } |
41 | 38 | } |
42 | 39 |
|
43 | 40 | public void onSuspend() { |
44 | | - if (this.scope != null && continuation != null) { |
45 | | - this.scope.close(); |
46 | | - this.scope = null; |
| 41 | + if (scope != null && continuation != null) { |
| 42 | + scope.close(); |
| 43 | + scope = null; |
47 | 44 | } |
48 | | - if (this.oldState != null) { |
49 | | - this.oldState.activate(); |
50 | | - this.oldState = null; |
| 45 | + if (oldContext != null) { |
| 46 | + oldContext.swap(); |
| 47 | + oldContext = null; |
51 | 48 | } |
52 | 49 | } |
53 | 50 |
|
54 | 51 | public void onResume() { |
55 | | - this.oldState = AgentTracer.get().newScopeState(); |
56 | | - this.oldState.fetchFromActive(); |
57 | | - |
58 | | - this.state.activate(); |
59 | | - |
60 | | - if (this.continuation != null) { |
61 | | - this.scope = continuation.activate(); |
| 52 | + oldContext = context.swap(); |
| 53 | + if (continuation != null) { |
| 54 | + scope = continuation.activate(); |
62 | 55 | continuation = null; |
63 | 56 | } |
64 | 57 | } |
|
0 commit comments