| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package plog |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
gootlplogs "go.opentelemetry.io/proto/slim/otlp/logs/v1" |
| 13 |
goproto "google.golang.org/protobuf/proto" |
| 14 |
|
| 15 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 16 |
) |
| 17 |
|
| 18 |
func TestLogsProtoWireCompatibility(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 Logs as pdata struct. |
| 24 |
td := generateTestLogs() |
| 25 |
|
| 26 |
// Marshal its underlying ProtoBuf to wire. |
| 27 |
marshaler := &ProtoMarshaler{} |
| 28 |
wire1, err := marshaler.MarshalLogs(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 gootlplogs.LogsData |
| 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 Logs |
| 44 |
unmarshaler := &ProtoUnmarshaler{} |
| 45 |
td2, err = unmarshaler.UnmarshalLogs(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 TestProtoLogsUnmarshalerError(t *testing.T) { |
| 54 |
p := &ProtoUnmarshaler{} |
| 55 |
_, err := p.UnmarshalLogs([]byte("+$%")) |
| 56 |
assert.Error(t, err) |
| 57 |
} |
| 58 |
|
| 59 |
func TestProtoSizer(t *testing.T) { |
| 60 |
marshaler := &ProtoMarshaler{} |
| 61 |
ld := NewLogs() |
| 62 |
ld.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().SetSeverityText("error") |
| 63 |
|
| 64 |
size := marshaler.LogsSize(ld) |
| 65 |
|
| 66 |
bytes, err := marshaler.MarshalLogs(ld) |
| 67 |
require.NoError(t, err) |
| 68 |
assert.Equal(t, len(bytes), size) |
| 69 |
} |
| 70 |
|
| 71 |
func TestProtoSizerEmptyLogs(t *testing.T) { |
| 72 |
sizer := &ProtoMarshaler{} |
| 73 |
assert.Equal(t, 0, sizer.LogsSize(NewLogs())) |
| 74 |
} |
| 75 |
|
| 76 |
func BenchmarkLogsToProto(b *testing.B) { |
| 77 |
marshaler := &ProtoMarshaler{} |
| 78 |
logs := generateBenchmarkLogs(128) |
| 79 |
|
| 80 |
for b.Loop() { |
| 81 |
buf, err := marshaler.MarshalLogs(logs) |
| 82 |
require.NoError(b, err) |
| 83 |
assert.NotEmpty(b, buf) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func BenchmarkLogsFromProto(b *testing.B) { |
| 88 |
marshaler := &ProtoMarshaler{} |
| 89 |
unmarshaler := &ProtoUnmarshaler{} |
| 90 |
baseLogs := generateBenchmarkLogs(128) |
| 91 |
buf, err := marshaler.MarshalLogs(baseLogs) |
| 92 |
require.NoError(b, err) |
| 93 |
assert.NotEmpty(b, buf) |
| 94 |
|
| 95 |
b.ReportAllocs() |
| 96 |
for b.Loop() { |
| 97 |
logs, err := unmarshaler.UnmarshalLogs(buf) |
| 98 |
require.NoError(b, err) |
| 99 |
assert.Equal(b, baseLogs.ResourceLogs().Len(), logs.ResourceLogs().Len()) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func generateBenchmarkLogs(logsCount int) Logs { |
| 104 |
endTime := pcommon.NewTimestampFromTime(time.Now()) |
| 105 |
|
| 106 |
md := NewLogs() |
| 107 |
ilm := md.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty() |
| 108 |
ilm.LogRecords().EnsureCapacity(logsCount) |
| 109 |
for range logsCount { |
| 110 |
im := ilm.LogRecords().AppendEmpty() |
| 111 |
im.SetTimestamp(endTime) |
| 112 |
} |
| 113 |
return md |
| 114 |
} |