Skip to content

Commit

Permalink
fix: retrieve lost code due to merge. test: support for ot.HTTPHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
tttoad committed May 25, 2022
1 parent f76da80 commit d25e080
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
35 changes: 27 additions & 8 deletions bridge/opentracing/bridge.go
Expand Up @@ -635,7 +635,7 @@ func (s fakeSpan) SpanContext() trace.SpanContext {
// Inject is a part of the implementation of the OpenTracing Tracer
// interface.
//
// Currently only the HTTPHeaders format is supported.
// Currently only the HTTPHeaders and TextMap format are supported.
func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier interface{}) error {
bridgeSC, ok := sm.(*bridgeSpanContext)
if !ok {
Expand All @@ -644,28 +644,47 @@ func (t *BridgeTracer) Inject(sm ot.SpanContext, format interface{}, carrier int
if !bridgeSC.otelSpanContext.IsValid() {
return ot.ErrInvalidSpanContext
}
if builtinFormat, ok := format.(ot.BuiltinFormat); !ok || builtinFormat != ot.HTTPHeaders {

builtinFormat, ok := format.(ot.BuiltinFormat)
if !ok {
return ot.ErrUnsupportedFormat
}
hhcarrier, ok := carrier.(ot.HTTPHeadersCarrier)
if !ok {
return ot.ErrInvalidCarrier

var textCarrier propagation.TextMapCarrier

switch builtinFormat {
case ot.HTTPHeaders:
hhcarrier, ok := carrier.(ot.HTTPHeadersCarrier)
if !ok {
return ot.ErrInvalidCarrier
}

textCarrier = propagation.HeaderCarrier(hhcarrier)
case ot.TextMap:
if textCarrier, ok = carrier.(propagation.TextMapCarrier); !ok {
var err error
if textCarrier, err = newTextMapWrapperForInject(carrier); err != nil {
return err
}
}
default:
return ot.ErrUnsupportedFormat
}
header := http.Header(hhcarrier)

fs := fakeSpan{
Span: noopSpan,
sc: bridgeSC.otelSpanContext,
}
ctx := trace.ContextWithSpan(context.Background(), fs)
ctx = baggage.ContextWithBaggage(ctx, bridgeSC.bag)
t.getPropagator().Inject(ctx, propagation.HeaderCarrier(header))
t.getPropagator().Inject(ctx, textCarrier)
return nil
}

// Extract is a part of the implementation of the OpenTracing Tracer
// interface.
//
// Currently only the HTTPHeaders format is supported.
// Currently only the HTTPHeaders and TextMap format are supported.
func (t *BridgeTracer) Extract(format interface{}, carrier interface{}) (ot.SpanContext, error) {
builtinFormat, ok := format.(ot.BuiltinFormat)
if !ok {
Expand Down
37 changes: 11 additions & 26 deletions bridge/opentracing/bridge_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"net/http"
"strings"
"testing"
)
Expand Down Expand Up @@ -181,29 +182,6 @@ func (t *textMapCarrier) Keys() []string {
return str
}

// opentracingTextMap Implemented opentracing.TextMapReader and opentracing.TextMapWriter interface
type opentracingTextMapCarrier struct {
m map[string]string
}

func newOpentracingTextMapCarrier() *opentracingTextMapCarrier {
return &opentracingTextMapCarrier{m: map[string]string{}}
}

func (o *opentracingTextMapCarrier) Set(key, val string) {
o.m[key] = val
}

func (o *opentracingTextMapCarrier) ForeachKey(handler func(key string, val string) error) error {
for key, val := range o.m {
if err := handler(key, val); err != nil {
return err
}
}

return nil
}

// testTextMapReader only implemented opentracing.TextMapReader interface
type testTextMapReader struct {
m *map[string]string
Expand Down Expand Up @@ -241,8 +219,9 @@ func TestBridgeTracer_ExtractAndInject(t *testing.T) {
bridge.SetTextMapPropagator(new(testTextMapPropagator))

tmc := newTextCarrier()
otmc := newOpentracingTextMapCarrier()
shareMap := map[string]string{}
otTextMap := ot.TextMapCarrier{}
httpHeader := ot.HTTPHeadersCarrier(http.Header{})

testCases := []struct {
name string
Expand All @@ -259,8 +238,14 @@ func TestBridgeTracer_ExtractAndInject(t *testing.T) {
{
name: "support for opentracing.TextMapReader and opentracing.TextMapWriter",
carrierType: ot.TextMap,
extractCarrier: otmc,
injectCarrier: otmc,
extractCarrier: otTextMap,
injectCarrier: otTextMap,
},
{
name: "support for HTTPHeaders",
carrierType: ot.HTTPHeaders,
extractCarrier: httpHeader,
injectCarrier: httpHeader,
},
{
name: "support for opentracing.TextMapReader and opentracing.TextMapWriter,non-same instance",
Expand Down

0 comments on commit d25e080

Please sign in to comment.