From 1c695d0f542547bab70677834ac9787169404268 Mon Sep 17 00:00:00 2001 From: Tiernan Messmer Date: Thu, 17 Sep 2020 13:47:20 +1000 Subject: [PATCH 1/2] Fix the special case for jaeger format traceid extraction being overridden regression introduced with #262 --- interceptors/tracing/id_extract.go | 31 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/interceptors/tracing/id_extract.go b/interceptors/tracing/id_extract.go index abdb9a8e4..a0e482cb0 100644 --- a/interceptors/tracing/id_extract.go +++ b/interceptors/tracing/id_extract.go @@ -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, ":") @@ -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) } } From 7d61e88f912ce3c4906b9176fd39f8721e80fb35 Mon Sep 17 00:00:00 2001 From: Tiernan Messmer Date: Thu, 17 Sep 2020 20:35:20 +1000 Subject: [PATCH 2/2] Add unit test for Jaeger trace id parsing fix --- interceptors/tracing/id_extract_test.go | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 interceptors/tracing/id_extract_test.go diff --git a/interceptors/tracing/id_extract_test.go b/interceptors/tracing/id_extract_test.go new file mode 100644 index 000000000..f0bca9459 --- /dev/null +++ b/interceptors/tracing/id_extract_test.go @@ -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()) +}