From 14e3bff0f954c2c3df4f997acd45d8c9dd1769f3 Mon Sep 17 00:00:00 2001 From: Lukas Vogel Date: Fri, 11 Sep 2020 22:55:55 +0200 Subject: [PATCH] TraceID.String prefix with zeroes The wire encoding of the TraceID uses zero prefixes (#472). The JaegerUI also uses zero prefixes (since 1.16). So it makes sense that the Stringer of the TraceID also does this. Resolves #532 Signed-off-by: Lukas Vogel --- span_context.go | 4 ++-- span_context_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/span_context.go b/span_context.go index 03aa99a0..fb2ae549 100644 --- a/span_context.go +++ b/span_context.go @@ -344,9 +344,9 @@ func (c *SpanContext) isDebugIDContainerOnly() bool { func (t TraceID) String() string { if t.High == 0 { - return fmt.Sprintf("%x", t.Low) + return fmt.Sprintf("%016x", t.Low) } - return fmt.Sprintf("%x%016x", t.High, t.Low) + return fmt.Sprintf("%016x%016x", t.High, t.Low) } // TraceIDFromString creates a TraceID from a hexadecimal string diff --git a/span_context_test.go b/span_context_test.go index 51b97a00..22cdbd10 100644 --- a/span_context_test.go +++ b/span_context_test.go @@ -15,6 +15,7 @@ package jaeger import ( + "math" "testing" "github.com/stretchr/testify/assert" @@ -166,3 +167,28 @@ func TestSpanContext_CopyFrom(t *testing.T) { assert.Equal(t, ctx, ctx2) assert.Equal(t, "y", ctx2.baggage["x"]) } + +func TestTraceIDString(t *testing.T) { + var tests = map[string]struct { + in TraceID + expected string + }{ + "Empty TraceID": { + in: TraceID{}, + expected: "0000000000000000", + }, + "TraceID low only": { + in: TraceID{Low: math.MaxUint64/16 - 405}, + expected: "0ffffffffffffe6a", + }, + "TraceID low and high": { + in: TraceID{High: math.MaxUint64 / 16, Low: math.MaxUint64/16 - 405}, + expected: "0fffffffffffffff0ffffffffffffe6a", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + assert.Equal(t, tc.expected, tc.in.String()) + }) + } +}