Skip to content

Commit 7d0a7e8

Browse files
authored
Fix AssertionError when ServiceTalk context captured with no activeSpan (#8951)
1 parent 3a93c7c commit 7d0a7e8

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

dd-java-agent/instrumentation/servicetalk/servicetalk-0.42.56/src/main/java/datadog/trace/instrumentation/servicetalk0_42_56/DatadogCapturedContextProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ public CapturedContext captureContext(CapturedContext underlying) {
1616

1717
@Override
1818
public CapturedContext captureContextCopy(CapturedContext underlying) {
19-
return new WithDatadogCapturedContext(AgentTracer.activeSpan(), underlying);
19+
AgentSpan activeSpan = AgentTracer.activeSpan();
20+
if (activeSpan == null) {
21+
return underlying;
22+
}
23+
return new WithDatadogCapturedContext(activeSpan, underlying);
2024
}
2125

2226
private static final class WithDatadogCapturedContext implements CapturedContext {

dd-java-agent/instrumentation/servicetalk/servicetalk-0.42.56/src/test/groovy/ContextPreservingInstrumentationTest.groovy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ class ContextPreservingInstrumentationTest extends AgentTestRunner {
2727
assertParentChildTrace()
2828
}
2929

30+
def "capturedContext without an active span"() {
31+
when:
32+
runInSeparateThread {
33+
try (def _ = asyncContextProvider.captureContext().attachContext()) {
34+
childSpan()
35+
}
36+
}
37+
38+
then:
39+
assertTraces(1) {
40+
trace(1) {
41+
span {
42+
operationName "child"
43+
tags {
44+
defaultTags()
45+
}
46+
}
47+
}
48+
}
49+
}
50+
3051
def "wrapBiConsumer"() {
3152
setup:
3253
def parent = startParentContext()

0 commit comments

Comments
 (0)