From fcb9b2a36230684e8b2f6c214d3672ec5af278a8 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 18 Apr 2022 11:18:26 +0100 Subject: [PATCH] Special-case extract from ot.HTTPHeadersCarrier Values stored in http.Header get title-cased, i.e. `traceparent` will turn into `Traceparent`. Since HTTPHeadersCarrier.ForeachKey does not undo this change, special-case that one type with a different wrapper that will allow the value to be fetched. Signed-off-by: Bryan Boreham --- bridge/opentracing/bridge.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bridge/opentracing/bridge.go b/bridge/opentracing/bridge.go index c759f68f672..a101e711dbf 100644 --- a/bridge/opentracing/bridge.go +++ b/bridge/opentracing/bridge.go @@ -701,14 +701,20 @@ func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.Span if builtinFormat, ok := format.(ot.BuiltinFormat); !ok || builtinFormat != ot.HTTPHeaders { return nil, ot.ErrUnsupportedFormat } - // If carrier implements the required interface directly, use that - tmCarrier, ok := carrier.(propagation.TextMapCarrier) - if !ok { - tmReader, ok := carrier.(ot.TextMapReader) // otherwise see if we can wrap it - if !ok { - return nil, ot.ErrInvalidCarrier - } - tmCarrier = textMapAdapter{r: tmReader} + var tmCarrier propagation.TextMapCarrier + switch typedCarrier := carrier.(type) { + case propagation.TextMapCarrier: + // If carrier implements the required interface directly, use that. + tmCarrier = typedCarrier + case ot.HTTPHeadersCarrier: + // HTTPHeadersCarrier requires special wrapping, because Set() title-cases the key. + header := http.Header(typedCarrier) + tmCarrier = propagation.HeaderCarrier(header) + case ot.TextMapReader: + // We can wrap anything corresponding to TextMapReader. + tmCarrier = textMapAdapter{r: typedCarrier} + default: + return nil, ot.ErrInvalidCarrier } ctx := t.getPropagator().Extract(context.Background(), tmCarrier) baggage := baggage.FromContext(ctx)