Debian Codegraph @

processor/processorhelper/metrics_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package processorhelper
5
6 import (
7 "context"
8 "errors"
9 "sync"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "go.opentelemetry.io/otel/attribute"
15 "go.opentelemetry.io/otel/sdk/metric/metricdata"
16 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
17
18 "go.opentelemetry.io/collector/component"
19 "go.opentelemetry.io/collector/component/componenttest"
20 "go.opentelemetry.io/collector/consumer"
21 "go.opentelemetry.io/collector/consumer/consumertest"
22 "go.opentelemetry.io/collector/pdata/pmetric"
23 "go.opentelemetry.io/collector/processor/processorhelper/internal/metadatatest"
24 "go.opentelemetry.io/collector/processor/processortest"
25 )
26
27 var testMetricsCfg = struct{}{}
28
29 func TestNewMetrics(t *testing.T) {
30 mp, err := NewMetrics(context.Background(), processortest.NewNopSettings(processortest.NopType), &testMetricsCfg, consumertest.NewNop(), newTestMProcessor(nil))
31 require.NoError(t, err)
32
33 assert.True(t, mp.Capabilities().MutatesData)
34 assert.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))
35 assert.NoError(t, mp.ConsumeMetrics(context.Background(), pmetric.NewMetrics()))
36 assert.NoError(t, mp.Shutdown(context.Background()))
37 }
38
39 func TestNewMetrics_WithOptions(t *testing.T) {
40 want := errors.New("my_error")
41 mp, err := NewMetrics(context.Background(), processortest.NewNopSettings(processortest.NopType), &testMetricsCfg, consumertest.NewNop(), newTestMProcessor(nil),
42 WithStart(func(context.Context, component.Host) error { return want }),
43 WithShutdown(func(context.Context) error { return want }),
44 WithCapabilities(consumer.Capabilities{MutatesData: false}))
45 require.NoError(t, err)
46
47 assert.Equal(t, want, mp.Start(context.Background(), componenttest.NewNopHost()))
48 assert.Equal(t, want, mp.Shutdown(context.Background()))
49 assert.False(t, mp.Capabilities().MutatesData)
50 }
51
52 func TestNewMetrics_NilRequiredFields(t *testing.T) {
53 _, err := NewMetrics(context.Background(), processortest.NewNopSettings(processortest.NopType), &testMetricsCfg, consumertest.NewNop(), nil)
54 assert.Error(t, err)
55 }
56
57 func TestNewMetrics_ProcessMetricsError(t *testing.T) {
58 want := errors.New("my_error")
59 mp, err := NewMetrics(context.Background(), processortest.NewNopSettings(processortest.NopType), &testMetricsCfg, consumertest.NewNop(), newTestMProcessor(want))
60 require.NoError(t, err)
61 assert.Equal(t, want, mp.ConsumeMetrics(context.Background(), pmetric.NewMetrics()))
62 }
63
64 func TestNewMetrics_ProcessMetricsErrSkipProcessingData(t *testing.T) {
65 mp, err := NewMetrics(context.Background(), processortest.NewNopSettings(processortest.NopType), &testMetricsCfg, consumertest.NewNop(), newTestMProcessor(ErrSkipProcessingData))
66 require.NoError(t, err)
67 assert.NoError(t, mp.ConsumeMetrics(context.Background(), pmetric.NewMetrics()))
68 }
69
70 func newTestMProcessor(retError error) ProcessMetricsFunc {
71 return func(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
72 return md, retError
73 }
74 }
75
76 func TestMetricsConcurrency(t *testing.T) {
77 metricsFunc := func(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
78 return md, nil
79 }
80
81 incomingMetrics := pmetric.NewMetrics()
82 dps := incomingMetrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints()
83
84 // Add 2 data points to the incoming
85 dps.AppendEmpty()
86 dps.AppendEmpty()
87
88 mp, err := NewMetrics(context.Background(), processortest.NewNopSettings(processortest.NopType), &testLogsCfg, consumertest.NewNop(), metricsFunc)
89 require.NoError(t, err)
90 assert.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))
91
92 var wg sync.WaitGroup
93 for range 10 {
94 wg.Add(1)
95 go func() {
96 defer wg.Done()
97 for range 10000 {
98 assert.NoError(t, mp.ConsumeMetrics(context.Background(), incomingMetrics))
99 }
100 }()
101 }
102 wg.Wait()
103 assert.NoError(t, mp.Shutdown(context.Background()))
104 }
105
106 func TestMetrics_RecordInOut(t *testing.T) {
107 // Regardless of how many data points are ingested, emit 3
108 mockAggregate := func(_ context.Context, _ pmetric.Metrics) (pmetric.Metrics, error) {
109 md := pmetric.NewMetrics()
110 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty()
111 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty()
112 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty()
113 return md, nil
114 }
115
116 incomingMetrics := pmetric.NewMetrics()
117 dps := incomingMetrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints()
118
119 // Add 2 data points to the incoming
120 dps.AppendEmpty()
121 dps.AppendEmpty()
122
123 tel := componenttest.NewTelemetry()
124 mp, err := NewMetrics(context.Background(), newSettings(tel), &testMetricsCfg, consumertest.NewNop(), mockAggregate)
125 require.NoError(t, err)
126
127 assert.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))
128 assert.NoError(t, mp.ConsumeMetrics(context.Background(), incomingMetrics))
129 assert.NoError(t, mp.Shutdown(context.Background()))
130
131 metadatatest.AssertEqualProcessorIncomingItems(t, tel,
132 []metricdata.DataPoint[int64]{
133 {
134 Value: 2,
135 Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "metrics")),
136 },
137 }, metricdatatest.IgnoreTimestamp())
138 metadatatest.AssertEqualProcessorOutgoingItems(t, tel,
139 []metricdata.DataPoint[int64]{
140 {
141 Value: 3,
142 Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "metrics")),
143 },
144 }, metricdatatest.IgnoreTimestamp())
145 }
146
147 func TestMetrics_RecordIn_ErrorOut(t *testing.T) {
148 /// Regardless of input, return error
149 mockErr := func(_ context.Context, _ pmetric.Metrics) (pmetric.Metrics, error) {
150 return pmetric.NewMetrics(), errors.New("fake")
151 }
152
153 incomingMetrics := pmetric.NewMetrics()
154 dps := incomingMetrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints()
155
156 // Add 2 data points to the incoming
157 dps.AppendEmpty()
158 dps.AppendEmpty()
159
160 tel := componenttest.NewTelemetry()
161 mp, err := NewMetrics(context.Background(), newSettings(tel), &testMetricsCfg, consumertest.NewNop(), mockErr)
162 require.NoError(t, err)
163
164 require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))
165 require.Error(t, mp.ConsumeMetrics(context.Background(), incomingMetrics))
166 require.NoError(t, mp.Shutdown(context.Background()))
167
168 metadatatest.AssertEqualProcessorIncomingItems(t, tel,
169 []metricdata.DataPoint[int64]{
170 {
171 Value: 2,
172 Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "metrics")),
173 },
174 }, metricdatatest.IgnoreTimestamp())
175 metadatatest.AssertEqualProcessorOutgoingItems(t, tel,
176 []metricdata.DataPoint[int64]{
177 {
178 Value: 0,
179 Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "metrics")),
180 },
181 }, metricdatatest.IgnoreTimestamp())
182 }
183
184 func TestMetrics_ProcessInternalDuration(t *testing.T) {
185 mockAggregate := func(_ context.Context, _ pmetric.Metrics) (pmetric.Metrics, error) {
186 md := pmetric.NewMetrics()
187 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty()
188 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty()
189 md.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty().SetEmptySum().DataPoints().AppendEmpty()
190 return md, nil
191 }
192
193 incomingMetrics := pmetric.NewMetrics()
194
195 tel := componenttest.NewTelemetry()
196 mp, err := NewMetrics(context.Background(), newSettings(tel), &testMetricsCfg, consumertest.NewNop(), mockAggregate)
197 require.NoError(t, err)
198
199 assert.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))
200 assert.NoError(t, mp.ConsumeMetrics(context.Background(), incomingMetrics))
201 assert.NoError(t, mp.Shutdown(context.Background()))
202
203 metadatatest.AssertEqualProcessorInternalDuration(t, tel,
204 []metricdata.HistogramDataPoint[float64]{
205 {
206 Count: 1,
207 BucketCounts: []uint64{1},
208 Attributes: attribute.NewSet(attribute.String("processor", "processorhelper"), attribute.String("otel.signal", "metrics")),
209 },
210 }, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
211 }