Skip to content

Commit

Permalink
Merge reader and writer adapters into one
Browse files Browse the repository at this point in the history
Code is a bit shorter this way

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
  • Loading branch information
bboreham committed Aug 12, 2021
1 parent 742a9bc commit f73fe19
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions bridge/opentracing/bridge.go
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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.
//
Expand All @@ -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)
Expand Down

0 comments on commit f73fe19

Please sign in to comment.