-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
generated_component_telemetry_test.go
84 lines (69 loc) · 2.39 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
77
78
79
80
81
82
83
84
// Code generated by mdatagen. DO NOT EDIT.
package grafanacloudconnector
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/component/componenttest"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/connector/connectortest"
)
type componentTestTelemetry struct {
reader *sdkmetric.ManualReader
meterProvider *sdkmetric.MeterProvider
}
func (tt *componentTestTelemetry) NewSettings() connector.Settings {
set := connectortest.NewNopSettings()
set.ID = component.NewID(component.MustNewType("grafanacloud"))
set.TelemetrySettings = tt.newTelemetrySettings()
return set
}
func (tt *componentTestTelemetry) newTelemetrySettings() component.TelemetrySettings {
set := componenttest.NewNopTelemetrySettings()
set.MeterProvider = tt.meterProvider
set.MetricsLevel = configtelemetry.LevelDetailed
return set
}
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)
}