Skip to content

Commit

Permalink
Prefix TraceID/SpanID.String() with zeroes (#533)
Browse files Browse the repository at this point in the history
* 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 <vogel@anapaya.net>

* SpanID.String prefix with zeroes

Also test parsing back works.

Signed-off-by: Lukas Vogel <vogel@anapaya.net>

* fix tests?

how do I run this thing locally?

Signed-off-by: Lukas Vogel <vogel@anapaya.net>

* fix tests!

Signed-off-by: Lukas Vogel <vogel@anapaya.net>
  • Loading branch information
lukedirtwalker committed Sep 12, 2020
1 parent 4b20232 commit b1fe7fe
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
6 changes: 3 additions & 3 deletions span_context.go
Expand Up @@ -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
Expand Down Expand Up @@ -379,7 +379,7 @@ func (t TraceID) IsValid() bool {
// ------- SpanID -------

func (s SpanID) String() string {
return fmt.Sprintf("%x", uint64(s))
return fmt.Sprintf("%016x", uint64(s))
}

// SpanIDFromString creates a SpanID from a hexadecimal string
Expand Down
59 changes: 56 additions & 3 deletions span_context_test.go
Expand Up @@ -15,6 +15,7 @@
package jaeger

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -63,9 +64,9 @@ func TestContextFromString(t *testing.T) {
assert.EqualValues(t, 1, ctx.spanID)
assert.EqualValues(t, 1, ctx.parentID)
assert.True(t, ctx.IsSampled())
assert.Equal(t, "ff", SpanID(255).String())
assert.Equal(t, "ff", TraceID{Low: 255}.String())
assert.Equal(t, "ff00000000000000ff", TraceID{High: 255, Low: 255}.String())
assert.Equal(t, "00000000000000ff", SpanID(255).String())
assert.Equal(t, "00000000000000ff", TraceID{Low: 255}.String())
assert.Equal(t, "00000000000000ff00000000000000ff", TraceID{High: 255, Low: 255}.String())
ctx = NewSpanContext(TraceID{High: 255, Low: 255}, SpanID(1), SpanID(1), false, nil)
assert.Equal(t, "00000000000000ff00000000000000ff:0000000000000001:0000000000000001:0", ctx.String())
}
Expand Down Expand Up @@ -166,3 +167,55 @@ 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())
parsed, err := TraceIDFromString(tc.in.String())
assert.NoError(t, err)
assert.Equal(t, tc.in, parsed)
})
}
}

func TestSpanIDString(t *testing.T) {
var tests = map[string]struct {
in SpanID
expected string
}{
"SpanID zero": {
in: 0,
expected: "0000000000000000",
},
"SpanID non zero": {
in: math.MaxUint64/16 - 405,
expected: "0ffffffffffffe6a",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expected, tc.in.String())
parsed, err := SpanIDFromString(tc.in.String())
assert.NoError(t, err)
assert.Equal(t, tc.in, parsed)
})
}
}
12 changes: 6 additions & 6 deletions zipkin/propagation_test.go
Expand Up @@ -32,31 +32,31 @@ var (

var (
rootSampledHeader = opentracing.TextMapCarrier{
"x-b3-traceid": "1",
"x-b3-traceid": "0000000000000001",
"x-b3-spanid": "2",
"x-b3-sampled": "1",
"baggage-foo": "bar",
}
nonRootSampledHeader = opentracing.TextMapCarrier{
"x-b3-traceid": "1",
"x-b3-traceid": "0000000000000001",
"x-b3-spanid": "2",
"x-b3-parentspanid": "1",
"x-b3-sampled": "1",
}
nonRootNonSampledHeader = opentracing.TextMapCarrier{
"x-b3-traceid": "1",
"x-b3-traceid": "0000000000000001",
"x-b3-spanid": "2",
"x-b3-parentspanid": "1",
"x-b3-sampled": "0",
}
rootSampledBooleanHeader = opentracing.TextMapCarrier{
"x-b3-traceid": "1",
"x-b3-traceid": "0000000000000001",
"x-b3-spanid": "2",
"x-b3-sampled": "true",
"baggage-foo": "bar",
}
nonRootSampledBooleanHeader = opentracing.TextMapCarrier{
"x-b3-traceid": "1",
"x-b3-traceid": "0000000000000001",
"x-b3-spanid": "2",
"x-b3-parentspanid": "1",
"x-b3-sampled": "true",
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestCustomBaggagePrefix(t *testing.T) {
err := propag.Inject(sc, hdr)
assert.Nil(t, err)
m := opentracing.TextMapCarrier{
"x-b3-traceid": "1",
"x-b3-traceid": "0000000000000001",
"x-b3-spanid": "2",
"x-b3-sampled": "1",
"emoji:)foo": "bar",
Expand Down

0 comments on commit b1fe7fe

Please sign in to comment.