Skip to content

Commit

Permalink
fix: ingest base64-encoded spanIDs from browsers (#184)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

- Ingesting base64-encoded spanIDs emitted from browsers
- follow-up to #182 

## Short description of the changes

- Added a test to demonstrate the issue
- `:=` -> `=` to fix unintended variable scoping
  • Loading branch information
robbkidd authored Mar 9, 2023
1 parent a5ad842 commit 00658f6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func bytesToSpanID(spanID []byte) string {
// The spec says that traceID and spanID should be encoded as hex, but
// the protobuf system is interpreting them as b64, so we need to
// reverse them back to b64 and then reencode as hex.
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(spanID)))
encoded = make([]byte, base64.StdEncoding.EncodedLen(len(spanID)))
base64.StdEncoding.Encode(encoded, spanID)
default:
encoded = make([]byte, len(spanID)*2)
Expand Down
40 changes: 40 additions & 0 deletions otlp/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,3 +1124,43 @@ func Test_BytesToTraceID(t *testing.T) {
})
}
}

func Test_BytesToSpanID(t *testing.T) {
tests := []struct {
name string
spanID string
b64 bool
want string
}{
{
name: "spanID",
spanID: "890452a577ef2e0f",
want: "890452a577ef2e0f",
},
{
name: "spanID munged by browser",
spanID: "890452a577ef2e0f",
b64: true,
want: "890452a577ef2e0f",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var spanID []byte
var err error
if tt.b64 {
spanID, err = base64.StdEncoding.DecodeString(tt.spanID)
} else {
spanID, err = hex.DecodeString(tt.spanID)
}
if err != nil {
spanID = []byte(tt.spanID)
}
got := bytesToSpanID(spanID)
if got != tt.want {
t.Errorf("got: %#v\n\twant: %#v", got, tt.want)
}
})
}
}

0 comments on commit 00658f6

Please sign in to comment.