Skip to content

Commit

Permalink
Merge pull request #155 from openzipkin-contrib/fixes_extraction_for_…
Browse files Browse the repository at this point in the history
…text_map

fix(propagation): fixes propagation for opentracing.TextMap.
  • Loading branch information
jcchavezs committed Dec 2, 2019
2 parents 2869220 + d462f4a commit 223664c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
6 changes: 5 additions & 1 deletion propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package zipkintracer

import (
"net/http"
"strings"

opentracing "github.com/opentracing/opentracing-go"
"github.com/openzipkin/zipkin-go/model"
Expand Down Expand Up @@ -108,7 +109,10 @@ func (p *textMapPropagator) Extract(
if carrier, ok := opaqueCarrier.(opentracing.TextMapReader); ok {
m := make(b3.Map)
carrier.ForeachKey(func(key string, val string) error {
m[key] = val
// no matter the format of the B3 headers, they will be retrieved
// using the standard lowercase format e.g. x-b3-traceid. See
// https://github.com/openzipkin/zipkin-go/blob/master/propagation/b3/shared.go
m[strings.ToLower(key)] = val
return nil
})
sc, err := m.Extract()
Expand Down
43 changes: 42 additions & 1 deletion propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package zipkintracer_test

import (
"net/http"
stdHTTP "net/http"
"reflect"
"testing"
Expand Down Expand Up @@ -249,6 +250,47 @@ func TestHTTPExtractInvalidParentIDError(t *testing.T) {

}

// httpHeadersCarrier implements opentracing.TextMapReader
type httpHeadersCarrier http.Header

func (c httpHeadersCarrier) Set(key, val string) {
h := http.Header(c)
h.Set(key, val)
}

func (c httpHeadersCarrier) ForeachKey(handler func(key, val string) error) error {
for k, vals := range c {
for _, v := range vals {
if err := handler(k, v); err != nil {
return err
}
}
}
return nil
}

func TestTextMapReaderIsCaseInsensitive(t *testing.T) {
tracer := zipkintracer.Wrap(nil)
c := stdHTTP.Header{}
c.Set("X-B3-Traceid", "1")
c.Set("X-B3-Spanid", "2")

sc, err := tracer.Extract(opentracing.HTTPHeaders, httpHeadersCarrier(c))
if err != nil {
t.Errorf("unexpected error: %v", err)
}

zsc, _ := sc.(zipkintracer.SpanContext)
if want, have := "0000000000000001", zsc.TraceID.String(); want != have {
t.Errorf("Extract Error want %+v, have %+v", want, have)
}

if want, have := "0000000000000002", zsc.ID.String(); want != have {
t.Errorf("Extract Error want %+v, have %+v", want, have)
}

}

func TestHTTPInjectEmptyContextError(t *testing.T) {
tracer := zipkintracer.Wrap(nil)
err := tracer.Inject(zipkintracer.SpanContext{}, opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier{})
Expand Down Expand Up @@ -368,7 +410,6 @@ func TestTextMapCarrier(t *testing.T) {
})

otSC, err := tracer.Extract(opentracing.TextMap, otMap)

if err != nil {
t.Errorf("[%d] Unexpected Extract failure %v", injectOption, err)
}
Expand Down

0 comments on commit 223664c

Please sign in to comment.