-
Notifications
You must be signed in to change notification settings - Fork 2k
/
testing.go
46 lines (39 loc) · 1.17 KB
/
testing.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package extprom
import (
"fmt"
"strings"
"testing"
"github.com/efficientgo/core/testutil"
"github.com/prometheus/client_golang/prometheus"
)
// CurrentGaugeValuesFor returns gauge values for given metric names. Useful for testing based on registry,
// when you don't have access to metric variable.
func CurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames ...string) map[string]float64 {
f, err := reg.Gather()
testutil.Ok(t, err)
res := make(map[string]float64, len(metricNames))
for _, g := range f {
for _, m := range metricNames {
if g.GetName() != m {
continue
}
for _, metric := range g.GetMetric() {
var lbls []string
for _, l := range metric.GetLabel() {
lbls = append(lbls, *l.Name+"="+*l.Value)
}
key := fmt.Sprintf("%s{%s}", m, strings.Join(lbls, ","))
if _, ok := res[key]; ok {
t.Fatal("duplicate metrics, should never happen with Prometheus Registry; key =", key)
}
if metric.GetGauge() == nil {
t.Fatal("metric is not a gauge; key =", key)
}
res[key] = *(metric.GetGauge().Value)
}
}
}
return res
}