-
Notifications
You must be signed in to change notification settings - Fork 796
/
metrics.go
183 lines (160 loc) · 5.01 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
package e2e
import (
"math"
io_prometheus_client "github.com/prometheus/client_model/go"
)
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))
}
}
// Equals is an isExpected function for WaitSumMetrics that returns true if given single sum is equals to given value.
func Equals(value float64) func(sums ...float64) bool {
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) func(sums ...float64) bool {
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) func(sums ...float64) bool {
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) func(sums ...float64) bool {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("less: expected one value")
}
return sums[0] < value
}
}
// LessOrEqual is an isExpected function for WaitSumMetrics that returns true if given single sum is less or equal than given value.
func LessOrEqual(value float64) func(sums ...float64) bool {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("less: expected one value")
}
return sums[0] <= value
}
}
// EqualsAmong is an isExpected function for WaitSumMetrics that returns true if the first sum is equal to any value provided.
func EqualsAmong(values ...float64) func(sums ...float64) bool {
return func(sums ...float64) bool {
if len(sums) != 1 {
panic("equals among: expected one value")
}
for _, value := range values {
if sums[0] == value {
return true
}
}
return false
}
}
// 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]
}