-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathinfo.go
541 lines (466 loc) · 16 KB
/
info.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package exporter
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
// precompiled regexps
// keydb multimaster
/*
master_host:kdb0.server.local
master_port:6377
master_1_host:kdb1.server.local
master_1_port:6377
*/
var reMasterHost = regexp.MustCompile(`^master(_[0-9]+)?_host`)
var reMasterPort = regexp.MustCompile(`^master(_[0-9]+)?_port`)
var reMasterLinkStatus = regexp.MustCompile(`^master(_[0-9]+)?_link_status`)
// info fieldKey:fieldValue -> metric redis_fieldKey{master_host, master_port} fieldValue
var reMasterDirect = regexp.MustCompile(`^(master(_[0-9]+)?_(last_io_seconds_ago|sync_in_progress)|slave_repl_offset)`)
// numbered slaves
/*
slave0:ip=10.254.11.1,port=6379,state=online,offset=1751844676,lag=0
slave1:ip=10.254.11.2,port=6379,state=online,offset=1751844222,lag=0
*/
var reSlave = regexp.MustCompile(`^slave\d+`)
func extractVal(s string) (val float64, err error) {
split := strings.Split(s, "=")
if len(split) != 2 {
return 0, fmt.Errorf("nope")
}
val, err = strconv.ParseFloat(split[1], 64)
if err != nil {
return 0, fmt.Errorf("nope")
}
return
}
func extractPercentileVal(s string) (percentile float64, val float64, err error) {
split := strings.Split(s, "=")
if len(split) != 2 {
return
}
percentile, err = strconv.ParseFloat(split[0][1:], 64)
if err != nil {
return
}
val, err = strconv.ParseFloat(split[1], 64)
return
}
func (e *Exporter) extractInfoMetrics(ch chan<- prometheus.Metric, info string, dbCount int) {
keyValues := map[string]string{}
handledDBs := map[string]bool{}
cmdCount := map[string]uint64{}
cmdSum := map[string]float64{}
cmdLatencyMap := map[string]map[float64]float64{}
fieldClass := ""
lines := strings.Split(info, "\n")
masterHost := ""
masterPort := ""
for _, line := range lines {
line = strings.TrimSpace(line)
log.Debugf("info: %s", line)
if len(line) > 0 && strings.HasPrefix(line, "# ") {
fieldClass = line[2:]
log.Debugf("set fieldClass: %s", fieldClass)
continue
}
if (len(line) < 2) || (!strings.Contains(line, ":")) {
continue
}
split := strings.SplitN(line, ":", 2)
fieldKey := split[0]
fieldValue := split[1]
keyValues[fieldKey] = fieldValue
if reMasterHost.MatchString(fieldKey) {
masterHost = fieldValue
}
if reMasterPort.MatchString(fieldKey) {
masterPort = fieldValue
}
switch fieldClass {
case "Replication":
if ok := e.handleMetricsReplication(ch, masterHost, masterPort, fieldKey, fieldValue); ok {
continue
}
case "Server":
e.handleMetricsServer(ch, fieldKey, fieldValue)
case "Commandstats":
cmd, calls, usecsTotal := e.handleMetricsCommandStats(ch, fieldKey, fieldValue)
cmdCount[cmd] = uint64(calls)
cmdSum[cmd] = usecsTotal
continue
case "Latencystats":
e.handleMetricsLatencyStats(fieldKey, fieldValue, cmdLatencyMap)
continue
case "Errorstats":
e.handleMetricsErrorStats(ch, fieldKey, fieldValue)
continue
case "Keyspace":
if keysTotal, keysEx, avgTTL, keysCached, ok := parseDBKeyspaceString(fieldKey, fieldValue); ok {
dbName := fieldKey
e.registerConstMetricGauge(ch, "db_keys", keysTotal, dbName)
e.registerConstMetricGauge(ch, "db_keys_expiring", keysEx, dbName)
if keysCached > -1 {
e.registerConstMetricGauge(ch, "db_keys_cached", keysCached, dbName)
}
if avgTTL > -1 {
e.registerConstMetricGauge(ch, "db_avg_ttl_seconds", avgTTL, dbName)
}
handledDBs[dbName] = true
continue
}
case "Sentinel":
e.handleMetricsSentinel(ch, fieldKey, fieldValue)
}
if !e.includeMetric(fieldKey) {
continue
}
e.parseAndRegisterConstMetric(ch, fieldKey, fieldValue)
}
// To be able to generate the latency summaries we need the count and sum that we get
// from #Commandstats processing and the percentile info that we get from the #Latencystats processing
e.generateCommandLatencySummaries(ch, cmdLatencyMap, cmdCount, cmdSum)
for dbIndex := 0; dbIndex < dbCount; dbIndex++ {
dbName := "db" + strconv.Itoa(dbIndex)
if _, exists := handledDBs[dbName]; !exists {
e.registerConstMetricGauge(ch, "db_keys", 0, dbName)
e.registerConstMetricGauge(ch, "db_keys_expiring", 0, dbName)
}
}
e.registerConstMetricGauge(ch, "instance_info", 1,
keyValues["role"],
keyValues["redis_version"],
keyValues["redis_build_id"],
keyValues["redis_mode"],
keyValues["os"],
keyValues["maxmemory_policy"],
keyValues["tcp_port"],
keyValues["run_id"],
keyValues["process_id"],
keyValues["master_replid"],
)
if keyValues["role"] == "slave" {
e.registerConstMetricGauge(ch, "slave_info", 1,
keyValues["master_host"],
keyValues["master_port"],
keyValues["slave_read_only"])
}
}
func (e *Exporter) generateCommandLatencySummaries(ch chan<- prometheus.Metric, cmdLatencyMap map[string]map[float64]float64, cmdCount map[string]uint64, cmdSum map[string]float64) {
for cmd, latencyMap := range cmdLatencyMap {
count, okCount := cmdCount[cmd]
sum, okSum := cmdSum[cmd]
if okCount && okSum {
e.registerConstSummary(ch, "latency_percentiles_usec", []string{"cmd"}, count, sum, latencyMap, cmd)
}
}
}
func (e *Exporter) extractClusterInfoMetrics(ch chan<- prometheus.Metric, info string) {
lines := strings.Split(info, "\r\n")
for _, line := range lines {
log.Debugf("info: %s", line)
split := strings.Split(line, ":")
if len(split) != 2 {
continue
}
fieldKey := split[0]
fieldValue := split[1]
if !e.includeMetric(fieldKey) {
continue
}
e.parseAndRegisterConstMetric(ch, fieldKey, fieldValue)
}
}
/*
valid example: db0:keys=1,expires=0,avg_ttl=0,cached_keys=0
*/
func parseDBKeyspaceString(inputKey string, inputVal string) (keysTotal float64, keysExpiringTotal float64, avgTTL float64, keysCachedTotal float64, ok bool) {
log.Debugf("parseDBKeyspaceString inputKey: [%s] inputVal: [%s]", inputKey, inputVal)
if !strings.HasPrefix(inputKey, "db") {
log.Debugf("parseDBKeyspaceString inputKey not starting with 'db': [%s]", inputKey)
return
}
split := strings.Split(inputVal, ",")
if len(split) < 2 || len(split) > 4 {
log.Debugf("parseDBKeyspaceString strings.Split(inputVal) invalid: %#v", split)
return
}
var err error
if keysTotal, err = extractVal(split[0]); err != nil {
log.Debugf("parseDBKeyspaceString extractVal(split[0]) invalid, err: %s", err)
return
}
if keysExpiringTotal, err = extractVal(split[1]); err != nil {
log.Debugf("parseDBKeyspaceString extractVal(split[1]) invalid, err: %s", err)
return
}
avgTTL = -1
if len(split) > 2 {
if avgTTL, err = extractVal(split[2]); err != nil {
log.Debugf("parseDBKeyspaceString extractVal(split[2]) invalid, err: %s", err)
return
}
avgTTL /= 1000
}
keysCachedTotal = -1
if len(split) > 3 {
if keysCachedTotal, err = extractVal(split[3]); err != nil {
log.Debugf("parseDBKeyspaceString extractVal(split[3]) invalid, err: %s", err)
return
}
}
ok = true
return
}
/*
slave0:ip=10.254.11.1,port=6379,state=online,offset=1751844676,lag=0
slave1:ip=10.254.11.2,port=6379,state=online,offset=1751844222,lag=0
*/
func parseConnectedSlaveString(slaveName string, keyValues string) (offset float64, ip string, port string, state string, lag float64, ok bool) {
ok = false
if !reSlave.MatchString(slaveName) {
return
}
connectedkeyValues := make(map[string]string)
for _, kvPart := range strings.Split(keyValues, ",") {
x := strings.Split(kvPart, "=")
if len(x) != 2 {
log.Debugf("Invalid format for connected slave string, got: %s", kvPart)
return
}
connectedkeyValues[x[0]] = x[1]
}
offset, err := strconv.ParseFloat(connectedkeyValues["offset"], 64)
if err != nil {
log.Debugf("Can not parse connected slave offset, got: %s", connectedkeyValues["offset"])
return
}
if lagStr, exists := connectedkeyValues["lag"]; !exists {
// Prior to Redis 3.0, "lag" property does not exist
lag = -1
} else {
lag, err = strconv.ParseFloat(lagStr, 64)
if err != nil {
log.Debugf("Can not parse connected slave lag, got: %s", lagStr)
return
}
}
ok = true
ip = connectedkeyValues["ip"]
port = connectedkeyValues["port"]
state = connectedkeyValues["state"]
return
}
func (e *Exporter) handleMetricsReplication(ch chan<- prometheus.Metric, masterHost string, masterPort string, fieldKey string, fieldValue string) bool {
// only slaves have this field
if reMasterLinkStatus.MatchString(fieldKey) {
if fieldValue == "up" {
e.registerConstMetricGauge(ch, "master_link_up", 1, masterHost, masterPort)
} else {
e.registerConstMetricGauge(ch, "master_link_up", 0, masterHost, masterPort)
}
return true
}
if reMasterDirect.MatchString(fieldKey) {
if strings.HasSuffix(fieldKey, "last_io_seconds_ago") {
fieldKey = "master_last_io_seconds_ago"
} else if strings.HasSuffix(fieldKey, "sync_in_progress") {
fieldKey = "master_sync_in_progress"
}
val, _ := strconv.Atoi(fieldValue)
e.registerConstMetricGauge(ch, fieldKey, float64(val), masterHost, masterPort)
return true
}
// not a slave, try extracting master metrics
if slaveOffset, slaveIP, slavePort, slaveState, slaveLag, ok := parseConnectedSlaveString(fieldKey, fieldValue); ok {
e.registerConstMetricGauge(ch,
"connected_slave_offset_bytes",
slaveOffset,
slaveIP, slavePort, slaveState,
)
if slaveLag > -1 {
e.registerConstMetricGauge(ch,
"connected_slave_lag_seconds",
slaveLag,
slaveIP, slavePort, slaveState,
)
}
return true
}
return false
}
func (e *Exporter) handleMetricsServer(ch chan<- prometheus.Metric, fieldKey string, fieldValue string) {
if fieldKey == "uptime_in_seconds" {
if uptime, err := strconv.ParseFloat(fieldValue, 64); err == nil {
e.registerConstMetricGauge(ch, "start_time_seconds", float64(time.Now().Unix())-uptime)
}
}
if fieldKey == "configured_hz" {
if hz, err := strconv.ParseInt(fieldValue, 10, 64); err == nil {
e.registerConstMetricGauge(ch, "configured_hz", float64(hz))
}
}
if fieldKey == "hz" {
if hz, err := strconv.ParseInt(fieldValue, 10, 64); err == nil {
e.registerConstMetricGauge(ch, "hz", float64(hz))
}
}
}
func parseMetricsCommandStats(fieldKey string, fieldValue string) (cmd string, calls float64, rejectedCalls float64, failedCalls float64, usecTotal float64, extendedStats bool, errorOut error) {
/*
There are 2 formats. (One before Redis 6.2 and one after it)
Format before v6.2:
cmdstat_get:calls=21,usec=175,usec_per_call=8.33
cmdstat_set:calls=61,usec=3139,usec_per_call=51.46
cmdstat_setex:calls=75,usec=1260,usec_per_call=16.80
cmdstat_georadius_ro:calls=75,usec=1260,usec_per_call=16.80
Format from v6.2 forward:
cmdstat_get:calls=21,usec=175,usec_per_call=8.33,rejected_calls=0,failed_calls=0
cmdstat_set:calls=61,usec=3139,usec_per_call=51.46,rejected_calls=0,failed_calls=0
cmdstat_setex:calls=75,usec=1260,usec_per_call=16.80,rejected_calls=0,failed_calls=0
cmdstat_georadius_ro:calls=75,usec=1260,usec_per_call=16.80,rejected_calls=0,failed_calls=0
broken up like this:
fieldKey = cmdstat_get
fieldValue= calls=21,usec=175,usec_per_call=8.33
*/
const cmdPrefix = "cmdstat_"
extendedStats = false
if !strings.HasPrefix(fieldKey, cmdPrefix) {
errorOut = errors.New("invalid fieldKey")
return
}
cmd = strings.TrimPrefix(fieldKey, cmdPrefix)
splitValue := strings.Split(fieldValue, ",")
splitLen := len(splitValue)
if splitLen < 3 {
errorOut = errors.New("invalid fieldValue")
return
}
// internal error variable
var err error
calls, err = extractVal(splitValue[0])
if err != nil {
errorOut = errors.New("invalid splitValue[0]")
return
}
usecTotal, err = extractVal(splitValue[1])
if err != nil {
errorOut = errors.New("invalid splitValue[1]")
return
}
// pre 6.2 did not include rejected/failed calls stats so if we have less than 5 tokens we're done here
if splitLen < 5 {
return
}
rejectedCalls, err = extractVal(splitValue[3])
if err != nil {
errorOut = errors.New("Invalid rejected_calls while parsing splitValue[3]")
return
}
failedCalls, err = extractVal(splitValue[4])
if err != nil {
errorOut = errors.New("Invalid failed_calls while parsing splitValue[4]")
return
}
extendedStats = true
return
}
func parseMetricsLatencyStats(fieldKey string, fieldValue string) (cmd string, percentileMap map[float64]float64, errorOut error) {
/*
# Latencystats
latency_percentiles_usec_rpop:p50=0.001,p99=1.003,p99.9=4.015
latency_percentiles_usec_zadd:p50=0.001,p99=1.003,p99.9=4.015
latency_percentiles_usec_hset:p50=0.001,p99=1.003,p99.9=3.007
latency_percentiles_usec_set:p50=0.001,p99=1.003,p99.9=4.015
latency_percentiles_usec_lpop:p50=0.001,p99=1.003,p99.9=4.015
latency_percentiles_usec_lpush:p50=0.001,p99=1.003,p99.9=4.015
latency_percentiles_usec_lrange:p50=17.023,p99=21.119,p99.9=27.007
latency_percentiles_usec_get:p50=0.001,p99=1.003,p99.9=3.007
latency_percentiles_usec_mset:p50=1.003,p99=1.003,p99.9=1.003
latency_percentiles_usec_spop:p50=0.001,p99=1.003,p99.9=1.003
latency_percentiles_usec_incr:p50=0.001,p99=1.003,p99.9=3.007
latency_percentiles_usec_rpush:p50=0.001,p99=1.003,p99.9=4.015
latency_percentiles_usec_zpopmin:p50=0.001,p99=1.003,p99.9=3.007
latency_percentiles_usec_config|resetstat:p50=280.575,p99=280.575,p99.9=280.575
latency_percentiles_usec_config|get:p50=8.031,p99=27.007,p99.9=27.007
latency_percentiles_usec_ping:p50=0.001,p99=1.003,p99.9=1.003
latency_percentiles_usec_sadd:p50=0.001,p99=1.003,p99.9=3.007
broken up like this:
fieldKey = latency_percentiles_usec_ping
fieldValue= p50=0.001,p99=1.003,p99.9=3.007
*/
const cmdPrefix = "latency_percentiles_usec_"
percentileMap = map[float64]float64{}
if !strings.HasPrefix(fieldKey, cmdPrefix) {
errorOut = errors.New("Invalid fieldKey")
return
}
cmd = strings.TrimPrefix(fieldKey, cmdPrefix)
splitValue := strings.Split(fieldValue, ",")
splitLen := len(splitValue)
if splitLen < 1 {
errorOut = errors.New("Invalid fieldValue")
return
}
for pos, kv := range splitValue {
percentile, value, err := extractPercentileVal(kv)
if err != nil {
errorOut = fmt.Errorf("Invalid splitValue[%d]", pos)
return
}
percentileMap[percentile] = value
}
return
}
func parseMetricsErrorStats(fieldKey string, fieldValue string) (errorType string, count float64, errorOut error) {
/*
Format:
errorstat_ERR:count=4
errorstat_NOAUTH:count=3
broken up like this:
fieldKey = errorstat_ERR
fieldValue= count=3
*/
const prefix = "errorstat_"
if !strings.HasPrefix(fieldKey, prefix) {
errorOut = errors.New("Invalid fieldKey. errorstat_ prefix not present")
return
}
errorType = strings.TrimPrefix(fieldKey, prefix)
count, err := extractVal(fieldValue)
if err != nil {
errorOut = errors.New("Invalid error type on splitValue[0]")
return
}
return
}
func (e *Exporter) handleMetricsCommandStats(ch chan<- prometheus.Metric, fieldKey string, fieldValue string) (cmd string, calls float64, usecTotal float64) {
cmd, calls, rejectedCalls, failedCalls, usecTotal, extendedStats, err := parseMetricsCommandStats(fieldKey, fieldValue)
if err != nil {
log.Debugf("parseMetricsCommandStats( %s , %s ) err: %s", fieldKey, fieldValue, err)
return
}
e.registerConstMetric(ch, "commands_total", calls, prometheus.CounterValue, cmd)
e.registerConstMetric(ch, "commands_duration_seconds_total", usecTotal/1e6, prometheus.CounterValue, cmd)
if extendedStats {
e.registerConstMetric(ch, "commands_rejected_calls_total", rejectedCalls, prometheus.CounterValue, cmd)
e.registerConstMetric(ch, "commands_failed_calls_total", failedCalls, prometheus.CounterValue, cmd)
}
return
}
func (e *Exporter) handleMetricsLatencyStats(fieldKey string, fieldValue string, cmdLatencyMap map[string]map[float64]float64) {
cmd, latencyMap, err := parseMetricsLatencyStats(fieldKey, fieldValue)
if err == nil {
cmdLatencyMap[cmd] = latencyMap
}
}
func (e *Exporter) handleMetricsErrorStats(ch chan<- prometheus.Metric, fieldKey string, fieldValue string) {
if errorPrefix, count, err := parseMetricsErrorStats(fieldKey, fieldValue); err == nil {
e.registerConstMetric(ch, "errors_total", count, prometheus.CounterValue, errorPrefix)
}
}