|
| 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