forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_metrics_exporter.go
More file actions
190 lines (165 loc) · 6.01 KB
/
Copy pathtest_metrics_exporter.go
File metadata and controls
190 lines (165 loc) · 6.01 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package telemetry
import (
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)
// TestScopeName is the name that the metrics running under test will have
const TestScopeName string = "argo-workflows-test"
// TestExporter is an opentelemetry metrics exporter, purely for use within
// tests. It is not possible to query the values of an instrument via the otel
// SDK, so this exporter provides methods by which you can request
// metrics by name+attributes and therefore inspect whether they exist, and
// their values for the purposes of testing only.
// This is a public structure as it is used outside of this module also.
type TestMetricsExporter struct {
metric.Reader
}
var _ metric.Reader = &TestMetricsExporter{}
func NewTestMetricsExporter() *TestMetricsExporter {
reader := metric.NewManualReader()
e := &TestMetricsExporter{
Reader: reader,
}
return e
}
func (t *TestMetricsExporter) getOurMetrics() (*[]metricdata.Metrics, error) {
metrics := metricdata.ResourceMetrics{}
err := t.Collect(context.TODO(), &metrics)
if err != nil {
return nil, err
}
for _, scope := range metrics.ScopeMetrics {
if scope.Scope.Name == TestScopeName {
return &scope.Metrics, nil
}
}
return nil, fmt.Errorf("%s scope not found", TestScopeName)
}
func (t *TestMetricsExporter) getNamedMetric(name string) (*metricdata.Metrics, error) {
mtcs, err := t.getOurMetrics()
if err != nil {
return nil, err
}
for _, metric := range *mtcs {
if metric.Name == name {
return &metric, nil
}
}
return nil, fmt.Errorf("%s named metric not found in %v", name, mtcs)
}
func (t *TestMetricsExporter) getNamedInt64CounterData(name string, attribs *attribute.Set) (*metricdata.DataPoint[int64], error) {
mtc, err := t.getNamedMetric(name)
if err != nil {
return nil, err
}
if counter, ok := mtc.Data.(metricdata.Sum[int64]); ok {
for _, dataPoint := range counter.DataPoints {
if dataPoint.Attributes.Equals(attribs) {
return &dataPoint, nil
}
}
return nil, fmt.Errorf("%s named counter[int64] with attribs %v not found in %v", name, attribs, mtc)
}
return nil, fmt.Errorf("%s type counter[int64] not found in %v", name, mtc)
}
func (t *TestMetricsExporter) getNamedFloat64GaugeData(name string, attribs *attribute.Set) (*metricdata.DataPoint[float64], error) {
mtc, err := t.getNamedMetric(name)
if err != nil {
return nil, err
}
if gauge, ok := mtc.Data.(metricdata.Gauge[float64]); ok {
for _, dataPoint := range gauge.DataPoints {
if dataPoint.Attributes.Equals(attribs) {
return &dataPoint, nil
}
}
return nil, fmt.Errorf("%s named gauge[float64] with attribs %v not found in %v", name, attribs, mtc)
}
return nil, fmt.Errorf("%s type gauge[float64] not found in %v", name, mtc)
}
func (t *TestMetricsExporter) getNamedInt64GaugeData(name string, attribs *attribute.Set) (*metricdata.DataPoint[int64], error) {
mtc, err := t.getNamedMetric(name)
if err != nil {
return nil, err
}
gauge, ok := mtc.Data.(metricdata.Gauge[int64])
if !ok {
return nil, fmt.Errorf("%s type gauge[float64] not found in %v", name, mtc)
}
for _, dataPoint := range gauge.DataPoints {
if dataPoint.Attributes.Equals(attribs) {
return &dataPoint, nil
}
}
return nil, fmt.Errorf("%s named gauge[float64] with attribs %v not found in %v", name, attribs, mtc)
}
func (t *TestMetricsExporter) getNamedFloat64CounterData(name string, attribs *attribute.Set) (*metricdata.DataPoint[float64], error) {
mtc, err := t.getNamedMetric(name)
if err != nil {
return nil, err
}
if counter, ok := mtc.Data.(metricdata.Sum[float64]); ok {
for _, dataPoint := range counter.DataPoints {
if dataPoint.Attributes.Equals(attribs) {
return &dataPoint, nil
}
}
return nil, fmt.Errorf("%s named counter[float64] with attribs %v not found in %v", name, attribs, mtc)
}
return nil, fmt.Errorf("%s type counter[float64] not found in %v", name, mtc)
}
func (t *TestMetricsExporter) getNamedFloat64HistogramData(name string, attribs *attribute.Set) (*metricdata.HistogramDataPoint[float64], error) {
mtc, err := t.getNamedMetric(name)
if err != nil {
return nil, err
}
if histogram, ok := mtc.Data.(metricdata.Histogram[float64]); ok {
for _, dataPoint := range histogram.DataPoints {
if dataPoint.Attributes.Equals(attribs) {
return &dataPoint, nil
}
}
return nil, fmt.Errorf("%s named counter[float64] with attribs %v not found in %v", name, attribs, mtc)
}
return nil, fmt.Errorf("%s type counter[float64] not found in %v", name, mtc)
}
// GetFloat64HistogramData returns an otel histogram float64 data point for test reads
func (t *TestMetricsExporter) GetFloat64HistogramData(name string, attribs *attribute.Set) (*metricdata.HistogramDataPoint[float64], error) {
data, err := t.getNamedFloat64HistogramData(name, attribs)
return data, err
}
// GetInt64CounterValue returns an otel int64 counter value for test reads
func (t *TestMetricsExporter) GetInt64CounterValue(name string, attribs *attribute.Set) (int64, error) {
counter, err := t.getNamedInt64CounterData(name, attribs)
if err != nil {
return 0, err
}
return counter.Value, err
}
// GetFloat64GaugeValue returns an otel float64 gauge value for test reads
func (t *TestMetricsExporter) GetFloat64GaugeValue(name string, attribs *attribute.Set) (float64, error) {
gauge, err := t.getNamedFloat64GaugeData(name, attribs)
if err != nil {
return 0, err
}
return gauge.Value, err
}
// GetInt64GaugeValue returns an otel int64 gauge value for test reads
func (t *TestMetricsExporter) GetInt64GaugeValue(name string, attribs *attribute.Set) (int64, error) {
gauge, err := t.getNamedInt64GaugeData(name, attribs)
if err != nil {
return 0, err
}
return gauge.Value, err
}
// GetFloat64CounterValue returns an otel float64 counter value for test reads
func (t *TestMetricsExporter) GetFloat64CounterValue(name string, attribs *attribute.Set) (float64, error) {
counter, err := t.getNamedFloat64CounterData(name, attribs)
if err != nil {
return 0, err
}
return counter.Value, err
}