Debian Codegraph @

pdata/pmetric/pb_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pmetric
5
6 import (
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 gootlpmetrics "go.opentelemetry.io/proto/slim/otlp/metrics/v1"
13 goproto "google.golang.org/protobuf/proto"
14
15 "go.opentelemetry.io/collector/pdata/pcommon"
16 )
17
18 func TestMetricsProtoWireCompatibility(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 Metrics as pdata struct.
24 td := generateTestMetrics()
25
26 // Marshal its underlying ProtoBuf to wire.
27 marshaler := &ProtoMarshaler{}
28 wire1, err := marshaler.MarshalMetrics(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 gootlpmetrics.MetricsData
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 Metrics
44 unmarshaler := &ProtoUnmarshaler{}
45 td2, err = unmarshaler.UnmarshalMetrics(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 TestProtoMetricsUnmarshalerError(t *testing.T) {
54 p := &ProtoUnmarshaler{}
55 _, err := p.UnmarshalMetrics([]byte("+$%"))
56 assert.Error(t, err)
57 }
58
59 func TestProtoSizer(t *testing.T) {
60 marshaler := &ProtoMarshaler{}
61 md := NewMetrics()
62 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetName("foo")
63
64 size := marshaler.MetricsSize(md)
65
66 bytes, err := marshaler.MarshalMetrics(md)
67 require.NoError(t, err)
68 assert.Equal(t, len(bytes), size)
69 }
70
71 func TestProtoSizerEmptyMetrics(t *testing.T) {
72 sizer := &ProtoMarshaler{}
73 assert.Equal(t, 0, sizer.MetricsSize(NewMetrics()))
74 }
75
76 func BenchmarkMetricsToProto(b *testing.B) {
77 marshaler := &ProtoMarshaler{}
78 metrics := generateBenchmarkMetrics(128)
79
80 for b.Loop() {
81 buf, err := marshaler.MarshalMetrics(metrics)
82 require.NoError(b, err)
83 assert.NotEmpty(b, buf)
84 }
85 }
86
87 func BenchmarkMetricsFromProto(b *testing.B) {
88 marshaler := &ProtoMarshaler{}
89 unmarshaler := &ProtoUnmarshaler{}
90 baseMetrics := generateBenchmarkMetrics(128)
91 buf, err := marshaler.MarshalMetrics(baseMetrics)
92 require.NoError(b, err)
93 assert.NotEmpty(b, buf)
94
95 b.ReportAllocs()
96 for b.Loop() {
97 metrics, err := unmarshaler.UnmarshalMetrics(buf)
98 require.NoError(b, err)
99 assert.Equal(b, baseMetrics.ResourceMetrics().Len(), metrics.ResourceMetrics().Len())
100 }
101 }
102
103 func generateBenchmarkMetrics(metricsCount int) Metrics {
104 now := time.Now()
105 startTime := pcommon.NewTimestampFromTime(now.Add(-10 * time.Second))
106 endTime := pcommon.NewTimestampFromTime(now)
107
108 md := NewMetrics()
109 ilm := md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty()
110 ilm.Metrics().EnsureCapacity(metricsCount)
111 for range metricsCount {
112 im := ilm.Metrics().AppendEmpty()
113 im.SetName("test_name")
114 idp := im.SetEmptySum().DataPoints().AppendEmpty()
115 idp.SetStartTimestamp(startTime)
116 idp.SetTimestamp(endTime)
117 idp.SetIntValue(123)
118 }
119 return md
120 }