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

Fix incorrect parsing of a single b3 header #190

Merged
merged 1 commit into from
Mar 20, 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
2 changes: 1 addition & 1 deletion propagation/b3/spancontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func ParseSingleHeader(contextHeader string) (*model.SpanContext, error) {
pos = 16
}

low, err := strconv.ParseUint(contextHeader[pos+1:pos+16], 16, 64)
low, err := strconv.ParseUint(contextHeader[pos:pos+16], 16, 64)
if err != nil {
return nil, ErrInvalidTraceIDValue
}
Expand Down
41 changes: 17 additions & 24 deletions propagation/b3/spancontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,19 @@ import (
)

func TestParseHeaderSuccess(t *testing.T) {
trueVal := true
falseVal := false
ParentIDVal := model.ID(456)

testCases := []struct {
header string
expectedContext *model.SpanContext
expectedErr error
}{
{"d", &model.SpanContext{Debug: true}, nil},
{"1", &model.SpanContext{Sampled: &trueVal}, nil},
{"1", &model.SpanContext{Sampled: pointerBool(true)}, nil},
{"000000000000007b00000000000001c8-000000000000007b", &model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123)}, nil},
{"000000000000007b00000000000001c8-000000000000007b-0", &model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123), Sampled: &falseVal}, nil},
{
"000000000000007b00000000000001c8-000000000000007b-1-00000000000001c8",
&model.SpanContext{
TraceID: model.TraceID{High: 123, Low: 456},
ID: model.ID(123),
ParentID: &ParentIDVal,
Sampled: &trueVal,
},
nil,
},
{"000000000000007b00000000000001c8-000000000000007b-0", &model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123), Sampled: pointerBool(false)}, nil},
{"000000000000007b00000000000001c8-000000000000007b-1-00000000000001c8", &model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123), ParentID: pointerID(model.ID(456)), Sampled: pointerBool(true)}, nil},
{"", nil, ErrEmptyContext},
{"80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90", &model.SpanContext{TraceID: model.TraceID{High: 9291375655657946024, Low: 7277407061855694839}, ID: model.ID(16453819474850114513), ParentID: pointerID(model.ID(424372568660523920)), Sampled: pointerBool(true)}, nil},
{"d4c3c787ce202dc5-77c6a763a5a72544-0-6a3211d95bed2c99", &model.SpanContext{TraceID: model.TraceID{High: 0, Low: 15331316942592028101}, ID: model.ID(8630769782324929860), ParentID: pointerID(model.ID(7652198342103739545)), Sampled: pointerBool(false)}, nil},
}

for _, testCase := range testCases {
Expand Down Expand Up @@ -95,24 +84,20 @@ func TestParseHeaderFails(t *testing.T) {
}

func TestBuildHeader(t *testing.T) {
trueVal := true
falseVal := false
ParentIDVal := model.ID(456)

testCases := []struct {
context model.SpanContext
expectedHeader string
}{
{model.SpanContext{ID: model.ID(123)}, ""},
{model.SpanContext{Debug: true}, "d"},
{model.SpanContext{Sampled: &trueVal}, "1"},
{model.SpanContext{Sampled: pointerBool(true)}, "1"},
{model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123)}, "000000000000007b00000000000001c8-000000000000007b"},
{model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123), Sampled: &falseVal}, "000000000000007b00000000000001c8-000000000000007b-0"},
{model.SpanContext{TraceID: model.TraceID{High: 123, Low: 456}, ID: model.ID(123), Sampled: pointerBool(false)}, "000000000000007b00000000000001c8-000000000000007b-0"},
{model.SpanContext{
TraceID: model.TraceID{High: 123, Low: 456},
ID: model.ID(123),
ParentID: &ParentIDVal,
Sampled: &falseVal,
ParentID: pointerID(model.ID(456)),
Sampled: pointerBool(false),
}, "000000000000007b00000000000001c8-000000000000007b-0-00000000000001c8"},
}

Expand All @@ -123,3 +108,11 @@ func TestBuildHeader(t *testing.T) {
}
}
}

func pointerID(value model.ID) *model.ID {
return &value
}

func pointerBool(value bool) *bool {
return &value
}