marshaler_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package kafkaexporter
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "testing"
  8. "time"
  9. zipkin "github.com/openzipkin/zipkin-go/model"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. "go.opentelemetry.io/collector/pdata/pcommon"
  13. "go.opentelemetry.io/collector/pdata/ptrace"
  14. conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
  15. )
  16. func TestDefaultTracesMarshalers(t *testing.T) {
  17. expectedEncodings := []string{
  18. "otlp_proto",
  19. "otlp_json",
  20. "zipkin_proto",
  21. "zipkin_json",
  22. "jaeger_proto",
  23. "jaeger_json",
  24. }
  25. marshalers := tracesMarshalers()
  26. assert.Equal(t, len(expectedEncodings), len(marshalers))
  27. for _, e := range expectedEncodings {
  28. t.Run(e, func(t *testing.T) {
  29. m, ok := marshalers[e]
  30. require.True(t, ok)
  31. assert.NotNil(t, m)
  32. })
  33. }
  34. }
  35. func TestDefaultMetricsMarshalers(t *testing.T) {
  36. expectedEncodings := []string{
  37. "otlp_proto",
  38. "otlp_json",
  39. }
  40. marshalers := metricsMarshalers()
  41. assert.Equal(t, len(expectedEncodings), len(marshalers))
  42. for _, e := range expectedEncodings {
  43. t.Run(e, func(t *testing.T) {
  44. m, ok := marshalers[e]
  45. require.True(t, ok)
  46. assert.NotNil(t, m)
  47. })
  48. }
  49. }
  50. func TestDefaultLogsMarshalers(t *testing.T) {
  51. expectedEncodings := []string{
  52. "otlp_proto",
  53. "otlp_json",
  54. "raw",
  55. }
  56. marshalers := logsMarshalers()
  57. assert.Equal(t, len(expectedEncodings), len(marshalers))
  58. for _, e := range expectedEncodings {
  59. t.Run(e, func(t *testing.T) {
  60. m, ok := marshalers[e]
  61. require.True(t, ok)
  62. assert.NotNil(t, m)
  63. })
  64. }
  65. }
  66. func TestOTLPTracesJsonMarshaling(t *testing.T) {
  67. t.Parallel()
  68. now := time.Unix(1, 0)
  69. traces := ptrace.NewTraces()
  70. traces.ResourceSpans().AppendEmpty()
  71. rs := traces.ResourceSpans().At(0)
  72. rs.SetSchemaUrl(conventions.SchemaURL)
  73. rs.ScopeSpans().AppendEmpty()
  74. ils := rs.ScopeSpans().At(0)
  75. ils.SetSchemaUrl(conventions.SchemaURL)
  76. ils.Spans().AppendEmpty()
  77. span := ils.Spans().At(0)
  78. span.SetKind(ptrace.SpanKindServer)
  79. span.SetName("foo")
  80. span.SetStartTimestamp(pcommon.NewTimestampFromTime(now))
  81. span.SetEndTimestamp(pcommon.NewTimestampFromTime(now.Add(time.Second)))
  82. span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
  83. span.SetSpanID([8]byte{0, 1, 2, 3, 4, 5, 6, 7})
  84. span.SetParentSpanID([8]byte{8, 9, 10, 11, 12, 13, 14})
  85. // Since marshaling json is not guaranteed to be in order
  86. // within a string, using a map to compare that the expected values are there
  87. otlpJSON := map[string]any{
  88. "resourceSpans": []any{
  89. map[string]any{
  90. "resource": map[string]any{},
  91. "scopeSpans": []any{
  92. map[string]any{
  93. "scope": map[string]any{},
  94. "spans": []any{
  95. map[string]any{
  96. "traceId": "0102030405060708090a0b0c0d0e0f10",
  97. "spanId": "0001020304050607",
  98. "parentSpanId": "08090a0b0c0d0e00",
  99. "name": "foo",
  100. "kind": float64(ptrace.SpanKindServer),
  101. "startTimeUnixNano": fmt.Sprint(now.UnixNano()),
  102. "endTimeUnixNano": fmt.Sprint(now.Add(time.Second).UnixNano()),
  103. "status": map[string]any{},
  104. },
  105. },
  106. "schemaUrl": conventions.SchemaURL,
  107. },
  108. },
  109. "schemaUrl": conventions.SchemaURL,
  110. },
  111. },
  112. }
  113. zipkinJSON := []any{
  114. map[string]any{
  115. "traceId": "0102030405060708090a0b0c0d0e0f10",
  116. "id": "0001020304050607",
  117. "parentId": "08090a0b0c0d0e00",
  118. "name": "foo",
  119. "timestamp": float64(time.Second.Microseconds()),
  120. "duration": float64(time.Second.Microseconds()),
  121. "kind": string(zipkin.Server),
  122. "localEndpoint": map[string]any{"serviceName": "otlpresourcenoservicename"},
  123. },
  124. }
  125. tests := []struct {
  126. encoding string
  127. expectedJSON any
  128. unmarshaled any
  129. }{
  130. {encoding: "otlp_json", expectedJSON: otlpJSON, unmarshaled: map[string]any{}},
  131. {encoding: "zipkin_json", expectedJSON: zipkinJSON, unmarshaled: []map[string]any{}},
  132. }
  133. for _, test := range tests {
  134. marshaler, ok := tracesMarshalers()[test.encoding]
  135. require.True(t, ok, fmt.Sprintf("Must have %s marshaller", test.encoding))
  136. msg, err := marshaler.Marshal(traces, t.Name())
  137. require.NoError(t, err, "Must have marshaled the data without error")
  138. require.Len(t, msg, 1, "Must have one entry in the message")
  139. data, err := msg[0].Value.Encode()
  140. require.NoError(t, err, "Must not error when encoding value")
  141. require.NotNil(t, data, "Must have valid data to test")
  142. unmarshaled := test.unmarshaled
  143. err = json.Unmarshal(data, &unmarshaled)
  144. require.NoError(t, err, "Must not error marshaling expected data")
  145. assert.Equal(t, test.expectedJSON, unmarshaled, "Must match the expected value")
  146. }
  147. }