-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathprometheus.go
More file actions
78 lines (64 loc) · 1.91 KB
/
Copy pathprometheus.go
File metadata and controls
78 lines (64 loc) · 1.91 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
package testutil
import (
"testing"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)
type kind string
const (
counterKind kind = "counter"
gaugeKind kind = "gauge"
)
func PromGaugeHasValue(t testing.TB, metrics []*dto.MetricFamily, value float64, name string, labels ...string) bool {
t.Helper()
return value == getValue(t, metrics, gaugeKind, name, labels...)
}
func PromCounterHasValue(t testing.TB, metrics []*dto.MetricFamily, value float64, name string, labels ...string) bool {
t.Helper()
return value == getValue(t, metrics, counterKind, name, labels...)
}
func PromGaugeAssertion(t testing.TB, metrics []*dto.MetricFamily, assert func(in float64) bool, name string, labels ...string) bool {
t.Helper()
return assert(getValue(t, metrics, gaugeKind, name, labels...))
}
func PromCounterGathered(t testing.TB, metrics []*dto.MetricFamily, name string, labels ...string) bool {
t.Helper()
return getMetric(t, metrics, name, labels...) != nil
}
func PromGaugeGathered(t testing.TB, metrics []*dto.MetricFamily, name string, labels ...string) bool {
t.Helper()
return getMetric(t, metrics, name, labels...) != nil
}
func getValue(t testing.TB, metrics []*dto.MetricFamily, kind kind, name string, labels ...string) float64 {
m := getMetric(t, metrics, name, labels...)
if m == nil {
return -1
}
switch kind {
case counterKind:
return m.GetCounter().GetValue()
case gaugeKind:
return m.GetGauge().GetValue()
default:
return -1
}
}
func getMetric(t testing.TB, metrics []*dto.MetricFamily, name string, labels ...string) *dto.Metric {
for _, family := range metrics {
if family.GetName() != name {
continue
}
ms := family.GetMetric()
metricsLoop:
for _, m := range ms {
require.Equal(t, len(labels), len(m.GetLabel()))
for i, lv := range labels {
if lv != m.GetLabel()[i].GetValue() {
continue metricsLoop
}
}
return m
}
}
return nil
}