Skip to content

Commit 8b17b89

Browse files
bborehamMrAlias
andauthored
sdk/trace: More trace id tests (#7155)
* Check that IDs round-trip between binary and hex string formats. `TestIDsRoundTrip` is patterned after `TestNewIDs`. * Check that bad values give the expected errors. * Use larger values in `TestWithIDGenerator`. Previously, nearly all the bits were zero so a mistake in encoding/decoding higher bits could be missed. Start with arbitrary values with more bits set. (Span ID still has top half as zero due to taking an uint64) * Modify `testIDGenerator` so this ^^ change runs on 32-bit platforms. The idea for more tests arose while considering #6791. Does not need a CHANGELOG entry - test only. --------- Signed-off-by: Bryan Boreham <bjboreham@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
1 parent d31fb11 commit 8b17b89

File tree

1 file changed

+73
-6
lines changed

1 file changed

+73
-6
lines changed

sdk/trace/trace_test.go

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,8 +1930,8 @@ func TestSamplerTraceState(t *testing.T) {
19301930
}
19311931

19321932
type testIDGenerator struct {
1933-
traceID int
1934-
spanID int
1933+
traceID uint64
1934+
spanID uint64
19351935
}
19361936

19371937
func (gen *testIDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
@@ -1954,8 +1954,8 @@ var _ IDGenerator = (*testIDGenerator)(nil)
19541954

19551955
func TestWithIDGenerator(t *testing.T) {
19561956
const (
1957-
startTraceID = 1
1958-
startSpanID = 10
1957+
startTraceID = 0x1001_1001_1001_1001
1958+
startSpanID = 0x2001_2001_2001_2001
19591959
numSpan = 5
19601960
)
19611961

@@ -1972,15 +1972,82 @@ func TestWithIDGenerator(t *testing.T) {
19721972

19731973
gotSpanID, err := strconv.ParseUint(span.SpanContext().SpanID().String(), 16, 64)
19741974
require.NoError(t, err)
1975-
assert.Equal(t, uint64(startSpanID+i), gotSpanID)
1975+
assert.Equal(t, uint64(startSpanID)+uint64(i), gotSpanID)
19761976

19771977
gotTraceID, err := strconv.ParseUint(span.SpanContext().TraceID().String(), 16, 64)
19781978
require.NoError(t, err)
1979-
assert.Equal(t, uint64(startTraceID+i), gotTraceID)
1979+
assert.Equal(t, uint64(startTraceID)+uint64(i), gotTraceID)
19801980
}()
19811981
}
19821982
}
19831983

1984+
func TestIDsRoundTrip(t *testing.T) {
1985+
gen := defaultIDGenerator()
1986+
1987+
for range 1000 {
1988+
traceID, spanID := gen.NewIDs(context.Background())
1989+
gotTraceID, err := trace.TraceIDFromHex(traceID.String())
1990+
assert.NoError(t, err)
1991+
assert.Equal(t, traceID, gotTraceID)
1992+
gotSpanID, err := trace.SpanIDFromHex(spanID.String())
1993+
assert.NoError(t, err)
1994+
assert.Equal(t, spanID, gotSpanID)
1995+
}
1996+
}
1997+
1998+
func TestIDConversionErrors(t *testing.T) {
1999+
for _, tt := range []struct {
2000+
name string
2001+
spanIDStr string
2002+
traceIDStr string
2003+
spanIDError string
2004+
traceIDError string
2005+
}{
2006+
{
2007+
name: "slightly too long",
2008+
spanIDStr: sid.String() + "0",
2009+
spanIDError: "hex encoded span-id must have length equals to 16",
2010+
traceIDStr: tid.String() + "0",
2011+
traceIDError: "hex encoded trace-id must have length equals to 32",
2012+
},
2013+
{
2014+
name: "blank input",
2015+
spanIDStr: "",
2016+
spanIDError: "hex encoded span-id must have length equals to 16",
2017+
traceIDStr: "",
2018+
traceIDError: "hex encoded trace-id must have length equals to 32",
2019+
},
2020+
{
2021+
name: "not hex",
2022+
spanIDStr: "unacceptablechar",
2023+
spanIDError: "trace-id and span-id can only contain [0-9a-f] characters, all lowercase",
2024+
traceIDStr: "completely unacceptablecharacter",
2025+
traceIDError: "trace-id and span-id can only contain [0-9a-f] characters, all lowercase",
2026+
},
2027+
{
2028+
name: "upper-case hex",
2029+
spanIDStr: "DEADBEEFBAD0CAFE",
2030+
spanIDError: "trace-id and span-id can only contain [0-9a-f] characters, all lowercase",
2031+
traceIDStr: "DEADBEEFBAD0CAFEDEADBEEFBAD0CAFE",
2032+
traceIDError: "trace-id and span-id can only contain [0-9a-f] characters, all lowercase",
2033+
},
2034+
{
2035+
name: "all zero",
2036+
spanIDStr: "0000000000000000",
2037+
spanIDError: "span-id can't be all zero",
2038+
traceIDStr: "00000000000000000000000000000000",
2039+
traceIDError: "trace-id can't be all zero",
2040+
},
2041+
} {
2042+
t.Run(tt.name, func(t *testing.T) {
2043+
_, err := trace.SpanIDFromHex(tt.spanIDStr)
2044+
assert.ErrorContains(t, err, tt.spanIDError)
2045+
_, err = trace.TraceIDFromHex(tt.traceIDStr)
2046+
assert.ErrorContains(t, err, tt.traceIDError)
2047+
})
2048+
}
2049+
}
2050+
19842051
func TestEmptyRecordingSpanAttributes(t *testing.T) {
19852052
assert.Nil(t, (&recordingSpan{}).Attributes())
19862053
}

0 commit comments

Comments
 (0)