|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package otelserializer |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "encoding/json" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 14 | + "go.opentelemetry.io/collector/pdata/ptrace" |
| 15 | + |
| 16 | + "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/elasticsearch" |
| 17 | +) |
| 18 | + |
| 19 | +func TestSerializeTraces(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + spanCustomizer func(resource pcommon.Resource, scope pcommon.InstrumentationScope, span ptrace.Span) |
| 23 | + wantErr bool |
| 24 | + expected any |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "default", |
| 28 | + spanCustomizer: func(resource pcommon.Resource, scope pcommon.InstrumentationScope, span ptrace.Span) { |
| 29 | + span.SetStartTimestamp(1721314113467654123) |
| 30 | + span.SetEndTimestamp(1721314113469654123) |
| 31 | + span.SetKind(ptrace.SpanKindServer) |
| 32 | + span.Status().SetCode(ptrace.StatusCodeOk) |
| 33 | + span.Status().SetMessage("Hello") |
| 34 | + resource.Attributes().PutInt("foo", 123) |
| 35 | + scope.Attributes().PutStr("foo", "bar") |
| 36 | + }, |
| 37 | + wantErr: false, |
| 38 | + expected: map[string]any{ |
| 39 | + "@timestamp": "1721314113467.654123", |
| 40 | + "duration": json.Number("2000000"), |
| 41 | + "kind": "Server", |
| 42 | + "links": []any{}, |
| 43 | + "resource": map[string]any{ |
| 44 | + "attributes": map[string]any{ |
| 45 | + "foo": json.Number("123"), |
| 46 | + }, |
| 47 | + }, |
| 48 | + "scope": map[string]any{ |
| 49 | + "attributes": map[string]any{ |
| 50 | + "foo": "bar", |
| 51 | + }, |
| 52 | + }, |
| 53 | + "status": map[string]any{ |
| 54 | + "code": "Ok", |
| 55 | + "message": "Hello", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "unset status code", |
| 61 | + spanCustomizer: func(_ pcommon.Resource, _ pcommon.InstrumentationScope, span ptrace.Span) { |
| 62 | + span.Status().SetCode(ptrace.StatusCodeUnset) |
| 63 | + }, |
| 64 | + wantErr: false, |
| 65 | + expected: map[string]any{ |
| 66 | + "@timestamp": "0.0", |
| 67 | + "duration": json.Number("0"), |
| 68 | + "kind": "Unspecified", |
| 69 | + "links": []any{}, |
| 70 | + "resource": map[string]any{}, |
| 71 | + "scope": map[string]any{}, |
| 72 | + "status": map[string]any{}, |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + traces := ptrace.NewTraces() |
| 79 | + resourceSpans := traces.ResourceSpans().AppendEmpty() |
| 80 | + scopeSpans := resourceSpans.ScopeSpans().AppendEmpty() |
| 81 | + span := scopeSpans.Spans().AppendEmpty() |
| 82 | + tt.spanCustomizer(resourceSpans.Resource(), scopeSpans.Scope(), span) |
| 83 | + traces.MarkReadOnly() |
| 84 | + |
| 85 | + var buf bytes.Buffer |
| 86 | + ser, err := New() |
| 87 | + require.NoError(t, err) |
| 88 | + err = ser.SerializeSpan(resourceSpans.Resource(), "", scopeSpans.Scope(), "", span, elasticsearch.Index{}, &buf) |
| 89 | + if (err != nil) != tt.wantErr { |
| 90 | + t.Errorf("SerializeSpan() error = %v, wantErr %v", err, tt.wantErr) |
| 91 | + } |
| 92 | + spanBytes := buf.Bytes() |
| 93 | + eventAsJSON := string(spanBytes) |
| 94 | + var result any |
| 95 | + decoder := json.NewDecoder(bytes.NewBuffer(spanBytes)) |
| 96 | + decoder.UseNumber() |
| 97 | + if err := decoder.Decode(&result); err != nil { |
| 98 | + t.Error(err) |
| 99 | + } |
| 100 | + |
| 101 | + assert.Equal(t, tt.expected, result, eventAsJSON) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments