Skip to content

Commit 7d0ad79

Browse files
committed
simplify
1 parent 1dcb5ce commit 7d0ad79

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package datadog.trace.instrumentation.zio.v2_0;
22

3+
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.capture;
4+
35
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
4-
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
56
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
67
import datadog.trace.bootstrap.instrumentation.api.ScopeState;
78

89
public class FiberContext {
9-
1010
private final ScopeState state;
11-
private AgentSpan span;
11+
private AgentScope.Continuation continuation;
1212
private AgentScope scope;
1313
private ScopeState oldState;
1414

1515
private FiberContext(ScopeState state) {
1616
this.state = state;
17-
this.span = AgentTracer.activeSpan();
1817
this.scope = null;
1918
this.oldState = null;
19+
this.continuation = capture();
2020
}
2121

2222
public static FiberContext create() {
@@ -29,6 +29,10 @@ public void onEnd() {
2929
this.scope.close();
3030
this.scope = null;
3131
}
32+
if (continuation != null) {
33+
continuation.cancel();
34+
continuation = null;
35+
}
3236

3337
if (this.oldState != null) {
3438
this.oldState.activate();
@@ -37,13 +41,14 @@ public void onEnd() {
3741
}
3842

3943
public void onSuspend() {
44+
if (this.scope != null) {
45+
this.scope.close();
46+
this.scope = null;
47+
}
4048
if (this.oldState != null) {
4149
this.oldState.activate();
4250
this.oldState = null;
4351
}
44-
if (this.scope != null) {
45-
this.scope.close();
46-
}
4752
}
4853

4954
public void onResume() {
@@ -52,8 +57,9 @@ public void onResume() {
5257

5358
this.state.activate();
5459

55-
if (this.span != null && this.scope == null) {
56-
this.scope = AgentTracer.activateSpan(span, true);
60+
if (this.continuation != null) {
61+
this.scope = continuation.activate();
62+
continuation = null;
5763
}
5864
}
5965
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import scala.Option;
55
import zio.Exit;
66
import zio.Fiber;
7+
import zio.FiberId;
78
import zio.Supervisor;
89
import zio.Unsafe;
910
import zio.ZEnvironment;
@@ -12,12 +13,9 @@
1213

1314
@SuppressWarnings("unchecked")
1415
public final class TracingSupervisor extends Supervisor<Object> {
16+
private final ContextStore<FiberId.Runtime, FiberContext> contextStore;
1517

16-
@SuppressWarnings("rawtypes")
17-
private final ContextStore<Fiber.Runtime, FiberContext> contextStore;
18-
19-
@SuppressWarnings("rawtypes")
20-
public TracingSupervisor(ContextStore<Fiber.Runtime, FiberContext> contextStore) {
18+
public TracingSupervisor(ContextStore<FiberId.Runtime, FiberContext> contextStore) {
2119
this.contextStore = contextStore;
2220
}
2321

@@ -35,24 +33,24 @@ public <R, E, A_> void onStart(
3533
Fiber.Runtime<E, A_> fiber,
3634
Unsafe unsafe) {
3735
FiberContext context = FiberContext.create();
38-
contextStore.put(fiber, context);
36+
contextStore.put(fiber.id(), context);
3937
}
4038

4139
@Override
4240
public <R, E, A_> void onEnd(Exit<E, A_> value, Fiber.Runtime<E, A_> fiber, Unsafe unsafe) {
43-
FiberContext context = contextStore.get(fiber);
41+
FiberContext context = contextStore.get(fiber.id());
4442
if (context != null) context.onEnd();
4543
}
4644

4745
@Override
4846
public <E, A_> void onSuspend(Fiber.Runtime<E, A_> fiber, Unsafe unsafe) {
49-
FiberContext context = contextStore.get(fiber);
47+
FiberContext context = contextStore.get(fiber.id());
5048
if (context != null) context.onSuspend();
5149
}
5250

5351
@Override
5452
public <E, A_> void onResume(Fiber.Runtime<E, A_> fiber, Unsafe unsafe) {
55-
FiberContext context = contextStore.get(fiber);
53+
FiberContext context = contextStore.get(fiber.id());
5654
if (context != null) context.onResume();
5755
}
5856
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.Collections;
1515
import java.util.Map;
1616
import net.bytebuddy.asm.Advice;
17-
import zio.Fiber;
17+
import zio.FiberId;
1818
import zio.Supervisor;
1919

2020
@AutoService(Instrumenter.class)
@@ -48,7 +48,10 @@ public String[] helperClassNames() {
4848

4949
@Override
5050
public Map<String, String> contextStore() {
51-
return singletonMap("zio.Fiber$Runtime", packageName + ".FiberContext");
51+
// it looks like zio.Fiber$Runtime will clash with Runnable causing our threadpool
52+
// instrumentation to fetch this key
53+
// but failing in casting to State. Hence, using another class as a key
54+
return singletonMap("zio.FiberId$Runtime", packageName + ".FiberContext");
5255
}
5356

5457
@Override
@@ -60,9 +63,8 @@ public Map<String, String> contextStore() {
6063
public static final class DefaultSupervisor {
6164
@Advice.OnMethodExit(suppress = Throwable.class)
6265
public static void onExit(@Advice.Return(readOnly = false) Supervisor<?> supervisor) {
63-
@SuppressWarnings("rawtypes")
64-
ContextStore<Fiber.Runtime, FiberContext> contextStore =
65-
InstrumentationContext.get(Fiber.Runtime.class, FiberContext.class);
66+
ContextStore<FiberId.Runtime, FiberContext> contextStore =
67+
InstrumentationContext.get(FiberId.Runtime.class, FiberContext.class);
6668
supervisor = supervisor.$plus$plus(new TracingSupervisor(contextStore));
6769
}
6870
}

0 commit comments

Comments
 (0)