-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmetrics.go
228 lines (196 loc) · 6.55 KB
/
metrics.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package e2emon
import (
"context"
"math"
"github.com/efficientgo/core/backoff"
"github.com/efficientgo/e2e/monitoring/matchers"
io_prometheus_client "github.com/prometheus/client_model/go"
)
// GetMetricValueFunc defined the signature of a function used to get the metric value.
type getMetricValueFunc func(m *io_prometheus_client.Metric) float64
// MetricsOption defined the signature of a function used to manipulate options.
type MetricsOption func(*metricsOptions)
// metricsOptions is the structure holding all options.
type metricsOptions struct {
getValue getMetricValueFunc
labelMatchers []*matchers.Matcher
waitMissingMetrics bool
skipMissingMetrics bool
waitBackoff *backoff.Backoff
}
// WithWaitBackoff is an option to configure a backoff when waiting on a metric value.
func WithWaitBackoff(backoffConfig *backoff.Config) MetricsOption {
return func(o *metricsOptions) {
o.waitBackoff = backoff.New(context.Background(), *backoffConfig)
}
}
// WithMetricCount is an option to get the histogram/summary count as metric value.
func WithMetricCount() MetricsOption {
return func(o *metricsOptions) {
o.getValue = getMetricCount
}
}
// WithLabelMatchers is an option to filter only matching series.
func WithLabelMatchers(matchers ...*matchers.Matcher) MetricsOption {
return func(o *metricsOptions) {
o.labelMatchers = matchers
}
}
// WaitMissingMetrics is an option to wait whenever an expected metric is missing. If this
// option is not enabled, will return error on missing metrics.
func WaitMissingMetrics() MetricsOption {
return func(o *metricsOptions) {
o.waitMissingMetrics = true
}
}
// SkipMissingMetrics is an option to skip/ignore whenever an expected metric is missing.
func SkipMissingMetrics() MetricsOption {
return func(o *metricsOptions) {
o.skipMissingMetrics = true
}
}
func getMetricValue(m *io_prometheus_client.Metric) float64 {
if m.GetGauge() != nil {
return m.GetGauge().GetValue()
} else if m.GetCounter() != nil {
return m.GetCounter().GetValue()
} else if m.GetHistogram() != nil {
return m.GetHistogram().GetSampleSum()
} else if m.GetSummary() != nil {
return m.GetSummary().GetSampleSum()
} else {
return 0
}
}
func getMetricCount(m *io_prometheus_client.Metric) float64 {
if m.GetHistogram() != nil {
return float64(m.GetHistogram().GetSampleCount())
} else if m.GetSummary() != nil {
return float64(m.GetSummary().GetSampleCount())
} else {
return 0
}
}
func getValues(metrics []*io_prometheus_client.Metric, opts metricsOptions) []float64 {
values := make([]float64, 0, len(metrics))
for _, m := range metrics {
values = append(values, opts.getValue(m))
}
return values
}
func filterMetrics(metrics []*io_prometheus_client.Metric, opts metricsOptions) []*io_prometheus_client.Metric {
// If no label matcher is configured, then no filtering should be done.
if len(opts.labelMatchers) == 0 {
return metrics
}
if len(metrics) == 0 {
return metrics
}
filtered := make([]*io_prometheus_client.Metric, 0, len(metrics))
for _, m := range metrics {
metricLabels := map[string]string{}
for _, lp := range m.GetLabel() {
metricLabels[lp.GetName()] = lp.GetValue()
}
matches := true
for _, matcher := range opts.labelMatchers {
if !matcher.Matches(metricLabels[matcher.Name]) {
matches = false
break
}
}
if !matches {
continue
}
filtered = append(filtered, m)
}
return filtered
}
func SumValues(values []float64) float64 {
sum := 0.0
for _, v := range values {
sum += v
}
return sum
}
func EqualsSingle(expected float64) func(float64) bool {
return func(v float64) bool {
return v == expected || (math.IsNaN(v) && math.IsNaN(expected))
}
}
type MetricValueExpectation func(sums ...float64) bool
// Equals is an MetricValueExpectation function for WaitSumMetrics that returns true if given single sum is equals to given value.
func Equals(value float64) MetricValueExpectation {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("equals: expected one value")
}
return sums[0] == value || math.IsNaN(sums[0]) && math.IsNaN(value)
}
}
// Greater is an isExpected function for WaitSumMetrics that returns true if given single sum is greater than given value.
func Greater(value float64) MetricValueExpectation {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("greater: expected one value")
}
return sums[0] > value
}
}
// GreaterOrEqual is an isExpected function for WaitSumMetrics that returns true if given single sum is greater or equal than given value.
func GreaterOrEqual(value float64) MetricValueExpectation {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("greater: expected one value")
}
return sums[0] >= value
}
}
// Less is an isExpected function for WaitSumMetrics that returns true if given single sum is less than given value.
func Less(value float64) MetricValueExpectation {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("less: expected one value")
}
return sums[0] < value
}
}
// Between is a MetricValueExpectation function for WaitSumMetrics that returns true if given single sum is between
// the lower and upper bounds (non-inclusive, as in `lower < x < upper`).
func Between(lower, upper float64) MetricValueExpectation {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("between: expected one value")
}
return sums[0] > lower && sums[0] < upper
}
}
// EqualsAmongTwo is an isExpected function for WaitSumMetrics that returns true if first sum is equal to the second.
// NOTE: Be careful on scrapes in between of process that changes two metrics. Those are
// usually not atomic.
func EqualsAmongTwo(sums ...float64) bool {
if len(sums) != 2 {
panic("equalsAmongTwo: expected two values")
}
return sums[0] == sums[1]
}
// GreaterAmongTwo is an isExpected function for WaitSumMetrics that returns true if first sum is greater than second.
// NOTE: Be careful on scrapes in between of process that changes two metrics. Those are
// usually not atomic.
func GreaterAmongTwo(sums ...float64) bool {
if len(sums) != 2 {
panic("greaterAmongTwo: expected two values")
}
return sums[0] > sums[1]
}
// LessAmongTwo is an isExpected function for WaitSumMetrics that returns true if first sum is smaller than second.
// NOTE: Be careful on scrapes in between of process that changes two metrics. Those are
// usually not atomic.
func LessAmongTwo(sums ...float64) bool {
if len(sums) != 2 {
panic("lessAmongTwo: expected two values")
}
return sums[0] < sums[1]
}