Skip to content

Commit 300b511

Browse files
authored
[refactor][clickhouse] Add round-trip tests for ClickHouse's dbmodel package (#7622)
1 parent 65a8abf commit 300b511

File tree

11 files changed

+229
-1495
lines changed

11 files changed

+229
-1495
lines changed

internal/jptrace/spankind.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
)
1111

1212
func StringToSpanKind(sk string) ptrace.SpanKind {
13-
switch sk {
14-
case "Internal":
13+
switch strings.ToLower(sk) {
14+
case "internal":
1515
return ptrace.SpanKindInternal
16-
case "Server":
16+
case "server":
1717
return ptrace.SpanKindServer
18-
case "Client":
18+
case "client":
1919
return ptrace.SpanKindClient
20-
case "Producer":
20+
case "producer":
2121
return ptrace.SpanKindProducer
22-
case "Consumer":
22+
case "consumer":
2323
return ptrace.SpanKindConsumer
2424
default:
2525
return ptrace.SpanKindUnspecified

internal/jptrace/spankind_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,31 @@ func TestStringToSpanKind(t *testing.T) {
1616
want ptrace.SpanKind
1717
}{
1818
{
19-
str: "Unspecified",
19+
str: "unspecified",
2020
want: ptrace.SpanKindUnspecified,
2121
},
2222
{
23-
str: "Internal",
23+
str: "internal",
2424
want: ptrace.SpanKindInternal,
2525
},
2626
{
27-
str: "Server",
27+
str: "server",
2828
want: ptrace.SpanKindServer,
2929
},
3030
{
31-
str: "Client",
31+
str: "client",
3232
want: ptrace.SpanKindClient,
3333
},
3434
{
35-
str: "Producer",
35+
str: "producer",
3636
want: ptrace.SpanKindProducer,
3737
},
3838
{
39-
str: "Consumer",
39+
str: "consumer",
4040
want: ptrace.SpanKindConsumer,
4141
},
4242
{
43-
str: "Unknown",
43+
str: "unknown",
4444
want: ptrace.SpanKindUnspecified,
4545
},
4646
{

internal/storage/v2/clickhouse/tracestore/assert_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.opentelemetry.io/collector/pdata/pcommon"
1414
"go.opentelemetry.io/collector/pdata/ptrace"
1515

16+
"github.com/jaegertracing/jaeger/internal/jptrace"
1617
"github.com/jaegertracing/jaeger/internal/storage/v2/clickhouse/tracestore/dbmodel"
1718
)
1819

@@ -51,7 +52,7 @@ func requireSpanEqual(t *testing.T, expected *dbmodel.SpanRow, actual ptrace.Spa
5152
require.Equal(t, expected.TraceState, actual.TraceState().AsRaw())
5253
require.Equal(t, expected.ParentSpanID, actual.ParentSpanID().String())
5354
require.Equal(t, expected.Name, actual.Name())
54-
require.Equal(t, expected.Kind, actual.Kind().String())
55+
require.Equal(t, expected.Kind, jptrace.SpanKindToString(actual.Kind()))
5556
require.Equal(t, expected.StartTime.UnixNano(), actual.StartTimestamp().AsTime().UnixNano())
5657
require.Equal(t, expected.StatusCode, actual.Status().Code().String())
5758
require.Equal(t, expected.StatusMessage, actual.Status().Message())
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Copyright (c) 2025 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package dbmodel
5+
6+
import (
7+
"encoding/base64"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/collector/pdata/pcommon"
13+
"go.opentelemetry.io/collector/pdata/ptrace"
14+
15+
"github.com/jaegertracing/jaeger/internal/telemetry/otelsemconv"
16+
)
17+
18+
func TestRoundTrip(t *testing.T) {
19+
now := time.Now().UTC()
20+
duration := 2 * time.Second
21+
22+
t.Run("ToRow->FromRow", func(t *testing.T) {
23+
rs := createTestResource()
24+
sc := createTestScope()
25+
span := createTestSpan(now, duration)
26+
27+
expected := createTestTrace(now, duration)
28+
29+
row := ToRow(rs, sc, span)
30+
trace := FromRow(row)
31+
require.Equal(t, expected, trace)
32+
})
33+
34+
t.Run("FromRow->ToRow", func(t *testing.T) {
35+
spanRow := createTestSpanRow(now, duration)
36+
37+
trace := FromRow(spanRow)
38+
rs := trace.ResourceSpans().At(0).Resource()
39+
sc := trace.ResourceSpans().At(0).ScopeSpans().At(0).Scope()
40+
span := trace.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0)
41+
42+
row := ToRow(rs, sc, span)
43+
require.Equal(t, spanRow, row)
44+
})
45+
}
46+
47+
func createTestTrace(now time.Time, duration time.Duration) ptrace.Traces {
48+
rs := createTestResource()
49+
sc := createTestScope()
50+
span := createTestSpan(now, duration)
51+
52+
td := ptrace.NewTraces()
53+
rsSpans := td.ResourceSpans().AppendEmpty()
54+
rs.CopyTo(rsSpans.Resource())
55+
scSpans := rsSpans.ScopeSpans().AppendEmpty()
56+
sc.CopyTo(scSpans.Scope())
57+
span.CopyTo(scSpans.Spans().AppendEmpty())
58+
return td
59+
}
60+
61+
func createTestResource() pcommon.Resource {
62+
rs := pcommon.NewResource()
63+
rs.Attributes().PutStr(otelsemconv.ServiceNameKey, "test-service")
64+
addTestAttributes(rs.Attributes())
65+
return rs
66+
}
67+
68+
func createTestScope() pcommon.InstrumentationScope {
69+
sc := pcommon.NewInstrumentationScope()
70+
sc.SetName("test-scope")
71+
sc.SetVersion("v1.0.0")
72+
addTestAttributes(sc.Attributes())
73+
return sc
74+
}
75+
76+
func createTestSpan(now time.Time, duration time.Duration) ptrace.Span {
77+
span := ptrace.NewSpan()
78+
span.SetSpanID(pcommon.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}))
79+
span.SetTraceID(pcommon.TraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}))
80+
span.TraceState().FromRaw("state1")
81+
span.SetParentSpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 2})
82+
span.SetName("test-span")
83+
span.SetKind(ptrace.SpanKindServer)
84+
span.SetStartTimestamp(pcommon.NewTimestampFromTime(now))
85+
span.SetEndTimestamp(pcommon.NewTimestampFromTime(now.Add(duration)))
86+
span.Status().SetCode(ptrace.StatusCodeOk)
87+
span.Status().SetMessage("test-status-message")
88+
89+
addTestAttributes(span.Attributes())
90+
addSpanEvent(span, now)
91+
addSpanLink(span)
92+
93+
return span
94+
}
95+
96+
func addSpanEvent(span ptrace.Span, now time.Time) {
97+
event := span.Events().AppendEmpty()
98+
event.SetName("test-event")
99+
event.SetTimestamp(pcommon.NewTimestampFromTime(now))
100+
addTestAttributes(event.Attributes())
101+
}
102+
103+
func addSpanLink(span ptrace.Span) {
104+
link := span.Links().AppendEmpty()
105+
link.SetTraceID(pcommon.TraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3}))
106+
link.SetSpanID(pcommon.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 4}))
107+
link.TraceState().FromRaw("link-state")
108+
addTestAttributes(link.Attributes())
109+
}
110+
111+
func addTestAttributes(attrs pcommon.Map) {
112+
attrs.PutBool("bool_attr", true)
113+
attrs.PutDouble("double_attr", 3.14)
114+
attrs.PutInt("int_attr", 42)
115+
attrs.PutStr("string_attr", "string_value")
116+
attrs.PutEmptyBytes("bytes_attr").FromRaw([]byte("bytes_value"))
117+
}
118+
119+
func createTestSpanRow(now time.Time, duration time.Duration) *SpanRow {
120+
encodedBytes := base64.StdEncoding.EncodeToString([]byte("bytes_value"))
121+
return &SpanRow{
122+
ID: "0000000000000001",
123+
TraceID: "00000000000000000000000000000001",
124+
TraceState: "state1",
125+
ParentSpanID: "0000000000000002",
126+
Name: "test-span",
127+
Kind: "server",
128+
StartTime: now,
129+
StatusCode: "Ok",
130+
StatusMessage: "test-status-message",
131+
Duration: duration.Nanoseconds(),
132+
Attributes: Attributes{
133+
BoolKeys: []string{"bool_attr"},
134+
BoolValues: []bool{true},
135+
DoubleKeys: []string{"double_attr"},
136+
DoubleValues: []float64{3.14},
137+
IntKeys: []string{"int_attr"},
138+
IntValues: []int64{42},
139+
StrKeys: []string{"string_attr"},
140+
StrValues: []string{"string_value"},
141+
ComplexKeys: []string{"@bytes@bytes_attr"},
142+
ComplexValues: []string{encodedBytes},
143+
},
144+
EventNames: []string{"test-event"},
145+
EventTimestamps: []time.Time{now},
146+
EventAttributes: Attributes2D{
147+
BoolKeys: [][]string{{"bool_attr"}},
148+
BoolValues: [][]bool{{true}},
149+
DoubleKeys: [][]string{{"double_attr"}},
150+
DoubleValues: [][]float64{{3.14}},
151+
IntKeys: [][]string{{"int_attr"}},
152+
IntValues: [][]int64{{42}},
153+
StrKeys: [][]string{{"string_attr"}},
154+
StrValues: [][]string{{"string_value"}},
155+
ComplexKeys: [][]string{{"@bytes@bytes_attr"}},
156+
ComplexValues: [][]string{{encodedBytes}},
157+
},
158+
LinkTraceIDs: []string{"00000000000000000000000000000003"},
159+
LinkSpanIDs: []string{"0000000000000004"},
160+
LinkTraceStates: []string{"link-state"},
161+
LinkAttributes: Attributes2D{
162+
BoolKeys: [][]string{{"bool_attr"}},
163+
BoolValues: [][]bool{{true}},
164+
DoubleKeys: [][]string{{"double_attr"}},
165+
DoubleValues: [][]float64{{3.14}},
166+
IntKeys: [][]string{{"int_attr"}},
167+
IntValues: [][]int64{{42}},
168+
StrKeys: [][]string{{"string_attr"}},
169+
StrValues: [][]string{{"string_value"}},
170+
ComplexKeys: [][]string{{"@bytes@bytes_attr"}},
171+
ComplexValues: [][]string{{encodedBytes}},
172+
},
173+
ServiceName: "test-service",
174+
ResourceAttributes: Attributes{
175+
BoolKeys: []string{"bool_attr"},
176+
BoolValues: []bool{true},
177+
DoubleKeys: []string{"double_attr"},
178+
DoubleValues: []float64{3.14},
179+
IntKeys: []string{"int_attr"},
180+
IntValues: []int64{42},
181+
StrKeys: []string{"service.name", "string_attr"},
182+
StrValues: []string{"test-service", "string_value"},
183+
ComplexKeys: []string{"@bytes@bytes_attr"},
184+
ComplexValues: []string{encodedBytes},
185+
},
186+
ScopeName: "test-scope",
187+
ScopeVersion: "v1.0.0",
188+
ScopeAttributes: Attributes{
189+
BoolKeys: []string{"bool_attr"},
190+
BoolValues: []bool{true},
191+
DoubleKeys: []string{"double_attr"},
192+
DoubleValues: []float64{3.14},
193+
IntKeys: []string{"int_attr"},
194+
IntValues: []int64{42},
195+
StrKeys: []string{"string_attr"},
196+
StrValues: []string{"string_value"},
197+
ComplexKeys: []string{"@bytes@bytes_attr"},
198+
ComplexValues: []string{encodedBytes},
199+
},
200+
}
201+
}

0 commit comments

Comments
 (0)