Skip to content

OtelTracer does not set parent context when explicitly specified #698

@jamesmoessis

Description

@jamesmoessis

The following code does not work with the OtelTracer. We expect that the span has trace ID equal to the one we specified in the parent context, but it actually ends up using Context.current().

@Test
void manualparentContext() {
    TraceContext parentCtx =
            tracer.traceContextBuilder()
                    .traceId(TRACE_ID)
                    .spanId(SPAN_ID)
                    .sampled(SAMPLING_FLAG)
                    .build();

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

    assertThat(span.context().traceId()).isEqualTo(TRACE_ID);
}

This can be achieved using the OTel API in such a way that this repo does not seem to implement:

io.opentelemetry.api.trace.Tracer otelTracer;

@Test
void manualParentContextOtel() {
    SpanContext spanCtx  = SpanContext
            .createFromRemoteParent(
                    TRACE_ID,
                    SPAN_ID,
                    TraceFlags.fromByte((byte)1),
                    TraceState.getDefault());
    Context ctx = Context.current().with(io.opentelemetry.api.trace.Span.wrap(spanCtx));

    io.opentelemetry.api.trace.Span span = otelTracer
            .spanBuilder("s")
            .setParent(ctx)
            .startSpan();
    try (Scope ignored = span.makeCurrent()) {
        doStuff();
    } finally {
        span.end();
    }
}

I believe the problem is in the toOtelContext() method, and doesn't use the Span.wrap() method.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions