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()) + }) + } +}