Skip to content

Commit 52a8cc7

Browse files
authored
Protect intercepted null traces from NPE (#9797)
* Protect intercepted null traces from NPE * Remove bad check
1 parent f651c33 commit 52a8cc7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static datadog.trace.common.metrics.MetricsAggregatorFactory.createMetricsAggregator;
1414
import static datadog.trace.util.AgentThreadFactory.AGENT_THREAD_GROUP;
1515
import static datadog.trace.util.CollectionUtils.tryMakeImmutableMap;
16+
import static java.util.Collections.emptyList;
1617
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1718
import static java.util.concurrent.TimeUnit.MINUTES;
1819
import static java.util.concurrent.TimeUnit.NANOSECONDS;
@@ -1183,7 +1184,11 @@ private List<DDSpan> interceptCompleteTrace(List<DDSpan> trace) {
11831184
String interceptorName = interceptor.getClass().getName();
11841185
rlLog.warn("Throwable raised in TraceInterceptor {}", interceptorName, e);
11851186
}
1187+
if (interceptedTrace == null) {
1188+
interceptedTrace = emptyList();
1189+
}
11861190
}
1191+
11871192
trace = new ArrayList<>(interceptedTrace.size());
11881193
for (final MutableSpan span : interceptedTrace) {
11891194
if (span instanceof DDSpan) {

dd-trace-core/src/test/groovy/datadog/trace/core/TraceInterceptorTest.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,29 @@ class TraceInterceptorTest extends DDCoreSpecification {
171171
tags.size() >= 7
172172
}
173173
174+
def "should be robust when interceptor return a null trace"() {
175+
setup:
176+
tracer.interceptors.add(new TraceInterceptor() {
177+
@Override
178+
Collection<? extends MutableSpan> onTraceComplete(Collection<? extends MutableSpan> trace) {
179+
null
180+
}
181+
182+
@Override
183+
int priority() {
184+
return 0
185+
}
186+
})
187+
188+
when:
189+
DDSpan span = (DDSpan) tracer.startSpan("test", "test")
190+
span.phasedFinish()
191+
tracer.write([span])
192+
193+
then:
194+
notThrown(Throwable)
195+
}
196+
174197
def "register interceptor through bridge"() {
175198
setup:
176199
GlobalTracer.registerIfAbsent(tracer)

0 commit comments

Comments
 (0)