Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honors parent trace context even if there was no span #722

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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