Skip to content

Commit 8bc35ce

Browse files
committed
Cleanup Zio fiber instrumentation to avoid repeated activation of continuation
1 parent f494c33 commit 8bc35ce

File tree

2 files changed

+19
-43
lines changed

2 files changed

+19
-43
lines changed

dd-java-agent/instrumentation/zio/zio-2.0/src/main/java/datadog/trace/instrumentation/zio/v2_0/FiberContext.java

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,34 @@
77
import datadog.trace.bootstrap.instrumentation.api.ScopeState;
88

99
public class FiberContext {
10-
private final ScopeState state;
11-
private AgentScope.Continuation continuation;
12-
private AgentScope scope;
13-
private ScopeState oldState;
10+
private final ScopeState scopeState;
11+
private final AgentScope.Continuation continuation;
1412

15-
private FiberContext(ScopeState state) {
16-
this.state = state;
17-
this.scope = null;
18-
this.oldState = null;
19-
this.continuation = captureActiveSpan();
20-
}
13+
private ScopeState oldScopeState;
2114

22-
public static FiberContext create() {
23-
final ScopeState state = AgentTracer.get().newScopeState();
24-
return new FiberContext(state);
15+
public FiberContext() {
16+
// copy scope stack to use for this fiber
17+
this.scopeState = AgentTracer.get().oldScopeState().copy();
18+
// stop enclosing trace from finishing early
19+
this.continuation = captureActiveSpan();
2520
}
2621

27-
public void onEnd() {
28-
if (this.scope != null) {
29-
this.scope.close();
30-
this.scope = null;
31-
}
32-
if (continuation != null) {
33-
continuation.cancel();
34-
continuation = null;
35-
}
36-
37-
if (this.oldState != null) {
38-
this.oldState.activate();
39-
this.oldState = null;
40-
}
22+
public void onResume() {
23+
oldScopeState = AgentTracer.get().oldScopeState();
24+
scopeState.activate(); // swap in the fiber's scope stack
4125
}
4226

4327
public void onSuspend() {
44-
if (this.scope != null && continuation != null) {
45-
this.scope.close();
46-
this.scope = null;
47-
}
48-
if (this.oldState != null) {
49-
this.oldState.activate();
50-
this.oldState = null;
28+
if (oldScopeState != null) {
29+
oldScopeState.activate(); // swap bock the original scope stack
30+
oldScopeState = null;
5131
}
5232
}
5333

54-
public void onResume() {
55-
this.oldState = AgentTracer.get().oldScopeState();
56-
57-
this.state.activate();
58-
59-
if (this.continuation != null) {
60-
this.scope = continuation.activate();
61-
continuation = null;
34+
public void onEnd() {
35+
if (continuation != null) {
36+
// release enclosing trace now the fiber has ended
37+
continuation.cancel();
6238
}
6339
}
6440
}

dd-java-agent/instrumentation/zio/zio-2.0/src/main/java/datadog/trace/instrumentation/zio/v2_0/TracingSupervisor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public <R, E, A_> void onStart(
3434
Option<Fiber.Runtime<Object, Object>> parent,
3535
Fiber.Runtime<E, A_> fiber,
3636
Unsafe unsafe) {
37-
FiberContext context = FiberContext.create();
37+
FiberContext context = new FiberContext();
3838
contextStore.put(fiber, context);
3939
}
4040

0 commit comments

Comments
 (0)