Skip to content

Commit

Permalink
Uses child span when extracted via a propagator; fixes gh-159
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Mar 10, 2023
1 parent 66ffffa commit f4274f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class OtelSpanInScope implements Tracer.SpanInScope {
}

private Scope storedContext(io.opentelemetry.api.trace.Span otelSpan) {
if (otelSpan instanceof SpanFromSpanContext) {
SpanFromSpanContext spanFromSpanContext = (SpanFromSpanContext) otelSpan;
return spanFromSpanContext.otelTraceContext.context().makeCurrent();
}
if (this.span == null || this.span.context() == null || this.span.context().context() == null) {
return otelSpan.makeCurrent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package io.micrometer.tracing.otel.bridge;

import io.micrometer.tracing.BaggageInScope;
import io.micrometer.tracing.Span;
import io.micrometer.tracing.*;
import io.micrometer.tracing.otel.propagation.BaggageTextMapPropagator;
import io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
Expand All @@ -31,6 +30,9 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;

class OtelPropagatorTests {

Expand Down Expand Up @@ -106,4 +108,29 @@ void should_propagate_context_with_baggage_only_as_field() {
}
}

@Test
void should_use_created_child_context_in_scope_instead_of_parent() {
OtelBaggageManager baggageManager = new OtelBaggageManager(otelCurrentTraceContext, Collections.emptyList(),
Collections.emptyList());
OtelTracer tracer = new OtelTracer(otelTracer, otelCurrentTraceContext, Function.identity()::apply,
baggageManager);

Map<String, String> carrier = new HashMap<>();
carrier.put("traceparent", "00-3e425f2373d89640bde06e8285e7bf88-9a5fdefae3abb440-00");

Span extracted = otelPropagator.extract(carrier, Map::get).start();
String expectedSpanId = extracted.context().spanId();

try (Tracer.SpanInScope ignored = tracer.withSpan(extracted)) {
assertThat(tracer.currentSpan()).extracting(Span::context).returns(expectedSpanId, TraceContext::spanId)
.returns("3e425f2373d89640bde06e8285e7bf88", TraceContext::traceId)
.returns("9a5fdefae3abb440", TraceContext::parentId);

assertThat(tracer.currentTraceContext()).isNotNull().extracting(CurrentTraceContext::context).isNotNull()
.returns(expectedSpanId, TraceContext::spanId)
.returns("3e425f2373d89640bde06e8285e7bf88", TraceContext::traceId)
.returns("9a5fdefae3abb440", TraceContext::parentId);
}
}

}

0 comments on commit f4274f1

Please sign in to comment.