-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
generated_component_telemetry_test.go
76 lines (61 loc) · 2.04 KB
/
generated_component_telemetry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Code generated by mdatagen. DO NOT EDIT.
package statsdreceiver
import (
"context"
"testing"
"github.com/stretchr/testify/require"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/receivertest"
)
type componentTestTelemetry struct {
reader *sdkmetric.ManualReader
meterProvider *sdkmetric.MeterProvider
}
func (tt *componentTestTelemetry) NewSettings() receiver.Settings {
settings := receivertest.NewNopSettings()
settings.MeterProvider = tt.meterProvider
settings.ID = component.NewID(component.MustNewType("statsd"))
return settings
}
func setupTestTelemetry() componentTestTelemetry {
reader := sdkmetric.NewManualReader()
return componentTestTelemetry{
reader: reader,
meterProvider: sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)),
}
}
func (tt *componentTestTelemetry) assertMetrics(t *testing.T, expected []metricdata.Metrics) {
var md metricdata.ResourceMetrics
require.NoError(t, tt.reader.Collect(context.Background(), &md))
// ensure all required metrics are present
for _, want := range expected {
got := tt.getMetric(want.Name, md)
metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp())
}
// ensure no additional metrics are emitted
require.Equal(t, len(expected), tt.len(md))
}
func (tt *componentTestTelemetry) getMetric(name string, got metricdata.ResourceMetrics) metricdata.Metrics {
for _, sm := range got.ScopeMetrics {
for _, m := range sm.Metrics {
if m.Name == name {
return m
}
}
}
return metricdata.Metrics{}
}
func (tt *componentTestTelemetry) len(got metricdata.ResourceMetrics) int {
metricsCount := 0
for _, sm := range got.ScopeMetrics {
metricsCount += len(sm.Metrics)
}
return metricsCount
}
func (tt *componentTestTelemetry) Shutdown(ctx context.Context) error {
return tt.meterProvider.Shutdown(ctx)
}