Debian Codegraph @

pdata/pmetric/pmetricotlp/request_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pmetricotlp
5
6 import (
7 "encoding/json"
8 "strings"
9 "testing"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 gootlpcollectormetrics "go.opentelemetry.io/proto/slim/otlp/collector/metrics/v1"
14 goproto "google.golang.org/protobuf/proto"
15
16 "go.opentelemetry.io/collector/pdata/internal"
17 "go.opentelemetry.io/collector/pdata/internal/otlp"
18 "go.opentelemetry.io/collector/pdata/pmetric"
19 )
20
21 var (
22 _ json.Unmarshaler = ExportRequest{}
23 _ json.Marshaler = ExportRequest{}
24 )
25
26 var metricsRequestJSON = []byte(`
27 {
28 "resourceMetrics": [
29 {
30 "resource": {},
31 "scopeMetrics": [
32 {
33 "scope": {},
34 "metrics": [
35 {
36 "name": "test_metric"
37 }
38 ]
39 }
40 ]
41 }
42 ]
43 }`)
44
45 func TestRequestToPData(t *testing.T) {
46 tr := NewExportRequest()
47 assert.Equal(t, 0, tr.Metrics().MetricCount())
48 tr.Metrics().ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty()
49 assert.Equal(t, 1, tr.Metrics().MetricCount())
50 }
51
52 func TestRequestJSON(t *testing.T) {
53 mr := NewExportRequest()
54 require.NoError(t, mr.UnmarshalJSON(metricsRequestJSON))
55 assert.Equal(t, "test_metric", mr.Metrics().ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Name())
56
57 got, err := mr.MarshalJSON()
58 require.NoError(t, err)
59 assert.Equal(t, strings.Join(strings.Fields(string(metricsRequestJSON)), ""), string(got))
60 }
61
62 func TestMetricsProtoWireCompatibility(t *testing.T) {
63 // This test verifies that OTLP ProtoBufs generated using goproto lib in
64 // opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in
65 // this repository are wire compatible.
66
67 // Generate Metrics as pdata struct.
68 md := NewExportRequestFromMetrics(pmetric.Metrics(internal.GenTestMetricsWrapper()))
69
70 // Marshal its underlying ProtoBuf to wire.
71 wire1, err := md.MarshalProto()
72 require.NoError(t, err)
73 assert.NotNil(t, wire1)
74
75 // Unmarshal from the wire to OTLP Protobuf in goproto's representation.
76 var goprotoMessage gootlpcollectormetrics.ExportMetricsServiceRequest
77 err = goproto.Unmarshal(wire1, &goprotoMessage)
78 require.NoError(t, err)
79
80 // Marshal to the wire again.
81 wire2, err := goproto.Marshal(&goprotoMessage)
82 require.NoError(t, err)
83 assert.NotNil(t, wire2)
84
85 // Unmarshal from the wire into gogoproto's representation.
86 md2 := NewExportRequest()
87 err = md2.UnmarshalProto(wire2)
88 require.NoError(t, err)
89
90 // Now compare that the original and final ProtoBuf messages are the same.
91 // This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
92 // Migration logic will run, so run it on the original message as well.
93 otlp.MigrateMetrics(md.orig.ResourceMetrics)
94 assert.Equal(t, md, md2)
95 }