-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
metrics.go
170 lines (152 loc) · 5.36 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package sqlx
import (
"database/sql"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/zeromicro/go-zero/core/metric"
)
const namespace = "sql_client"
var (
metricReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "duration_ms",
Help: "mysql client requests duration(ms).",
Labels: []string{"command"},
Buckets: []float64{0.25, 0.5, 1, 1.5, 2, 3, 5, 10, 25, 50, 100, 250, 500, 1000, 2000, 5000, 10000, 15000},
})
metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "error_total",
Help: "mysql client requests error count.",
Labels: []string{"command", "error"},
})
metricSlowCount = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "slow_total",
Help: "mysql client requests slow count.",
Labels: []string{"command"},
})
connLabels = []string{"db_name", "hash"}
connCollector = newCollector()
_ prometheus.Collector = (*collector)(nil)
)
type (
statGetter struct {
dbName string
hash string
poolStats func() sql.DBStats
}
// collector collects statistics from a redis client.
// It implements the prometheus.Collector interface.
collector struct {
maxOpenConnections *prometheus.Desc
openConnections *prometheus.Desc
inUseConnections *prometheus.Desc
idleConnections *prometheus.Desc
waitCount *prometheus.Desc
waitDuration *prometheus.Desc
maxIdleClosed *prometheus.Desc
maxIdleTimeClosed *prometheus.Desc
maxLifetimeClosed *prometheus.Desc
clients []*statGetter
lock sync.Mutex
}
)
func newCollector() *collector {
c := &collector{
maxOpenConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_open_connections"),
"Maximum number of open connections to the database.",
connLabels, nil,
),
openConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "open_connections"),
"The number of established connections both in use and idle.",
connLabels, nil,
),
inUseConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "in_use_connections"),
"The number of connections currently in use.",
connLabels, nil,
),
idleConnections: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "idle_connections"),
"The number of idle connections.",
connLabels, nil,
),
waitCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "wait_count_total"),
"The total number of connections waited for.",
connLabels, nil,
),
waitDuration: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "wait_duration_seconds_total"),
"The total time blocked waiting for a new connection.",
connLabels, nil,
),
maxIdleClosed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_idle_closed_total"),
"The total number of connections closed due to SetMaxIdleConns.",
connLabels, nil,
),
maxIdleTimeClosed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_idle_time_closed_total"),
"The total number of connections closed due to SetConnMaxIdleTime.",
connLabels, nil,
),
maxLifetimeClosed: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "max_lifetime_closed_total"),
"The total number of connections closed due to SetConnMaxLifetime.",
connLabels, nil,
),
}
prometheus.MustRegister(c)
return c
}
// Describe implements the prometheus.Collector interface.
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.maxOpenConnections
ch <- c.openConnections
ch <- c.inUseConnections
ch <- c.idleConnections
ch <- c.waitCount
ch <- c.waitDuration
ch <- c.maxIdleClosed
ch <- c.maxLifetimeClosed
ch <- c.maxIdleTimeClosed
}
// Collect implements the prometheus.Collector interface.
func (c *collector) Collect(ch chan<- prometheus.Metric) {
c.lock.Lock()
defer c.lock.Unlock()
for _, client := range c.clients {
dbName, hash := client.dbName, client.hash
stats := client.poolStats()
ch <- prometheus.MustNewConstMetric(c.maxOpenConnections, prometheus.GaugeValue,
float64(stats.MaxOpenConnections), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.openConnections, prometheus.GaugeValue,
float64(stats.OpenConnections), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.inUseConnections, prometheus.GaugeValue,
float64(stats.InUse), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.idleConnections, prometheus.GaugeValue,
float64(stats.Idle), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.waitCount, prometheus.CounterValue,
float64(stats.WaitCount), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.waitDuration, prometheus.CounterValue,
stats.WaitDuration.Seconds(), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.maxIdleClosed, prometheus.CounterValue,
float64(stats.MaxIdleClosed), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.maxLifetimeClosed, prometheus.CounterValue,
float64(stats.MaxLifetimeClosed), dbName, hash)
ch <- prometheus.MustNewConstMetric(c.maxIdleTimeClosed, prometheus.CounterValue,
float64(stats.MaxIdleTimeClosed), dbName, hash)
}
}
func (c *collector) registerClient(client *statGetter) {
c.lock.Lock()
defer c.lock.Unlock()
c.clients = append(c.clients, client)
}