Skip to content

Commit 9ef1718

Browse files
committed
Protect currentContext access for reactor inner operators
1 parent 42ee2aa commit 9ef1718

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

dd-java-agent/instrumentation/reactive-streams/src/main/java/datadog/trace/instrumentation/reactivestreams/PublisherInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static AgentScope onSubscribe(
9494
final AgentSpan span =
9595
InstrumentationContext.get(Publisher.class, AgentSpan.class).remove(self);
9696
final AgentSpan activeSpan = activeSpan();
97-
if (span == null && activeSpan == null) {
97+
if (s == null || (span == null && activeSpan == null)) {
9898
return null;
9999
}
100100
final AgentSpan current =

dd-java-agent/instrumentation/reactive-streams/src/main/java/datadog/trace/instrumentation/reactivestreams/SubscriberInstrumentation.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import datadog.trace.bootstrap.InstrumentationContext;
1515
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
1616
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
17-
import java.lang.reflect.Method;
1817
import java.util.Collections;
1918
import java.util.Map;
2019
import net.bytebuddy.asm.Advice;
@@ -65,8 +64,7 @@ public ElementMatcher<TypeDescription> hierarchyMatcher() {
6564
*/
6665
public static class SubscriberDownStreamAdvice {
6766
@Advice.OnMethodEnter(suppress = Throwable.class)
68-
public static AgentScope before(
69-
@Advice.Origin final Method m, @Advice.This final Subscriber self) {
67+
public static AgentScope before(@Advice.This final Subscriber self) {
7068
if (activeSpan() != null) {
7169
return null;
7270
}

dd-java-agent/instrumentation/reactor-core-3.1/src/latestDepTest/groovy/ReactorCoreTest.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,15 @@ class ReactorCoreTest extends AgentTestRunner {
441441
})
442442
}
443443

444+
def "test currentContext() calls on inner operator is not throwing a NPE on the advice"() {
445+
when:
446+
def mono = Flux.range(1, 100).windowUntil {it % 10 == 0}.count()
447+
then:
448+
// we are not interested into asserting a trace structure but only that the instrumentation error count is 0
449+
assert mono.block() == 10
450+
}
451+
452+
444453
@Trace(operationName = "trace-parent", resourceName = "trace-parent")
445454
def assemblePublisherUnderTrace(def publisherSupplier) {
446455
def span = startSpan("publisher-parent")

dd-java-agent/instrumentation/reactor-core-3.1/src/main/java/datadog/trace/instrumentation/reactor/core/CoreSubscriberInstrumentation.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ public void methodAdvice(MethodTransformer transformer) {
4444
public static class PropagateSpanInScopeAdvice {
4545
@Advice.OnMethodEnter(suppress = Throwable.class)
4646
public static AgentScope before(@Advice.This final CoreSubscriber<?> self) {
47-
final Context context = self.currentContext();
47+
Context context = null;
48+
try {
49+
context = self.currentContext();
50+
} catch (Throwable ignored) {
51+
}
52+
if (context == null) {
53+
return null;
54+
}
4855
if (context.hasKey("dd.span")) {
4956
Object maybeSpan = context.get("dd.span");
5057
if (maybeSpan instanceof WithAgentSpan) {

dd-java-agent/instrumentation/reactor-core-3.1/src/test/groovy/ReactorCoreTest.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ class ReactorCoreTest extends AgentTestRunner {
490490
span.finish()
491491
}
492492

493+
def "test currentContext() calls on inner operator is not throwing a NPE on the advice"() {
494+
when:
495+
def mono = Flux.range(1, 100).windowUntil {it % 10 == 0}.count()
496+
then:
497+
// we are not interested into asserting a trace structure but only that the instrumentation error count is 0
498+
assert mono.block() == 11
499+
}
500+
493501
@Trace(operationName = "addOne", resourceName = "addOne")
494502
def static addOneFunc(int i) {
495503
return i + 1

0 commit comments

Comments
 (0)