|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package jaegerencodingextension |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gogo/protobuf/jsonpb" |
| 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/pkg/translator/jaeger" |
| 17 | +) |
| 18 | + |
| 19 | +func TestUnmarshalJaeger(t *testing.T) { |
| 20 | + td := ptrace.NewTraces() |
| 21 | + span := td.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty() |
| 22 | + span.SetName("foo") |
| 23 | + span.SetStartTimestamp(pcommon.Timestamp(10)) |
| 24 | + span.SetEndTimestamp(pcommon.Timestamp(20)) |
| 25 | + span.SetTraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) |
| 26 | + span.SetSpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}) |
| 27 | + batches, err := jaeger.ProtoFromTraces(td) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + protoBytes, err := batches[0].Spans[0].Marshal() |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + jsonMarshaler := &jsonpb.Marshaler{} |
| 34 | + jsonBytes := new(bytes.Buffer) |
| 35 | + require.NoError(t, jsonMarshaler.Marshal(jsonBytes, batches[0].Spans[0])) |
| 36 | + |
| 37 | + tests := []struct { |
| 38 | + unmarshaler ptrace.Unmarshaler |
| 39 | + encoding string |
| 40 | + bytes []byte |
| 41 | + }{ |
| 42 | + { |
| 43 | + unmarshaler: jaegerProtobufTrace{}, |
| 44 | + encoding: "jaeger_proto", |
| 45 | + bytes: protoBytes, |
| 46 | + }, |
| 47 | + { |
| 48 | + unmarshaler: jaegerJSONTrace{}, |
| 49 | + encoding: "jaeger_json", |
| 50 | + bytes: jsonBytes.Bytes(), |
| 51 | + }, |
| 52 | + } |
| 53 | + for _, test := range tests { |
| 54 | + t.Run(test.encoding, func(t *testing.T) { |
| 55 | + got, err := test.unmarshaler.UnmarshalTraces(test.bytes) |
| 56 | + require.NoError(t, err) |
| 57 | + assert.Equal(t, td, got) |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
0 commit comments