Debian Codegraph @

pdata/internal/unpacked_unmarshal_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal
5
6 import (
7 "encoding/binary"
8 "math"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "go.opentelemetry.io/collector/pdata/internal/proto"
15 )
16
17 // For each repeated field in the OTLP proto that should be packed, check that we can unmarshal
18 // payloads where it is encoded as unpacked.
19 // The Protobuf spec recommends this for backwards compatibility purposes.
20 // We do not test profiles payloads since their proto definition is currently in development.
21
22 func appendTag(buf []byte, fieldNo byte, wireType proto.WireType) []byte {
23 return append(buf, (fieldNo<<3)|byte(wireType))
24 }
25
26 func appendVarint(buf []byte, v uint64) []byte {
27 n := proto.Sov(v)
28 for range n {
29 buf = append(buf, 0)
30 }
31 _ = proto.EncodeVarint(buf, len(buf), v)
32 return buf
33 }
34
35 func TestUnmarshalUnpackedHistogramDataPoint(t *testing.T) {
36 var pb []byte
37 pb = appendTag(pb, 6, proto.WireTypeI64) // bucket_counts
38 pb = binary.LittleEndian.AppendUint64(pb, 42)
39 pb = appendTag(pb, 7, proto.WireTypeI64) // explicit_bounds
40 pb = binary.LittleEndian.AppendUint64(pb, math.Float64bits(42.0))
41
42 var hdp HistogramDataPoint
43 err := hdp.UnmarshalProto(pb)
44 require.NoError(t, err)
45 assert.Equal(t, HistogramDataPoint{
46 BucketCounts: []uint64{42},
47 ExplicitBounds: []float64{42.0},
48 }, hdp)
49 }
50
51 func TestUnmarshalUnpackedExponentialHistogramDataPoint_Buckets(t *testing.T) {
52 var pb []byte
53 pb = appendTag(pb, 2, proto.WireTypeVarint) // bucket_counts
54 pb = appendVarint(pb, 42)
55
56 var ehdpb ExponentialHistogramDataPointBuckets
57 err := ehdpb.UnmarshalProto(pb)
58 require.NoError(t, err)
59 assert.Equal(t, ExponentialHistogramDataPointBuckets{
60 BucketCounts: []uint64{42},
61 }, ehdpb)
62 }