Skip to content

Commit e600841

Browse files
committed
Avoid NPE when TraceContext has deferred sampling
Closes gh-33093
1 parent 6e4bece commit e600841

File tree

2 files changed

+156
-1
lines changed

2 files changed

+156
-1
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/prometheus/PrometheusExemplarsAutoConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ public String getSpanId() {
8080
@Override
8181
public boolean isSampled() {
8282
Span currentSpan = currentSpan();
83-
return currentSpan != null && currentSpan.context().sampled();
83+
if (currentSpan == null) {
84+
return false;
85+
}
86+
Boolean sampled = currentSpan.context().sampled();
87+
return sampled != null && sampled;
8488
}
8589

8690
private Span currentSpan() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.tracing.prometheus;
18+
19+
import io.micrometer.tracing.Span;
20+
import io.micrometer.tracing.TraceContext;
21+
import io.micrometer.tracing.Tracer;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.beans.BeansException;
25+
import org.springframework.beans.factory.ObjectProvider;
26+
import org.springframework.boot.actuate.autoconfigure.tracing.prometheus.PrometheusExemplarsAutoConfiguration.LazyTracingSpanContextSupplier;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.mock;
31+
32+
/**
33+
* Tests for {@link LazyTracingSpanContextSupplier}.
34+
*
35+
* @author Andy Wilkinson
36+
*/
37+
class LazyTracingSpanContextSupplierTests {
38+
39+
private final Tracer tracer = mock(Tracer.class);
40+
41+
private final ObjectProvider<Tracer> objectProvider = new ObjectProvider<>() {
42+
43+
@Override
44+
public Tracer getObject() throws BeansException {
45+
return LazyTracingSpanContextSupplierTests.this.tracer;
46+
}
47+
48+
@Override
49+
public Tracer getObject(Object... args) throws BeansException {
50+
return LazyTracingSpanContextSupplierTests.this.tracer;
51+
}
52+
53+
@Override
54+
public Tracer getIfAvailable() throws BeansException {
55+
return LazyTracingSpanContextSupplierTests.this.tracer;
56+
}
57+
58+
@Override
59+
public Tracer getIfUnique() throws BeansException {
60+
return LazyTracingSpanContextSupplierTests.this.tracer;
61+
}
62+
63+
};
64+
65+
private final LazyTracingSpanContextSupplier spanContextSupplier = new LazyTracingSpanContextSupplier(
66+
this.objectProvider);
67+
68+
@Test
69+
void whenCurrentSpanIsNullThenSpanIdIsNull() {
70+
assertThat(this.spanContextSupplier.getSpanId()).isNull();
71+
}
72+
73+
@Test
74+
void whenCurrentSpanIsNullThenTraceIdIsNull() {
75+
assertThat(this.spanContextSupplier.getTraceId()).isNull();
76+
}
77+
78+
@Test
79+
void whenCurrentSpanIsNullThenSampledIsFalse() {
80+
assertThat(this.spanContextSupplier.isSampled()).isFalse();
81+
}
82+
83+
@Test
84+
void whenCurrentSpanHasSpanIdThenSpanIdIsFromSpan() {
85+
Span span = mock(Span.class);
86+
given(this.tracer.currentSpan()).willReturn(span);
87+
TraceContext traceContext = mock(TraceContext.class);
88+
given(traceContext.spanId()).willReturn("span-id");
89+
given(span.context()).willReturn(traceContext);
90+
assertThat(this.spanContextSupplier.getSpanId()).isEqualTo("span-id");
91+
}
92+
93+
@Test
94+
void whenCurrentSpanHasTraceIdThenTraceIdIsFromSpan() {
95+
Span span = mock(Span.class);
96+
given(this.tracer.currentSpan()).willReturn(span);
97+
TraceContext traceContext = mock(TraceContext.class);
98+
given(traceContext.traceId()).willReturn("trace-id");
99+
given(span.context()).willReturn(traceContext);
100+
assertThat(this.spanContextSupplier.getTraceId()).isEqualTo("trace-id");
101+
}
102+
103+
@Test
104+
void whenCurrentSpanHasNoSpanIdThenSpanIdIsNull() {
105+
Span span = mock(Span.class);
106+
given(this.tracer.currentSpan()).willReturn(span);
107+
TraceContext traceContext = mock(TraceContext.class);
108+
given(span.context()).willReturn(traceContext);
109+
assertThat(this.spanContextSupplier.getSpanId()).isNull();
110+
}
111+
112+
@Test
113+
void whenCurrentSpanHasNoTraceIdThenTraceIdIsFNull() {
114+
Span span = mock(Span.class);
115+
given(this.tracer.currentSpan()).willReturn(span);
116+
TraceContext traceContext = mock(TraceContext.class);
117+
given(span.context()).willReturn(traceContext);
118+
assertThat(this.spanContextSupplier.getTraceId()).isNull();
119+
}
120+
121+
@Test
122+
void whenCurrentSpanIsSampledThenSampledIsTrue() {
123+
Span span = mock(Span.class);
124+
given(this.tracer.currentSpan()).willReturn(span);
125+
TraceContext traceContext = mock(TraceContext.class);
126+
given(traceContext.sampled()).willReturn(true);
127+
given(span.context()).willReturn(traceContext);
128+
assertThat(this.spanContextSupplier.isSampled()).isTrue();
129+
}
130+
131+
@Test
132+
void whenCurrentSpanIsNotSampledThenSampledIsFalse() {
133+
Span span = mock(Span.class);
134+
given(this.tracer.currentSpan()).willReturn(span);
135+
TraceContext traceContext = mock(TraceContext.class);
136+
given(traceContext.sampled()).willReturn(false);
137+
given(span.context()).willReturn(traceContext);
138+
assertThat(this.spanContextSupplier.isSampled()).isFalse();
139+
}
140+
141+
@Test
142+
void whenCurrentSpanHasDeferredSamplingThenSampledIsFalse() {
143+
Span span = mock(Span.class);
144+
given(this.tracer.currentSpan()).willReturn(span);
145+
TraceContext traceContext = mock(TraceContext.class);
146+
given(traceContext.sampled()).willReturn(null);
147+
given(span.context()).willReturn(traceContext);
148+
assertThat(this.spanContextSupplier.isSampled()).isFalse();
149+
}
150+
151+
}

0 commit comments

Comments
 (0)