Skip to content

Commit

Permalink
Honors parent trace context even if there was no span (#722)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
marcingrzejszczak authored May 22, 2024
1 parent bdda93a commit fedae62
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/
package io.micrometer.tracing.otel.bridge;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import io.micrometer.common.lang.Nullable;
import io.micrometer.tracing.TraceContext;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadableSpan;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
* OpenTelemetry implementation of a {@link TraceContext}.
*
Expand Down Expand Up @@ -87,6 +87,9 @@ public static Context toOtelContext(TraceContext context) {
if (span != null) {
return span.storeInContext(Context.current());
}
else {
return Context.current().with(Span.wrap(((OtelTraceContext) context).delegate));
}
}
return Context.current();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.micrometer.tracing.Link;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.TraceContext;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.extension.trace.propagation.B3Propagator;
Expand Down Expand Up @@ -47,6 +48,11 @@ class OtelSpanBuilderTests {

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

OtelCurrentTraceContext otelCurrentTraceContext = new OtelCurrentTraceContext();

OtelTracer tracer = new OtelTracer(otelTracer, otelCurrentTraceContext, event -> {
});

@Test
void should_set_child_span_when_using_builders() {

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

@Test
void should_honor_parent_context_using_tracecontextbuilder() {
Span foo = tracer.spanBuilder().name("foo").start();

TraceContext parentCtx = tracer.traceContextBuilder()
.traceId(foo.context().traceId())
.spanId(foo.context().spanId())
.sampled(foo.context().sampled())
.build();

Span span = tracer.spanBuilder().setParent(parentCtx).name("test-span").start();

then(span.context().traceId()).isEqualTo(foo.context().traceId());
}

private Map<String, Object> tags() {
Map<String, Object> map = new HashMap<>();
map.put("tag1", "value1");
Expand Down

0 comments on commit fedae62

Please sign in to comment.