Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

TraceID.String prefix with zeroes #533

Merged
merged 4 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions span_context.go
Original file line number Diff line number Diff line change
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also do it in L382 for span ID

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Should I squash? Or will you do that on pre-merge?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we squash on merge

}
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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