opentelemetry-go-contrib icon indicating copy to clipboard operation
opentelemetry-go-contrib copied to clipboard

[otelgin] Would extract Baggage from parent span be supported in the roadmap?

Open mong0520 opened this issue 1 year ago • 3 comments

Problem Statement

When parent span injects baggage, otelgin seems not parse baggage information. ( https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go#L70 )

Proposed Solution

In https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go, implement baggage.FromContext(ctx) and set it to it's span attributes.

sample code

ctx := cfg.Propagators.Extract(req.Context(), propagation.HeaderCarrier(req.Header))
ctx, span := tracer.Start(ctx, spanName, opts...)
defer span.End()
    
reqBaggage := baggage.FromContext(ctx)
span.SetAttributes(
    attribute.String("user.id", reqBaggage.Member("user.id").Value()),
    attribute.String("user.name", reqBaggage.Member("user.name").Value()),
)    

Alternatives

N/A

Prior Art

N/A

Additional Context

N/A

mong0520 avatar Sep 15 '23 04:09 mong0520

I'm not sure extracting the entire baggage into the span is the correct generic behavior. It seems it would be better to have a custom middleware on your end which would retrieve the span from the context and add the attributes you need.

dmathieu avatar Sep 15 '23 13:09 dmathieu

@dmathieu Thank you for the suggestion for the baggage. I am new to OpenTelemetry, and my understanding is that a Span is immutable and otelgin starts a span already. So my middleware to extra baggage should be prior than otelgin's middleware, otherwise there will be two spans, right? Please correct if I am wrong, thank you.

mong0520 avatar Sep 15 '23 13:09 mong0520

Spans are only immutable once ended. They can be extended with new attributes and events at any moment, until End() has been called.

dmathieu avatar Sep 15 '23 14:09 dmathieu