-
-
Notifications
You must be signed in to change notification settings - Fork 614
/
Copy pathmetrics_test.go
76 lines (63 loc) · 2.71 KB
/
metrics_test.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
package redis
import (
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/redis/go-redis/v9"
"github.com/letsencrypt/boulder/metrics"
)
type mockPoolStatGetter struct{}
var _ poolStatGetter = mockPoolStatGetter{}
func (mockPoolStatGetter) PoolStats() *redis.PoolStats {
return &redis.PoolStats{
Hits: 13,
Misses: 7,
Timeouts: 4,
TotalConns: 1000,
IdleConns: 500,
StaleConns: 10,
}
}
func TestMetrics(t *testing.T) {
mets := newClientMetricsCollector(mockPoolStatGetter{},
prometheus.Labels{
"foo": "bar",
})
// Check that it has the correct type to satisfy MustRegister
metrics.NoopRegisterer.MustRegister(mets)
expectedMetrics := 6
outChan := make(chan prometheus.Metric, expectedMetrics)
mets.Collect(outChan)
results := make(map[string]bool)
for range expectedMetrics {
metric := <-outChan
results[metric.Desc().String()] = true
}
expected := strings.Split(
`Desc{fqName: "redis_connection_pool_lookups", help: "Number of lookups for a connection in the pool, labeled by hit/miss", constLabels: {foo="bar"}, variableLabels: [{result <nil>}]}
Desc{fqName: "redis_connection_pool_lookups", help: "Number of lookups for a connection in the pool, labeled by hit/miss", constLabels: {foo="bar"}, variableLabels: [{result <nil>}]}
Desc{fqName: "redis_connection_pool_lookups", help: "Number of lookups for a connection in the pool, labeled by hit/miss", constLabels: {foo="bar"}, variableLabels: [{result <nil>}]}
Desc{fqName: "redis_connection_pool_total_conns", help: "Number of total connections in the pool.", constLabels: {foo="bar"}, variableLabels: []}
Desc{fqName: "redis_connection_pool_idle_conns", help: "Number of idle connections in the pool.", constLabels: {foo="bar"}, variableLabels: []}
Desc{fqName: "redis_connection_pool_stale_conns", help: "Number of stale connections removed from the pool.", constLabels: {foo="bar"}, variableLabels: []}`,
"\n")
for _, e := range expected {
if !results[e] {
t.Errorf("expected metrics to contain %q, but they didn't", e)
}
}
if len(results) > len(expected) {
t.Errorf("expected metrics to contain %d entries, but they contained %d",
len(expected), len(results))
}
}
func TestMustRegisterClientMetricsCollector(t *testing.T) {
client := mockPoolStatGetter{}
stats := prometheus.NewRegistry()
// First registration should succeed.
MustRegisterClientMetricsCollector(client, stats, map[string]string{"foo": "bar"}, "baz")
// Duplicate registration should succeed.
MustRegisterClientMetricsCollector(client, stats, map[string]string{"foo": "bar"}, "baz")
// Registration with different label values should succeed.
MustRegisterClientMetricsCollector(client, stats, map[string]string{"f00": "b4r"}, "b4z")
}