-
Notifications
You must be signed in to change notification settings - Fork 3
/
metrics.go
127 lines (116 loc) · 3.04 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
package memdb
import (
"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"
)
var defBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1}
var redisDurations = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "sniper",
Subsystem: "memdb",
Name: "commands_duration_seconds",
Help: "commands latency distributions",
Buckets: defBuckets,
}, []string{"db_name", "cmd"})
func init() {
prometheus.MustRegister(redisDurations)
}
type StatsCollector struct {
db *redis.Client
// descriptions of exported metrics
hitDesc *prometheus.Desc
missDesc *prometheus.Desc
timeoutDesc *prometheus.Desc
totalDesc *prometheus.Desc
idleDesc *prometheus.Desc
staleDesc *prometheus.Desc
}
const (
namespace = "sniper"
subsystem = "memdb_connections"
)
// NewStatsCollector creates a new StatsCollector.
func NewStatsCollector(dbName string, db *redis.Client) *StatsCollector {
labels := prometheus.Labels{"db_name": dbName}
return &StatsCollector{
db: db,
hitDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "hit"),
"The number number of times free connection was NOT found in the pool.",
nil,
labels,
),
missDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "miss"),
"The number of times free connection was found in the pool.",
nil,
labels,
),
timeoutDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "timeout"),
"The number of times a wait timeout occurred.",
nil,
labels,
),
totalDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "total"),
"The number of total connections in the pool.",
nil,
labels,
),
idleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "idle"),
"The number of idle connections in the pool.",
nil,
labels,
),
staleDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "stale"),
"The number of stale connections in the pool.",
nil,
labels,
),
}
}
// Describe implements the prometheus.Collector interface.
func (c StatsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.hitDesc
ch <- c.missDesc
ch <- c.timeoutDesc
ch <- c.totalDesc
ch <- c.idleDesc
ch <- c.staleDesc
}
// Collect implements the prometheus.Collector interface.
func (c StatsCollector) Collect(ch chan<- prometheus.Metric) {
stats := c.db.PoolStats()
ch <- prometheus.MustNewConstMetric(
c.hitDesc,
prometheus.CounterValue,
float64(stats.Hits),
)
ch <- prometheus.MustNewConstMetric(
c.missDesc,
prometheus.CounterValue,
float64(stats.Misses),
)
ch <- prometheus.MustNewConstMetric(
c.timeoutDesc,
prometheus.CounterValue,
float64(stats.Timeouts),
)
ch <- prometheus.MustNewConstMetric(
c.totalDesc,
prometheus.GaugeValue,
float64(stats.TotalConns),
)
ch <- prometheus.MustNewConstMetric(
c.idleDesc,
prometheus.GaugeValue,
float64(stats.IdleConns),
)
ch <- prometheus.MustNewConstMetric(
c.staleDesc,
prometheus.GaugeValue,
float64(stats.StaleConns),
)
}