Skip to content

Commit

Permalink
[v2] Fix the special case for jaeger format traceid extraction (#340)
Browse files Browse the repository at this point in the history
* Fix the special case for jaeger format traceid extraction being overridden regression introduced with #262

* Add unit test for Jaeger trace id parsing fix
  • Loading branch information
nvx authored Sep 17, 2020
1 parent 88c8444 commit 2a8b5ab
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
31 changes: 17 additions & 14 deletions interceptors/tracing/id_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,6 @@ type tagsCarrier struct {

func (t *tagsCarrier) Set(key, val string) {
key = strings.ToLower(key)
if strings.Contains(key, "traceid") {
t.Tags.Set(TagTraceId, val) // this will most likely be base-16 (hex) encoded
}

if strings.Contains(key, "spanid") && !strings.Contains(strings.ToLower(key), "parent") {
t.Tags.Set(TagSpanId, val) // this will most likely be base-16 (hex) encoded
}

if strings.Contains(key, "sampled") {
switch val {
case "true", "false":
t.Tags.Set(TagSampled, val)
}
}

if key == t.traceHeaderName {
parts := strings.Split(val, ":")
Expand All @@ -67,6 +53,23 @@ func (t *tagsCarrier) Set(key, val string) {
} else {
t.Tags.Set(TagSampled, "false")
}

return
}
}

if strings.Contains(key, "traceid") {
t.Tags.Set(TagTraceId, val) // this will most likely be base-16 (hex) encoded
}

if strings.Contains(key, "spanid") && !strings.Contains(strings.ToLower(key), "parent") {
t.Tags.Set(TagSpanId, val) // this will most likely be base-16 (hex) encoded
}

if strings.Contains(key, "sampled") {
switch val {
case "true", "false":
t.Tags.Set(TagSampled, val)
}
}

Expand Down
32 changes: 32 additions & 0 deletions interceptors/tracing/id_extract_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tracing

import (
"fmt"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/tags"
"github.com/stretchr/testify/assert"
"testing"
)

func TestTagsCarrier_Set_JaegerTraceFormat(t *testing.T) {
var (
fakeTraceSampled = 1
fakeInboundTraceId = "deadbeef"
fakeInboundSpanId = "c0decafe"
traceHeaderName = "uber-trace-id"
)

traceHeaderValue := fmt.Sprintf("%s:%s:%s:%d", fakeInboundTraceId, fakeInboundSpanId, fakeInboundSpanId, fakeTraceSampled)

c := &tagsCarrier{
Tags: tags.NewTags(),
traceHeaderName: traceHeaderName,
}

c.Set(traceHeaderName, traceHeaderValue)

assert.EqualValues(t, map[string]string{
TagTraceId: fakeInboundTraceId,
TagSpanId: fakeInboundSpanId,
TagSampled: "true",
}, c.Tags.Values())
}

0 comments on commit 2a8b5ab

Please sign in to comment.