|
| 1 | +// Copyright (c) 2025 The Jaeger Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package internal |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 12 | + "go.opentelemetry.io/collector/pdata/ptrace" |
| 13 | + conventions "go.opentelemetry.io/collector/semconv/v1.27.0" |
| 14 | + |
| 15 | + "github.com/jaegertracing/jaeger/pkg/testutils" |
| 16 | +) |
| 17 | + |
| 18 | +var expected = "INSERT INTO \"otel_traces\" (\"Timestamp\",\"traceId\",\"SpanId\",\"ParentSpanId\",\"TraceState\"," + |
| 19 | + "\"SpanName\",\"SpanKind\",\"ServiceName\",\"ResourceAttributes.keys\",\"ResourceAttributes.values\",\"ScopeName\"," + |
| 20 | + "\"ScopeVersion\",\"SpanAttributes.keys\",\"SpanAttributes.values\",\"Duration\",\"StatusCode\",\"StatusMessage\"," + |
| 21 | + "\"Events.Timestamp\",\"Events.Name\",\"Events.Attributes\",\"Links.traceId\",\"Links.SpanId\",\"Links.TraceState\"," + |
| 22 | + "\"Links.Attributes\") VALUES" |
| 23 | + |
| 24 | +func TestInput(t *testing.T) { |
| 25 | + td := simpleTraces(2) |
| 26 | + actual := Input(td) |
| 27 | + assert.Equal(t, expected, actual.Into("otel_traces")) |
| 28 | +} |
| 29 | + |
| 30 | +func TestMain(m *testing.M) { |
| 31 | + testutils.VerifyGoLeaks(m) |
| 32 | +} |
| 33 | + |
| 34 | +func simpleTraces(count int) ptrace.Traces { |
| 35 | + traces := ptrace.NewTraces() |
| 36 | + rs := traces.ResourceSpans().AppendEmpty() |
| 37 | + rs.SetSchemaUrl("https://opentelemetry.io/schemas/1.4.0") |
| 38 | + rs.Resource().SetDroppedAttributesCount(10) |
| 39 | + rs.Resource().Attributes().PutStr("service.name", "test-service") |
| 40 | + ss := rs.ScopeSpans().AppendEmpty() |
| 41 | + ss.Scope().SetName("io.opentelemetry.contrib.clickhouse") |
| 42 | + ss.Scope().SetVersion("1.0.0") |
| 43 | + ss.SetSchemaUrl("https://opentelemetry.io/schemas/1.7.0") |
| 44 | + ss.Scope().SetDroppedAttributesCount(20) |
| 45 | + ss.Scope().Attributes().PutStr("lib", "clickhouse") |
| 46 | + timestamp := time.Unix(1703498029, 0) |
| 47 | + for i := 0; i < count; i++ { |
| 48 | + s := ss.Spans().AppendEmpty() |
| 49 | + s.SetTraceID([16]byte{1, 2, 3, byte(i)}) |
| 50 | + s.SetSpanID([8]byte{1, 2, 3, byte(i)}) |
| 51 | + s.TraceState().FromRaw("trace state") |
| 52 | + s.SetParentSpanID([8]byte{1, 2, 4, byte(i)}) |
| 53 | + s.SetName("call db") |
| 54 | + s.SetKind(ptrace.SpanKindInternal) |
| 55 | + s.SetStartTimestamp(pcommon.NewTimestampFromTime(timestamp)) |
| 56 | + s.SetEndTimestamp(pcommon.NewTimestampFromTime(timestamp.Add(time.Minute))) |
| 57 | + s.Attributes().PutStr(conventions.AttributeServiceName, "v") |
| 58 | + s.Status().SetMessage("error") |
| 59 | + s.Status().SetCode(ptrace.StatusCodeError) |
| 60 | + event := s.Events().AppendEmpty() |
| 61 | + event.SetName("event1") |
| 62 | + event.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) |
| 63 | + event.Attributes().PutStr("level", "info") |
| 64 | + link := s.Links().AppendEmpty() |
| 65 | + link.SetTraceID([16]byte{1, 2, 5, byte(i)}) |
| 66 | + link.SetSpanID([8]byte{1, 2, 5, byte(i)}) |
| 67 | + link.TraceState().FromRaw("error") |
| 68 | + link.Attributes().PutStr("k", "v") |
| 69 | + } |
| 70 | + return traces |
| 71 | +} |
0 commit comments