diff --git a/bridge/opentracing/bridge.go b/bridge/opentracing/bridge.go index c20c61cb7ce..4007efe59e9 100644 --- a/bridge/opentracing/bridge.go +++ b/bridge/opentracing/bridge.go @@ -620,23 +620,43 @@ func (s fakeSpan) SpanContext() trace.SpanContext { return s.sc } -// adapt OpenTracing TextMapWriter to OpenTelemetry propagation.TextMapCarrier -type textMapWriterAdapter struct { - t ot.TextMapWriter +// adapt OpenTracing TextMapReader or TextMapWriter to OpenTelemetry propagation.TextMapCarrier +type textMapAdapter struct { + r ot.TextMapReader + w ot.TextMapWriter } -func (a textMapWriterAdapter) Get(key string) string { - // not implemented - return "" +func (a textMapAdapter) Get(key string) string { + if a.r == nil { + return "" // not implemented on writer + } + var ret string + _ = a.r.ForeachKey(func(k, v string) error { + if key == k { + ret = v + } + return nil + }) + return ret } -func (a textMapWriterAdapter) Set(key string, value string) { - a.t.Set(key, value) +func (a textMapAdapter) Set(key string, value string) { + if a.w == nil { + return // not implemented on reader + } + a.w.Set(key, value) } -func (a textMapWriterAdapter) Keys() []string { - // not implemented - return nil +func (a textMapAdapter) Keys() []string { + if a.r == nil { + return nil // not implemented on writer + } + var ret []string + _ = a.r.ForeachKey(func(k, v string) error { + ret = append(ret, k) + return nil + }) + return ret } // Inject is a part of the implementation of the OpenTracing Tracer @@ -661,7 +681,7 @@ func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier int if !ok { return ot.ErrInvalidCarrier } - tmCarrier = textMapWriterAdapter{t: tmWriter} + tmCarrier = textMapAdapter{w: tmWriter} } fs := fakeSpan{ Span: noop.Span, @@ -673,35 +693,6 @@ func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier int return nil } -// adapt OpenTracing TextMapReader to OpenTelemetry propagation.TextMapCarrier -type textMapReaderAdapter struct { - t ot.TextMapReader -} - -func (a textMapReaderAdapter) Get(key string) string { - var ret string - _ = a.t.ForeachKey(func(k, v string) error { - if key == k { - ret = v - } - return nil - }) - return ret -} - -func (a textMapReaderAdapter) Set(key string, value string) { - // not implemented -} - -func (a textMapReaderAdapter) Keys() []string { - var ret []string - _ = a.t.ForeachKey(func(k, v string) error { - ret = append(ret, k) - return nil - }) - return ret -} - // Extract is a part of the implementation of the OpenTracing Tracer // interface. // @@ -717,7 +708,7 @@ func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.Span if !ok { return nil, ot.ErrInvalidCarrier } - tmCarrier = textMapReaderAdapter{t: tmReader} + tmCarrier = textMapAdapter{r: tmReader} } ctx := t.getPropagator().Extract(context.Background(), tmCarrier) baggage := baggage.FromContext(ctx)