-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
298 lines (254 loc) · 8.53 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package longredis
import (
"context"
"errors"
"time"
"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"
)
// 兼容 promethues metrics
var (
namespace = "default"
subsystem = "longredis"
)
// metrics 包含内容
// 1. 请求成功数量
// 2. 请求失败数量
// 3. 超时数量
// 4. miss率
// 3. 命令的数量分布
// 4. 重连次数
// 5. 当前连接数
// 6. 请求时间分布
var (
// 成功数量
SuccessCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "success",
Subsystem: subsystem,
Help: "success counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 失败数量
FailCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "fail",
Subsystem: subsystem,
Help: "fail counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 上层取消数量
ContextCancelCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "ctxcancel",
Subsystem: subsystem,
Help: "context cancel counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 超时数量
ContextTimeoutCounter = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "ctxtimeout",
Subsystem: subsystem,
Help: "context timeout counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// miss 数
MissCounter = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "miss",
Subsystem: subsystem,
Help: "miss counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 重连次数
ReconnectCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "reconnect",
Subsystem: subsystem,
Help: "reconnect counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 当前连接数
ConnsGuage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "conns",
Subsystem: subsystem,
Help: "conns counts of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 当前空闲连接数
IdleConnsGuage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "idleconns",
Subsystem: subsystem,
Help: "idle conns counts of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 请求时间分布
DurationHistgram = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: namespace,
Name: "duration",
Subsystem: subsystem,
Help: "command duration of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
Buckets: []float64{1, 3, 5, 10, 20, 50, 100, 300, 500, 1000, 2000}, // ms
})
// 请求类型分布
CommandCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "duration",
Subsystem: subsystem,
Help: "command type couter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"command"})
// 慢请求数量
SlowCounter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "duration",
Subsystem: subsystem,
Help: "slow counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
})
)
type metricsHook struct{}
func (h *metricsHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
return ctx, nil
}
func (h *metricsHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
t := ctx.Value(startTimeKey).(time.Time)
id := ctx.Value(clientidKey).(string)
// duration
DurationHistgram.Observe(float64(time.Now().Sub(t).Milliseconds()))
// command type
CommandCounter.WithLabelValues(cmd.Name()).Inc()
// success or fail
if cmd.Err() == nil {
SuccessCounter.WithLabelValues(id).Inc()
} else if errors.Is(cmd.Err(), redis.Nil) {
SuccessCounter.WithLabelValues(id).Inc()
} else {
FailCounter.WithLabelValues(id).Inc()
if errors.Is(cmd.Err(), context.Canceled) {
ContextCancelCounter.WithLabelValues(id).Inc()
}
if errors.Is(cmd.Err(), context.DeadlineExceeded) {
ContextTimeoutCounter.WithLabelValues(id).Inc()
}
}
return nil
}
func (h *metricsHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
return ctx, nil
}
func (h *metricsHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
t := ctx.Value(startTimeKey).(time.Time)
id := ctx.Value(clientidKey).(string)
// duration
DurationHistgram.Observe(float64(time.Now().Sub(t).Milliseconds()))
// command type
CommandCounter.WithLabelValues("pipeline").Inc()
for _, cmd := range cmds {
// command type
CommandCounter.WithLabelValues(cmd.Name()).Inc()
// success or fail
if cmd.Err() == nil {
SuccessCounter.WithLabelValues(id).Inc()
} else if errors.Is(cmd.Err(), redis.Nil) {
SuccessCounter.WithLabelValues(id).Inc()
MissCounter.WithLabelValues(id).Inc()
} else {
FailCounter.WithLabelValues(id).Inc()
if errors.Is(cmd.Err(), context.Canceled) {
ContextCancelCounter.WithLabelValues(id).Inc()
}
if errors.Is(cmd.Err(), context.DeadlineExceeded) {
ContextTimeoutCounter.WithLabelValues(id).Inc()
}
}
}
return nil
}
func ResetMetrics(namespace, subsystem string) {
// 成功数量
SuccessCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "success",
Subsystem: subsystem,
Help: "success counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 失败数量
FailCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "fail",
Subsystem: subsystem,
Help: "fail counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 上层取消数量
ContextCancelCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "ctxcancel",
Subsystem: subsystem,
Help: "context cancel counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 超时数量
ContextTimeoutCounter = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "ctxtimeout",
Subsystem: subsystem,
Help: "context timeout counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// miss 数
MissCounter = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "miss",
Subsystem: subsystem,
Help: "miss counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 重连次数
ReconnectCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "reconnect",
Subsystem: subsystem,
Help: "reconnect counter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 当前连接数
ConnsGuage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "conns",
Subsystem: subsystem,
Help: "conns counts of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 当前空闲连接数
IdleConnsGuage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "idleconns",
Subsystem: subsystem,
Help: "idle conns counts of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"_clientid"})
// 请求时间分布
DurationHistgram = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: namespace,
Name: "duration",
Subsystem: subsystem,
Help: "command duration of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
Buckets: []float64{1, 3, 5, 10, 20, 50, 100, 300, 500, 1000, 2000}, // ms
})
// 请求类型分布
CommandCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "duration",
Subsystem: subsystem,
Help: "command type couter of longredis",
ConstLabels: prometheus.Labels{"mod": "longredis"},
}, []string{"command"})
}