exporter_traces_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package clickhouseexporter
  4. import (
  5. "context"
  6. "database/sql/driver"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/pdata/pcommon"
  12. "go.opentelemetry.io/collector/pdata/ptrace"
  13. conventions "go.opentelemetry.io/collector/semconv/v1.18.0"
  14. "go.uber.org/zap/zaptest"
  15. )
  16. func TestExporter_pushTracesData(t *testing.T) {
  17. t.Run("push success", func(t *testing.T) {
  18. var items int
  19. initClickhouseTestServer(t, func(query string, values []driver.Value) error {
  20. t.Logf("%d, values:%+v", items, values)
  21. if strings.HasPrefix(query, "INSERT") {
  22. items++
  23. }
  24. return nil
  25. })
  26. exporter := newTestTracesExporter(t, defaultEndpoint)
  27. mustPushTracesData(t, exporter, simpleTraces(1))
  28. mustPushTracesData(t, exporter, simpleTraces(2))
  29. require.Equal(t, 3, items)
  30. })
  31. t.Run("check insert scopeName and ScopeVersion", func(t *testing.T) {
  32. initClickhouseTestServer(t, func(query string, values []driver.Value) error {
  33. if strings.HasPrefix(query, "INSERT") {
  34. require.Equal(t, "io.opentelemetry.contrib.clickhouse", values[9])
  35. require.Equal(t, "1.0.0", values[10])
  36. }
  37. return nil
  38. })
  39. exporter := newTestTracesExporter(t, defaultEndpoint)
  40. mustPushTracesData(t, exporter, simpleTraces(1))
  41. })
  42. }
  43. func newTestTracesExporter(t *testing.T, dsn string, fns ...func(*Config)) *tracesExporter {
  44. exporter, err := newTracesExporter(zaptest.NewLogger(t), withTestExporterConfig(fns...)(dsn))
  45. require.NoError(t, err)
  46. require.NoError(t, exporter.start(context.TODO(), nil))
  47. t.Cleanup(func() { _ = exporter.shutdown(context.TODO()) })
  48. return exporter
  49. }
  50. func simpleTraces(count int) ptrace.Traces {
  51. traces := ptrace.NewTraces()
  52. rs := traces.ResourceSpans().AppendEmpty()
  53. rs.SetSchemaUrl("https://opentelemetry.io/schemas/1.4.0")
  54. rs.Resource().SetDroppedAttributesCount(10)
  55. rs.Resource().Attributes().PutStr("service.name", "test-service")
  56. ss := rs.ScopeSpans().AppendEmpty()
  57. ss.Scope().SetName("io.opentelemetry.contrib.clickhouse")
  58. ss.Scope().SetVersion("1.0.0")
  59. ss.SetSchemaUrl("https://opentelemetry.io/schemas/1.7.0")
  60. ss.Scope().SetDroppedAttributesCount(20)
  61. ss.Scope().Attributes().PutStr("lib", "clickhouse")
  62. for i := 0; i < count; i++ {
  63. s := ss.Spans().AppendEmpty()
  64. s.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Now()))
  65. s.SetEndTimestamp(pcommon.NewTimestampFromTime(time.Now()))
  66. s.Attributes().PutStr(conventions.AttributeServiceName, "v")
  67. event := s.Events().AppendEmpty()
  68. event.SetName("event1")
  69. event.SetTimestamp(pcommon.NewTimestampFromTime(time.Now()))
  70. link := s.Links().AppendEmpty()
  71. link.Attributes().PutStr("k", "v")
  72. }
  73. return traces
  74. }
  75. func mustPushTracesData(t *testing.T, exporter *tracesExporter, td ptrace.Traces) {
  76. err := exporter.pushTraceData(context.TODO(), td)
  77. require.NoError(t, err)
  78. }