-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
collector.go
117 lines (109 loc) · 3.37 KB
/
collector.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
package redisprometheus
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/redis/go-redis/v9"
)
// StatGetter provides a method to get pool statistics.
type StatGetter interface {
PoolStats() *redis.PoolStats
}
// Collector collects statistics from a redis client.
// It implements the prometheus.Collector interface.
type Collector struct {
getter StatGetter
hitDesc *prometheus.Desc
missDesc *prometheus.Desc
timeoutDesc *prometheus.Desc
totalDesc *prometheus.Desc
idleDesc *prometheus.Desc
staleDesc *prometheus.Desc
}
var _ prometheus.Collector = (*Collector)(nil)
// NewCollector returns a new Collector based on the provided StatGetter.
// The given namespace and subsystem are used to build the fully qualified metric name,
// i.e. "{namespace}_{subsystem}_{metric}".
// The provided metrics are:
// - pool_hit_total
// - pool_miss_total
// - pool_timeout_total
// - pool_conn_total_current
// - pool_conn_idle_current
// - pool_conn_stale_total
func NewCollector(namespace, subsystem string, getter StatGetter, constantLabels prometheus.Labels) *Collector {
return &Collector{
getter: getter,
hitDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_hit_total"),
"Number of times a connection was found in the pool",
nil, constantLabels,
),
missDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_miss_total"),
"Number of times a connection was not found in the pool",
nil, constantLabels,
),
timeoutDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_timeout_total"),
"Number of times a timeout occurred when looking for a connection in the pool",
nil, constantLabels,
),
totalDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_conn_total_current"),
"Current number of connections in the pool",
nil, constantLabels,
),
idleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_conn_idle_current"),
"Current number of idle connections in the pool",
nil, constantLabels,
),
staleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "pool_conn_stale_total"),
"Number of times a connection was removed from the pool because it was stale",
nil, constantLabels,
),
}
}
// Describe implements the prometheus.Collector interface.
func (s *Collector) Describe(descs chan<- *prometheus.Desc) {
descs <- s.hitDesc
descs <- s.missDesc
descs <- s.timeoutDesc
descs <- s.totalDesc
descs <- s.idleDesc
descs <- s.staleDesc
}
// Collect implements the prometheus.Collector interface.
func (s *Collector) Collect(metrics chan<- prometheus.Metric) {
stats := s.getter.PoolStats()
metrics <- prometheus.MustNewConstMetric(
s.hitDesc,
prometheus.CounterValue,
float64(stats.Hits),
)
metrics <- prometheus.MustNewConstMetric(
s.missDesc,
prometheus.CounterValue,
float64(stats.Misses),
)
metrics <- prometheus.MustNewConstMetric(
s.timeoutDesc,
prometheus.CounterValue,
float64(stats.Timeouts),
)
metrics <- prometheus.MustNewConstMetric(
s.totalDesc,
prometheus.GaugeValue,
float64(stats.TotalConns),
)
metrics <- prometheus.MustNewConstMetric(
s.idleDesc,
prometheus.GaugeValue,
float64(stats.IdleConns),
)
metrics <- prometheus.MustNewConstMetric(
s.staleDesc,
prometheus.CounterValue,
float64(stats.StaleConns),
)
}