| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package ptrace |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
gootlptrace "go.opentelemetry.io/proto/slim/otlp/trace/v1" |
| 13 |
goproto "google.golang.org/protobuf/proto" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 16 |
) |
| 17 |
|
| 18 |
func TestTracesProtoWireCompatibility(t *testing.T) { |
| 19 |
// This test verifies that OTLP ProtoBufs generated using goproto lib in |
| 20 |
// opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in |
| 21 |
// this repository are wire compatible. |
| 22 |
|
| 23 |
// Generate Traces as pdata struct. |
| 24 |
td := generateTestTraces() |
| 25 |
|
| 26 |
// Marshal its underlying ProtoBuf to wire. |
| 27 |
marshaler := &ProtoMarshaler{} |
| 28 |
wire1, err := marshaler.MarshalTraces(td) |
| 29 |
require.NoError(t, err) |
| 30 |
assert.NotNil(t, wire1) |
| 31 |
|
| 32 |
// Unmarshal from the wire to OTLP Protobuf in goproto's representation. |
| 33 |
var goprotoMessage gootlptrace.TracesData |
| 34 |
err = goproto.Unmarshal(wire1, &goprotoMessage) |
| 35 |
require.NoError(t, err) |
| 36 |
|
| 37 |
// Marshal to the wire again. |
| 38 |
wire2, err := goproto.Marshal(&goprotoMessage) |
| 39 |
require.NoError(t, err) |
| 40 |
assert.NotNil(t, wire2) |
| 41 |
|
| 42 |
// Unmarshal from the wire into gogoproto's representation. |
| 43 |
var td2 Traces |
| 44 |
unmarshaler := &ProtoUnmarshaler{} |
| 45 |
td2, err = unmarshaler.UnmarshalTraces(wire2) |
| 46 |
require.NoError(t, err) |
| 47 |
|
| 48 |
// Now compare that the original and final ProtoBuf messages are the same. |
| 49 |
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible. |
| 50 |
assert.Equal(t, td, td2) |
| 51 |
} |
| 52 |
|
| 53 |
func TestProtoTracesUnmarshalerError(t *testing.T) { |
| 54 |
p := &ProtoUnmarshaler{} |
| 55 |
_, err := p.UnmarshalTraces([]byte("+$%")) |
| 56 |
assert.Error(t, err) |
| 57 |
} |
| 58 |
|
| 59 |
func TestProtoSizer(t *testing.T) { |
| 60 |
marshaler := &ProtoMarshaler{} |
| 61 |
td := NewTraces() |
| 62 |
rms := td.ResourceSpans() |
| 63 |
rms.AppendEmpty().ScopeSpans().AppendEmpty().Spans().AppendEmpty().SetName("foo") |
| 64 |
|
| 65 |
size := marshaler.TracesSize(td) |
| 66 |
|
| 67 |
bytes, err := marshaler.MarshalTraces(td) |
| 68 |
require.NoError(t, err) |
| 69 |
assert.Equal(t, len(bytes), size) |
| 70 |
} |
| 71 |
|
| 72 |
func TestProtoSizerEmptyTraces(t *testing.T) { |
| 73 |
sizer := &ProtoMarshaler{} |
| 74 |
assert.Equal(t, 0, sizer.TracesSize(NewTraces())) |
| 75 |
} |
| 76 |
|
| 77 |
func BenchmarkTracesToProto(b *testing.B) { |
| 78 |
marshaler := &ProtoMarshaler{} |
| 79 |
traces := generateBenchmarkTraces(128) |
| 80 |
|
| 81 |
for b.Loop() { |
| 82 |
buf, err := marshaler.MarshalTraces(traces) |
| 83 |
require.NoError(b, err) |
| 84 |
assert.NotEmpty(b, buf) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func BenchmarkTracesFromProto(b *testing.B) { |
| 89 |
marshaler := &ProtoMarshaler{} |
| 90 |
unmarshaler := &ProtoUnmarshaler{} |
| 91 |
baseTraces := generateBenchmarkTraces(128) |
| 92 |
buf, err := marshaler.MarshalTraces(baseTraces) |
| 93 |
require.NoError(b, err) |
| 94 |
assert.NotEmpty(b, buf) |
| 95 |
|
| 96 |
b.ReportAllocs() |
| 97 |
for b.Loop() { |
| 98 |
traces, err := unmarshaler.UnmarshalTraces(buf) |
| 99 |
require.NoError(b, err) |
| 100 |
assert.Equal(b, baseTraces.ResourceSpans().Len(), traces.ResourceSpans().Len()) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func generateBenchmarkTraces(metricsCount int) Traces { |
| 105 |
now := time.Now() |
| 106 |
startTime := pcommon.NewTimestampFromTime(now.Add(-10 * time.Second)) |
| 107 |
endTime := pcommon.NewTimestampFromTime(now) |
| 108 |
|
| 109 |
md := NewTraces() |
| 110 |
ilm := md.ResourceSpans().AppendEmpty().ScopeSpans().AppendEmpty() |
| 111 |
ilm.Spans().EnsureCapacity(metricsCount) |
| 112 |
for range metricsCount { |
| 113 |
im := ilm.Spans().AppendEmpty() |
| 114 |
im.SetName("test_name") |
| 115 |
im.SetStartTimestamp(startTime) |
| 116 |
im.SetEndTimestamp(endTime) |
| 117 |
} |
| 118 |
return md |
| 119 |
} |