1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package clickhouseexporter
- import (
- "context"
- "database/sql/driver"
- "strings"
- "testing"
- "time"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/pdata/pcommon"
- "go.opentelemetry.io/collector/pdata/ptrace"
- conventions "go.opentelemetry.io/collector/semconv/v1.18.0"
- "go.uber.org/zap/zaptest"
- )
- func TestExporter_pushTracesData(t *testing.T) {
- t.Run("push success", func(t *testing.T) {
- var items int
- initClickhouseTestServer(t, func(query string, values []driver.Value) error {
- t.Logf("%d, values:%+v", items, values)
- if strings.HasPrefix(query, "INSERT") {
- items++
- }
- return nil
- })
- exporter := newTestTracesExporter(t, defaultEndpoint)
- mustPushTracesData(t, exporter, simpleTraces(1))
- mustPushTracesData(t, exporter, simpleTraces(2))
- require.Equal(t, 3, items)
- })
- t.Run("check insert scopeName and ScopeVersion", func(t *testing.T) {
- initClickhouseTestServer(t, func(query string, values []driver.Value) error {
- if strings.HasPrefix(query, "INSERT") {
- require.Equal(t, "io.opentelemetry.contrib.clickhouse", values[9])
- require.Equal(t, "1.0.0", values[10])
- }
- return nil
- })
- exporter := newTestTracesExporter(t, defaultEndpoint)
- mustPushTracesData(t, exporter, simpleTraces(1))
- })
- }
- func newTestTracesExporter(t *testing.T, dsn string, fns ...func(*Config)) *tracesExporter {
- exporter, err := newTracesExporter(zaptest.NewLogger(t), withTestExporterConfig(fns...)(dsn))
- require.NoError(t, err)
- require.NoError(t, exporter.start(context.TODO(), nil))
- t.Cleanup(func() { _ = exporter.shutdown(context.TODO()) })
- return exporter
- }
- func simpleTraces(count int) ptrace.Traces {
- traces := ptrace.NewTraces()
- rs := traces.ResourceSpans().AppendEmpty()
- rs.SetSchemaUrl("https://opentelemetry.io/schemas/1.4.0")
- rs.Resource().SetDroppedAttributesCount(10)
- rs.Resource().Attributes().PutStr("service.name", "test-service")
- ss := rs.ScopeSpans().AppendEmpty()
- ss.Scope().SetName("io.opentelemetry.contrib.clickhouse")
- ss.Scope().SetVersion("1.0.0")
- ss.SetSchemaUrl("https://opentelemetry.io/schemas/1.7.0")
- ss.Scope().SetDroppedAttributesCount(20)
- ss.Scope().Attributes().PutStr("lib", "clickhouse")
- for i := 0; i < count; i++ {
- s := ss.Spans().AppendEmpty()
- s.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now()))
- s.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
- s.Attributes().PutStr(conventions.AttributeServiceName, "v")
- event := s.Events().AppendEmpty()
- event.SetName("event1")
- event.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
- link := s.Links().AppendEmpty()
- link.Attributes().PutStr("k", "v")
- }
- return traces
- }
- func mustPushTracesData(t *testing.T, exporter *tracesExporter, td ptrace.Traces) {
- err := exporter.pushTraceData(context.TODO(), td)
- require.NoError(t, err)
- }
|