Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

span id to hex #2062

Merged
merged 7 commits into from
Feb 22, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [BUGFIX] Apply `rate()` to bytes/s panel in tenant's dashboard. [#2081](https://github.com/grafana/tempo/pull/2081) (@mapno)
* [BUGFIX] Correctly coalesce trace level data when combining Parquet traces. [#2095](https://github.com/grafana/tempo/pull/2095) (@joe-elliott)
* [CHANGE] Update Go to 1.20 [#2079](https://github.com/grafana/tempo/pull/2079) (@scalalang2)
* [CHANGE] Removing leading zeroes in span id [#2062](https://github.com/grafana/tempo/pull/2062) (@ie-pham)

## v2.0.0 / 2023-01-31

Expand Down
2 changes: 1 addition & 1 deletion pkg/traceql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (e *Engine) asTraceSearchMetadata(spanset Spanset) *tempopb.TraceSearchMeta

for _, span := range spanset.Spans {
tempopbSpan := &tempopb.Span{
SpanID: util.TraceIDToHexString(span.ID),
SpanID: util.SpanIDToHexString(span.ID),
StartTimeUnixNano: span.StartTimeUnixNanos,
DurationNanos: span.EndtimeUnixNanos - span.StartTimeUnixNanos,
Attributes: nil,
Expand Down
6 changes: 3 additions & 3 deletions pkg/traceql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestEngine_Execute(t *testing.T) {
SpanSet: &tempopb.SpanSet{
Spans: []*tempopb.Span{
{
SpanID: "2",
SpanID: "0000000000000002",
StartTimeUnixNano: uint64(now.UnixNano()),
DurationNanos: 100_000_000,
Attributes: []*v1.KeyValue{
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestEngine_asTraceSearchMetadata(t *testing.T) {
Matched: 2,
Spans: []*tempopb.Span{
{
SpanID: util.TraceIDToHexString(spanID1),
SpanID: util.SpanIDToHexString(spanID1),
Name: "HTTP GET",
StartTimeUnixNano: uint64(now.UnixNano()),
DurationNanos: 10_000_000_000,
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestEngine_asTraceSearchMetadata(t *testing.T) {
},
},
{
SpanID: util.TraceIDToHexString(spanID2),
SpanID: util.SpanIDToHexString(spanID2),
StartTimeUnixNano: uint64(now.Add(2 * time.Second).UnixNano()),
DurationNanos: 18_000_000_000,
Attributes: nil,
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/traceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func TraceIDToHexString(byteID []byte) string {
return id
}

// SpanIDToHexString converts a span ID to its string representation and WITHOUT removing any leading zeros.
// If the id is < 16, left pad with 0s
func SpanIDToHexString(byteID []byte) string {
id := hex.EncodeToString(byteID)
id = strings.TrimLeft(id, "0")
return fmt.Sprintf("%016s", id)
}

// EqualHexStringTraceIDs compares two trace ID strings and compares the
// resulting bytes after padding. Returns true unless there is a reason not
// to.
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/traceid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ func TestTraceIDToHexString(t *testing.T) {
}
}

func TestSpanIDToHexString(t *testing.T) {
tc := []struct {
byteID []byte
spanID string
}{
{
byteID: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12},
spanID: "0000000000000012",
},
{
byteID: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef},
spanID: "1234567890abcdef", // 64 bit
},
{
byteID: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0xa0},
spanID: "00000000000012a0", // trailing zero
},
{
byteID: []byte{0x12, 0xa0},
spanID: "00000000000012a0", // less than 64 bytes
},
{
byteID: []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef},
spanID: "1234567890abcdef1234567890abcdef", // 128 bit
},
{
byteID: []byte{0x00, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef},
spanID: "34567890abcdef1234567890abcdef", // 128 bit with leading zeroes
},
}

for _, tt := range tc {
t.Run(tt.spanID, func(t *testing.T) {
actual := SpanIDToHexString(tt.byteID)

assert.Equal(t, tt.spanID, actual)
})
}
}

func TestEqualHexStringTraceIDs(t *testing.T) {
a := "82f6471b46d25e23418a0a99d4c2cda"
b := "082f6471b46d25e23418a0a99d4c2cda"
Expand Down