Skip to content

Commit a10652b

Browse files
mahendrabishnoi2dmathieupellared
authored
sdk/trace: trace id high 64 bit tests (#7212)
Fixes #7160 - Modified `testIDGenerator` to have both low and high bits (in uint64 form) since we can't store 128 bits integer - start with high seed values (taken from #7155) - validate both high and low 64 bits of trace id Does not need a CHANGELOG entry - test only. --------- Co-authored-by: Damien Mathieu <[email protected]> Co-authored-by: Robert Pająk <[email protected]> Co-authored-by: dmathieu <[email protected]>
1 parent 5937fc8 commit a10652b

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

sdk/trace/trace_test.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,14 +1931,15 @@ func TestSamplerTraceState(t *testing.T) {
19311931
}
19321932

19331933
type testIDGenerator struct {
1934-
traceID uint64
1935-
spanID uint64
1934+
traceIDHigh uint64
1935+
traceIDLow uint64
1936+
spanID uint64
19361937
}
19371938

19381939
func (gen *testIDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
1939-
traceIDHex := fmt.Sprintf("%032x", gen.traceID)
1940+
traceIDHex := fmt.Sprintf("%016x%016x", gen.traceIDHigh, gen.traceIDLow)
19401941
traceID, _ := trace.TraceIDFromHex(traceIDHex)
1941-
gen.traceID++
1942+
gen.traceIDLow++
19421943

19431944
spanID := gen.NewSpanID(ctx, traceID)
19441945
return traceID, spanID
@@ -1955,12 +1956,13 @@ var _ IDGenerator = (*testIDGenerator)(nil)
19551956

19561957
func TestWithIDGenerator(t *testing.T) {
19571958
const (
1958-
startTraceID = 0x1001_1001_1001_1001
1959-
startSpanID = 0x2001_2001_2001_2001
1960-
numSpan = 5
1959+
startTraceIDHigh uint64 = 0x1001_1001_1001_1001
1960+
startTraceIDLow uint64 = 0x2002_2002_2002_2002
1961+
startSpanID uint64 = 0x3003_3003_3003_3003
1962+
numSpan = 5
19611963
)
19621964

1963-
gen := &testIDGenerator{traceID: startTraceID, spanID: startSpanID}
1965+
gen := &testIDGenerator{traceIDHigh: startTraceIDHigh, traceIDLow: startTraceIDLow, spanID: startSpanID}
19641966
te := NewTestExporter()
19651967
tp := NewTracerProvider(
19661968
WithSyncer(te),
@@ -1973,11 +1975,19 @@ func TestWithIDGenerator(t *testing.T) {
19731975

19741976
gotSpanID, err := strconv.ParseUint(span.SpanContext().SpanID().String(), 16, 64)
19751977
require.NoError(t, err)
1976-
assert.Equal(t, uint64(startSpanID)+uint64(i), gotSpanID)
1978+
assert.Equal(t, startSpanID+uint64(i), gotSpanID)
19771979

1978-
gotTraceID, err := strconv.ParseUint(span.SpanContext().TraceID().String(), 16, 64)
1979-
require.NoError(t, err)
1980-
assert.Equal(t, uint64(startTraceID)+uint64(i), gotTraceID)
1980+
traceIdStr := span.SpanContext().TraceID().String()
1981+
highBitsStr := traceIdStr[:16]
1982+
lowBitsStr := traceIdStr[16:]
1983+
1984+
traceIdValidator := func(t *testing.T, id string, expected uint64) {
1985+
gotTraceID, err := strconv.ParseUint(id, 16, 64)
1986+
require.NoError(t, err)
1987+
assert.Equal(t, expected, gotTraceID)
1988+
}
1989+
traceIdValidator(t, highBitsStr, startTraceIDHigh)
1990+
traceIdValidator(t, lowBitsStr, startTraceIDLow+uint64(i))
19811991
}()
19821992
}
19831993
}

0 commit comments

Comments
 (0)