Skip to content

Commit ed9960d

Browse files
Honors parent trace context even if there was no span (#722)
- without this change, for OTel, we always look at spans when talking about setting a parent on a span. However it is completely valid to pass just trace context data (span id, trace id, sampling flag etc.) - with this change, for OTel, we honor that setting and will look at both span and span trace context fixes gh-698
1 parent acac926 commit ed9960d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
*/
1616
package io.micrometer.tracing.otel.bridge;
1717

18-
import java.util.Objects;
19-
import java.util.concurrent.atomic.AtomicReference;
20-
2118
import io.micrometer.common.lang.Nullable;
2219
import io.micrometer.tracing.TraceContext;
2320
import io.opentelemetry.api.trace.Span;
2421
import io.opentelemetry.api.trace.SpanContext;
2522
import io.opentelemetry.context.Context;
2623
import io.opentelemetry.sdk.trace.ReadableSpan;
2724

25+
import java.util.Objects;
26+
import java.util.concurrent.atomic.AtomicReference;
27+
2828
/**
2929
* OpenTelemetry implementation of a {@link TraceContext}.
3030
*
@@ -87,6 +87,9 @@ public static Context toOtelContext(TraceContext context) {
8787
if (span != null) {
8888
return span.storeInContext(Context.current());
8989
}
90+
else {
91+
return Context.current().with(Span.wrap(((OtelTraceContext) context).delegate));
92+
}
9093
}
9194
return Context.current();
9295
}

micrometer-tracing-bridges/micrometer-tracing-bridge-otel/src/test/java/io/micrometer/tracing/otel/bridge/OtelSpanBuilderTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.micrometer.tracing.Link;
1919
import io.micrometer.tracing.Span;
20+
import io.micrometer.tracing.TraceContext;
2021
import io.opentelemetry.api.common.AttributeKey;
2122
import io.opentelemetry.context.propagation.ContextPropagators;
2223
import io.opentelemetry.extension.trace.propagation.B3Propagator;
@@ -47,6 +48,11 @@ class OtelSpanBuilderTests {
4748

4849
io.opentelemetry.api.trace.Tracer otelTracer = openTelemetrySdk.getTracer("io.micrometer.micrometer-tracing");
4950

51+
OtelCurrentTraceContext otelCurrentTraceContext = new OtelCurrentTraceContext();
52+
53+
OtelTracer tracer = new OtelTracer(otelTracer, otelCurrentTraceContext, event -> {
54+
});
55+
5056
@Test
5157
void should_set_child_span_when_using_builders() {
5258

@@ -98,6 +104,21 @@ void should_set_non_string_tags() {
98104
then(poll.getAttributes().get(AttributeKey.booleanKey("boolean"))).isTrue();
99105
}
100106

107+
@Test
108+
void should_honor_parent_context_using_tracecontextbuilder() {
109+
Span foo = tracer.spanBuilder().name("foo").start();
110+
111+
TraceContext parentCtx = tracer.traceContextBuilder()
112+
.traceId(foo.context().traceId())
113+
.spanId(foo.context().spanId())
114+
.sampled(foo.context().sampled())
115+
.build();
116+
117+
Span span = tracer.spanBuilder().setParent(parentCtx).name("test-span").start();
118+
119+
then(span.context().traceId()).isEqualTo(foo.context().traceId());
120+
}
121+
101122
private Map<String, Object> tags() {
102123
Map<String, Object> map = new HashMap<>();
103124
map.put("tag1", "value1");

0 commit comments

Comments
 (0)