Debian Codegraph @

bridge/opencensus/metric_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus"
5
6 import (
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/require"
11 ocmetricdata "go.opencensus.io/metric/metricdata"
12 "go.opencensus.io/metric/metricproducer"
13 ocresource "go.opencensus.io/resource"
14
15 "go.opentelemetry.io/otel/attribute"
16 "go.opentelemetry.io/otel/sdk/instrumentation"
17 "go.opentelemetry.io/otel/sdk/metric/metricdata"
18 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
19 )
20
21 func TestMetricProducer(t *testing.T) {
22 now := time.Now()
23 for _, tc := range []struct {
24 desc string
25 input []*ocmetricdata.Metric
26 expected []metricdata.ScopeMetrics
27 expectErr bool
28 }{
29 {
30 desc: "empty",
31 expected: nil,
32 },
33 {
34 desc: "success",
35 input: []*ocmetricdata.Metric{
36 {
37 Resource: &ocresource.Resource{
38 Labels: map[string]string{
39 "R1": "V1",
40 "R2": "V2",
41 },
42 },
43 TimeSeries: []*ocmetricdata.TimeSeries{
44 {
45 StartTime: now,
46 Points: []ocmetricdata.Point{
47 {Value: int64(123), Time: now},
48 },
49 },
50 },
51 },
52 },
53 expected: []metricdata.ScopeMetrics{{
54 Scope: instrumentation.Scope{
55 Name: scopeName,
56 Version: Version(),
57 },
58 Metrics: []metricdata.Metrics{
59 {
60 Data: metricdata.Gauge[int64]{
61 DataPoints: []metricdata.DataPoint[int64]{
62 {
63 Attributes: attribute.NewSet(),
64 StartTime: now,
65 Time: now,
66 Value: 123,
67 },
68 },
69 },
70 },
71 },
72 }},
73 },
74 {
75 desc: "partial success",
76 input: []*ocmetricdata.Metric{
77 {
78 Descriptor: ocmetricdata.Descriptor{
79 Name: "foo.com/bad-point",
80 Description: "a bad type",
81 Unit: ocmetricdata.UnitDimensionless,
82 Type: ocmetricdata.TypeGaugeDistribution,
83 },
84 },
85 {
86 Resource: &ocresource.Resource{
87 Labels: map[string]string{
88 "R1": "V1",
89 "R2": "V2",
90 },
91 },
92 TimeSeries: []*ocmetricdata.TimeSeries{
93 {
94 StartTime: now,
95 Points: []ocmetricdata.Point{
96 {Value: int64(123), Time: now},
97 },
98 },
99 },
100 },
101 },
102 expected: []metricdata.ScopeMetrics{{
103 Scope: instrumentation.Scope{
104 Name: scopeName,
105 Version: Version(),
106 },
107 Metrics: []metricdata.Metrics{
108 {
109 Data: metricdata.Gauge[int64]{
110 DataPoints: []metricdata.DataPoint[int64]{
111 {
112 Attributes: attribute.NewSet(),
113 StartTime: now,
114 Time: now,
115 Value: 123,
116 },
117 },
118 },
119 },
120 },
121 }},
122 expectErr: true,
123 },
124 } {
125 t.Run(tc.desc, func(t *testing.T) {
126 fakeProducer := &fakeOCProducer{metrics: tc.input}
127 metricproducer.GlobalManager().AddProducer(fakeProducer)
128 defer metricproducer.GlobalManager().DeleteProducer(fakeProducer)
129 output, err := NewMetricProducer().Produce(t.Context())
130 if tc.expectErr {
131 require.Error(t, err)
132 } else {
133 require.NoError(t, err)
134 }
135 require.Len(t, tc.expected, len(output))
136 for i := range output {
137 metricdatatest.AssertEqual(t, tc.expected[i], output[i])
138 }
139 })
140 }
141 }
142
143 type fakeOCProducer struct {
144 metrics []*ocmetricdata.Metric
145 }
146
147 func (f *fakeOCProducer) Read() []*ocmetricdata.Metric {
148 return f.metrics
149 }