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

Support odd length strings and error on value too long #605

Merged
merged 2 commits into from
Mar 22, 2021
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 @@ -14,6 +14,7 @@
* [BUGFIX] Fixes where ingester may leave file open [#570](https://github.com/grafana/tempo/pull/570)
* [BUGFIX] Fixes a bug where some blocks were not searched due to query sharding and randomness in blocklist poll. [#583](https://github.com/grafana/tempo/pull/583)
* [BUGFIX] Fixes issue where wal was deleted before successful flush and adds exponential backoff for flush errors [#593](https://github.com/grafana/tempo/pull/593)
* [BUGFIX] Fixes issue where Tempo would not parse odd length trace ids [#605](https://github.com/grafana/tempo/pull/605)

## v0.6.0

Expand Down
15 changes: 0 additions & 15 deletions pkg/util/hash.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package util

import (
"encoding/hex"
"hash/fnv"
)

Expand All @@ -19,17 +18,3 @@ func TokenForTraceID(b []byte) uint32 {
_, _ = h.Write(b)
return h.Sum32()
}

func HexStringToTraceID(id string) ([]byte, error) {
byteID, err := hex.DecodeString(id)
if err != nil {
return nil, err
}

size := len(byteID)
if size < 16 {
byteID = append(make([]byte, 16-size), byteID...)
}

return byteID, nil
}
27 changes: 26 additions & 1 deletion pkg/util/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package util

import (
"encoding/hex"
"errors"
"fmt"
"net/http"

Expand All @@ -21,10 +23,33 @@ func ParseTraceID(r *http.Request) ([]byte, error) {
return nil, fmt.Errorf("please provide a traceID")
}

byteID, err := HexStringToTraceID(traceID)
byteID, err := hexStringToTraceID(traceID)
if err != nil {
return nil, err
}

return byteID, nil
}

func hexStringToTraceID(id string) ([]byte, error) {
// the encoding/hex package does not like odd length strings.
// just append a bit here
if len(id)%2 == 1 {
id = "0" + id
}

byteID, err := hex.DecodeString(id)
if err != nil {
return nil, err
}

size := len(byteID)
if size > 16 {
return nil, errors.New("trace ids can't be larger than 128 bits")
}
if size < 16 {
byteID = append(make([]byte, 16-size), byteID...)
}

return byteID, nil
}
53 changes: 53 additions & 0 deletions pkg/util/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestHexStringToTraceID(t *testing.T) {

tc := []struct {
id string
expected []byte
expectError bool
}{
{