Skip to content

Commit 06c616e

Browse files
Fixed wrong OtelTraceContext sampling and parent fields resolution
Without this change an NPE was thrown when unboxing the sampling flag. Also the parent id was resolved improperly. Lastly OTel doesn't understand a deferred sampling decision (that's why NPE was thrown, but even after fixing it sampling decision is either positive or negative). with this change we're fixing the unboxing problem plus we're directly passing the sampling decision and parent id as respones for trace context method execution (for sampling and parent id) fixes gh-911
1 parent be256d8 commit 06c616e

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

micrometer-tracing-bridges/micrometer-tracing-bridge-otel/src/main/java/io/micrometer/tracing/otel/bridge/OtelTraceContextBuilder.java

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package io.micrometer.tracing.otel.bridge;
1717

18+
import io.micrometer.common.lang.Nullable;
1819
import io.micrometer.common.util.StringUtils;
1920
import io.micrometer.tracing.TraceContext;
2021
import io.opentelemetry.api.trace.SpanContext;
@@ -35,6 +36,7 @@ public class OtelTraceContextBuilder implements TraceContext.Builder {
3536

3637
private String spanId;
3738

39+
@Nullable
3840
private Boolean sampled;
3941

4042
@Override
@@ -56,23 +58,39 @@ public TraceContext.Builder spanId(String spanId) {
5658
}
5759

5860
@Override
59-
public TraceContext.Builder sampled(Boolean sampled) {
61+
public TraceContext.Builder sampled(@Nullable Boolean sampled) {
6062
this.sampled = sampled;
6163
return this;
6264
}
6365

6466
@Override
6567
public TraceContext build() {
68+
boolean actualSampled = this.sampled != null && this.sampled;
6669
if (StringUtils.isNotEmpty(this.parentId)) {
6770
return new OtelTraceContext(
6871
SpanContext.createFromRemoteParent(this.traceId, this.spanId,
69-
this.sampled ? TraceFlags.getSampled() : TraceFlags.getDefault(), TraceState.getDefault()),
70-
null);
72+
actualSampled ? TraceFlags.getSampled() : TraceFlags.getDefault(), TraceState.getDefault()),
73+
null) {
74+
@Override
75+
public String parentId() {
76+
return parentId;
77+
}
78+
79+
@Override
80+
public Boolean sampled() {
81+
return sampled;
82+
}
83+
};
7184
}
7285
return new OtelTraceContext(
7386
SpanContext.create(this.traceId, this.spanId,
74-
this.sampled ? TraceFlags.getSampled() : TraceFlags.getDefault(), TraceState.getDefault()),
75-
null);
87+
actualSampled ? TraceFlags.getSampled() : TraceFlags.getDefault(), TraceState.getDefault()),
88+
null) {
89+
@Override
90+
public Boolean sampled() {
91+
return sampled;
92+
}
93+
};
7694
}
7795

7896
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2025 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+
package io.micrometer.tracing.otel.bridge;
17+
18+
import io.micrometer.tracing.TraceContext;
19+
import org.assertj.core.api.SoftAssertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
class OtelTraceContextBuilderTests {
23+
24+
public static final String TRACE_ID = "3e425f2373d89640bde06e8285e7bf88";
25+
26+
public static final String PARENT_SPAN_ID = "9a5fdefae3abb440";
27+
28+
public static final String SPAN_ID = "ae3abb4409a5fdef";
29+
30+
@Test
31+
void should_build_trace_context_for_non_null_sampled() {
32+
TraceContext traceContext = new OtelTraceContextBuilder().traceId(TRACE_ID)
33+
.parentId(PARENT_SPAN_ID)
34+
.spanId(SPAN_ID)
35+
.sampled(true)
36+
.build();
37+
38+
SoftAssertions.assertSoftly(softly -> {
39+
softly.assertThat(traceContext.spanId()).isEqualTo(SPAN_ID);
40+
softly.assertThat(traceContext.traceId()).isEqualTo(TRACE_ID);
41+
softly.assertThat(traceContext.parentId()).isEqualTo(PARENT_SPAN_ID);
42+
softly.assertThat(traceContext.sampled()).isTrue();
43+
});
44+
}
45+
46+
@Test
47+
void should_build_trace_context_for_null_sampled() {
48+
TraceContext traceContext = new OtelTraceContextBuilder().traceId(TRACE_ID)
49+
.parentId(PARENT_SPAN_ID)
50+
.spanId(SPAN_ID)
51+
.sampled(null)
52+
.build();
53+
54+
SoftAssertions.assertSoftly(softly -> {
55+
softly.assertThat(traceContext.spanId()).isEqualTo(SPAN_ID);
56+
softly.assertThat(traceContext.traceId()).isEqualTo(TRACE_ID);
57+
softly.assertThat(traceContext.parentId()).isEqualTo(PARENT_SPAN_ID);
58+
softly.assertThat(traceContext.sampled()).isNull();
59+
});
60+
}
61+
62+
}

micrometer-tracing/src/main/java/io/micrometer/tracing/TraceContext.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public Boolean sampled() {
7373
* @return {@code true} when sampled, {@code false} when not sampled and {@code null}
7474
* when sampling decision should be deferred
7575
*/
76+
@Nullable
7677
Boolean sampled();
7778

7879
/**
@@ -138,7 +139,7 @@ public TraceContext build() {
138139
* @param sampled if span is sampled
139140
* @return this
140141
*/
141-
TraceContext.Builder sampled(Boolean sampled);
142+
TraceContext.Builder sampled(@Nullable Boolean sampled);
142143

143144
/**
144145
* Builds the trace context.

0 commit comments

Comments
 (0)