Skip to content

Context has no baggage at SpanProcessor #1162

@Thiago-Alessandro

Description

@Thiago-Alessandro

Hi, im using micrometer tracing bridge for otel in a observation library and In my application i would like to get the baggage and set its values as span attributes for the span.kind SERVER. The best approach i can think of is setting a SpanProcessor, getting the baggage from the context on the onStart method and setting it on the span attributes, but at this point, there is no baggage in the context.

While debbuging, i notice the main point is on OtelSpanBuilder, in the start() method, i have parentTraceContext set, with otelContext containing the baggage, but when spanBuilder.setParent runs, the provided context parameter from OtelTraceContext.toOtelContext comes without the baggage. Then spanBuilder.startSpan() runs and triggers SpanProcessor to run onStart with a context that has no baggage in it. and latter in the start() method of OtelSpanBuilder it is returned the span with the context containing the baggage (SpanFromSpanContext).

OtelSpanBuilder

public Span start() {
        SpanBuilder spanBuilder = this.tracer.spanBuilder(StringUtils.isNotEmpty(this.name) ? this.name : "");
        if (this.parentTraceContext != null) {
            spanBuilder.setParent(OtelTraceContext.toOtelContext(this.parentTraceContext)); // here
        }

        if (this.noParent) {
            spanBuilder.setNoParent();
        }

        spanBuilder.setAllAttributes(this.attributes.build());
        spanBuilder.setSpanKind(this.spanKind);
        if (this.startTimestampUnit != null) {
            spanBuilder.setStartTimestamp(this.startTimestamp, this.startTimestampUnit);
        }

        this.links.forEach((e) -> spanBuilder.addLink((SpanContext)e.getKey(), (Attributes)e.getValue()));
        io.opentelemetry.api.trace.Span span = spanBuilder.startSpan(); // triggers spanProcessor.onstart
        if (this.error != null) {
            span.recordException(this.error);
        }

        List var10000 = this.annotations;
        Objects.requireNonNull(span);
        var10000.forEach(span::addEvent);
        return this.parentTraceContext != null ? OtelSpan.fromOtel(new SpanFromSpanContext(span, span.getSpanContext(), (OtelTraceContext)this.parentTraceContext)) : OtelSpan.fromOtel(span); //SpanFromSpanContext returns span with context containing the baggage
    }

OtelTraceContext

public static Context toOtelContext(TraceContext context) {
        if (context instanceof OtelTraceContext) {
            Span span = ((OtelTraceContext)context).span;
            return span != null ? span.storeInContext(Context.current()) : Context.current().with(Span.wrap(((OtelTraceContext)context).delegate)); // span.storeInContext returns a context with no baggage because Context.current has no baggage
        } else {
            return Context.current();
        }
    }

How can i get over this problem? why is the span started while the right context is still not set? is there any other easier way to set the baggage as span attributes?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions