Skip to content

Commit

Permalink
Merge pull request #23 from boyand/case_sensitive_propagation
Browse files Browse the repository at this point in the history
Handle case-sensitive pre-existing headers
  • Loading branch information
pglombardo authored Apr 17, 2017
2 parents 07d34de + ce27cd4 commit 48d8f2e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
38 changes: 34 additions & 4 deletions propagation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,47 @@ func (r *textMapPropagator) inject(spanContext ot.SpanContext, opaqueCarrier int
return ot.ErrInvalidSpanContext
}

roCarrier, ok := opaqueCarrier.(ot.TextMapReader)
if !ok {
return ot.ErrInvalidCarrier
}

// Handle pre-existing case-sensitive keys
var (
exstFieldT = FieldT
exstFieldS = FieldS
exstFieldL = FieldL
exstFieldB = FieldB
)

roCarrier.ForeachKey(func(k, v string) error {
switch strings.ToLower(k) {
case FieldT:
exstFieldT = k
case FieldS:
exstFieldS = k
case FieldL:
exstFieldL = k
default:
if strings.HasPrefix(strings.ToLower(k), FieldB) {
exstFieldB = string([]rune(k)[0:len(FieldB)])
}
}

return nil
})

carrier, ok := opaqueCarrier.(ot.TextMapWriter)
if !ok {
return ot.ErrInvalidCarrier
}

carrier.Set(FieldT, strconv.FormatUint(sc.TraceID, 16))
carrier.Set(FieldS, strconv.FormatUint(sc.SpanID, 16))
carrier.Set(FieldL, strconv.Itoa(1))
carrier.Set(exstFieldT, strconv.FormatUint(sc.TraceID, 16))
carrier.Set(exstFieldS, strconv.FormatUint(sc.SpanID, 16))
carrier.Set(exstFieldL, strconv.Itoa(1))

for k, v := range sc.Baggage {
carrier.Set(FieldB+k, v)
carrier.Set(exstFieldB+k, v)
}

return nil
Expand Down
61 changes: 61 additions & 0 deletions propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/instana/golang-sensor"
bt "github.com/opentracing/basictracer-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
)

func TestSpanPropagator(t *testing.T) {
Expand Down Expand Up @@ -73,3 +74,63 @@ func TestSpanPropagator(t *testing.T) {
}
}
}

func TestCaseSensitiveHeaderPropagation(t *testing.T) {
var (
op = "test"
spanParentIdBase64 = uint64(4884)
spanParentIdString = "1314"
)

recorder := bt.NewInMemoryRecorder()
tracer := instana.NewTracerWithEverything(&instana.Options{}, recorder)

// Simulate an existing root span
metadata := make(map[string]string)
metadata["X-Instana-T"] = spanParentIdString
metadata["X-Instana-S"] = spanParentIdString
metadata["X-Instana-L"] = "1"
metadata["X-Instana-B-Foo"] = "bar"

tmc1 := opentracing.TextMapCarrier(metadata)
tmc2 := opentracing.TextMapCarrier(make(map[string]string))

for k, v := range tmc1 {
tmc2[k] = v
}

tests := []struct {
typ, carrier interface{}
}{
{opentracing.HTTPHeaders, tmc1},
{opentracing.TextMap, tmc2},
}

for i, test := range tests {
// Extract the existing context
injectedContext, err := tracer.Extract(test.typ, test.carrier)
if err != nil {
t.Fatalf("%d: %v", i, err)
}
// Start a new child span and overwrite the baggage key
child := tracer.StartSpan(
op,
opentracing.ChildOf(injectedContext))
child.SetBaggageItem("foo", "baz")

// Inject the context into the metadata
if err := tracer.Inject(child.Context(), test.typ, test.carrier); err != nil {
t.Fatalf("%d: %v", i, err)
}

child.Finish()
assert.Equal(t, child.BaggageItem("foo"), "baz")

}

for _, s := range recorder.GetSpans() {
assert.Equal(t, spanParentIdBase64, s.ParentSpanID)
assert.NotEqual(t, spanParentIdBase64, s.Context.SpanID)
}

}

0 comments on commit 48d8f2e

Please sign in to comment.