forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generated_component_telemetry_test.go
78 lines (64 loc) · 2.14 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
// Code generated by mdatagen. DO NOT EDIT.
package service
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric"
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"
)
type componentTestTelemetry struct {
reader *sdkmetric.ManualReader
meterProvider *sdkmetric.MeterProvider
}
func (tt *componentTestTelemetry) newTelemetrySettings() component.TelemetrySettings {
set := componenttest.NewNopTelemetrySettings()
set.MeterProvider = tt.meterProvider
set.LeveledMeterProvider = func(_ configtelemetry.Level) metric.MeterProvider {
return tt.meterProvider
}
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)
}