-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathinfo_test.go
447 lines (397 loc) · 12.8 KB
/
info_test.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
package exporter
import (
"fmt"
"net/http/httptest"
"os"
"reflect"
"regexp"
"strings"
"testing"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
func TestKeyspaceStringParser(t *testing.T) {
tsts := []struct {
db string
stats string
keysTotal, keysEx, keysCached, avgTTL float64
ok bool
}{
{db: "xxx", stats: "", ok: false},
{db: "xxx", stats: "keys=1,expires=0,avg_ttl=0", ok: false},
{db: "db0", stats: "xxx", ok: false},
{db: "db1", stats: "keys=abcd,expires=0,avg_ttl=0", ok: false},
{db: "db2", stats: "keys=1234=1234,expires=0,avg_ttl=0", ok: false},
{db: "db3", stats: "keys=abcde,expires=0", ok: false},
{db: "db3", stats: "keys=213,expires=xxx", ok: false},
{db: "db3", stats: "keys=123,expires=0,avg_ttl=zzz", ok: false},
{db: "db3", stats: "keys=1,expires=0,avg_ttl=zzz,cached_keys=0", ok: false},
{db: "db3", stats: "keys=1,expires=0,avg_ttl=0,cached_keys=zzz", ok: false},
{db: "db3", stats: "keys=1,expires=0,avg_ttl=0,cached_keys=0,extra=0", ok: false},
{db: "db0", stats: "keys=1,expires=0,avg_ttl=0", keysTotal: 1, keysEx: 0, avgTTL: 0, keysCached: -1, ok: true},
{db: "db0", stats: "keys=1,expires=0,avg_ttl=0,cached_keys=0", keysTotal: 1, keysEx: 0, avgTTL: 0, keysCached: 0, ok: true},
}
for _, tst := range tsts {
if kt, kx, ttl, kc, ok := parseDBKeyspaceString(tst.db, tst.stats); true {
if ok != tst.ok {
t.Errorf("failed for: db:%s stats:%s", tst.db, tst.stats)
continue
}
if ok && (kt != tst.keysTotal || kx != tst.keysEx || kc != tst.keysCached || ttl != tst.avgTTL) {
t.Errorf("values not matching, db:%s stats:%s %f %f %f %f", tst.db, tst.stats, kt, kx, kc, ttl)
}
}
}
}
type slaveData struct {
k, v string
ip, state, port string
offset float64
lag float64
ok bool
}
func TestParseConnectedSlaveString(t *testing.T) {
tsts := []slaveData{
{k: "slave0", v: "ip=10.254.11.1,port=6379,state=online,offset=1751844676,lag=0", offset: 1751844676, ip: "10.254.11.1", port: "6379", state: "online", ok: true, lag: 0},
{k: "slave0", v: "ip=2a00:1450:400e:808::200e,port=6379,state=online,offset=1751844676,lag=0", offset: 1751844676, ip: "2a00:1450:400e:808::200e", port: "6379", state: "online", ok: true, lag: 0},
{k: "slave1", v: "offset=1,lag=0", offset: 1, ok: true},
{k: "slave1", v: "offset=1", offset: 1, ok: true, lag: -1},
{k: "slave2", v: "ip=1.2.3.4,state=online,offset=123,lag=42", offset: 123, ip: "1.2.3.4", state: "online", ok: true, lag: 42},
{k: "slave", v: "offset=1751844676,lag=0", ok: false},
{k: "slaveA", v: "offset=1751844676,lag=0", ok: false},
{k: "slave0", v: "offset=abc,lag=0", ok: false},
{k: "slave0", v: "offset=0,lag=abc", ok: false},
}
for _, tst := range tsts {
t.Run(fmt.Sprintf("%s---%s", tst.k, tst.v), func(t *testing.T) {
offset, ip, port, state, lag, ok := parseConnectedSlaveString(tst.k, tst.v)
if ok != tst.ok {
t.Errorf("failed for: db:%s stats:%s", tst.k, tst.v)
return
}
if offset != tst.offset || ip != tst.ip || port != tst.port || state != tst.state || lag != tst.lag {
t.Errorf("values not matching, string:%s %f %s %s %s %f", tst.v, offset, ip, port, state, lag)
}
})
}
}
func TestCommandStats(t *testing.T) {
defaultAddr := os.Getenv("TEST_REDIS_URI")
e := getTestExporterWithAddr(defaultAddr)
setupTestKeys(t, defaultAddr)
want := map[string]bool{"test_commands_duration_seconds_total": false, "test_commands_total": false}
commandStatsCheck(t, e, want)
deleteTestKeys(t, defaultAddr)
redisSixTwoAddr := os.Getenv("TEST_REDIS6_URI")
if redisSixTwoAddr != "" {
// Since Redis v6.2 we should expect extra failed calls and rejected calls
e = getTestExporterWithAddr(redisSixTwoAddr)
setupTestKeys(t, redisSixTwoAddr)
want = map[string]bool{"test_commands_duration_seconds_total": false, "test_commands_total": false, "commands_failed_calls_total": false, "commands_rejected_calls_total": false, "errors_total": false}
commandStatsCheck(t, e, want)
deleteTestKeys(t, redisSixTwoAddr)
}
}
func commandStatsCheck(t *testing.T, e *Exporter, want map[string]bool) {
chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
for m := range chM {
for k := range want {
if strings.Contains(m.Desc().String(), k) {
want[k] = true
}
}
}
for k, found := range want {
if !found {
t.Errorf("didn't find %s", k)
}
}
}
func TestClusterMaster(t *testing.T) {
if os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") == "" {
t.Skipf("TEST_REDIS_CLUSTER_MASTER_URI not set - skipping")
}
addr := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
e, _ := NewRedisExporter(addr, Options{Namespace: "test", Registry: prometheus.NewRegistry()})
ts := httptest.NewServer(e)
defer ts.Close()
chM := make(chan prometheus.Metric, 10000)
go func() {
e.Collect(chM)
close(chM)
}()
body := downloadURL(t, ts.URL+"/metrics")
log.Debugf("master - body: %s", body)
for _, want := range []string{
"test_instance_info{",
"test_master_repl_offset",
} {
if !strings.Contains(body, want) {
t.Errorf("Did not find key [%s] \nbody: %s", want, body)
}
}
}
func TestClusterSlave(t *testing.T) {
if os.Getenv("TEST_REDIS_CLUSTER_SLAVE_URI") == "" {
t.Skipf("TEST_REDIS_CLUSTER_SLAVE_URI not set - skipping")
}
addr := os.Getenv("TEST_REDIS_CLUSTER_SLAVE_URI")
e, _ := NewRedisExporter(addr, Options{Namespace: "test", Registry: prometheus.NewRegistry()})
ts := httptest.NewServer(e)
defer ts.Close()
chM := make(chan prometheus.Metric, 10000)
go func() {
e.Collect(chM)
close(chM)
}()
body := downloadURL(t, ts.URL+"/metrics")
log.Debugf("slave - body: %s", body)
for _, want := range []string{
"test_instance_info",
"test_master_last_io_seconds",
"test_slave_info",
} {
if !strings.Contains(body, want) {
t.Errorf("Did not find key [%s] \nbody: %s", want, body)
}
}
hostReg, _ := regexp.Compile(`master_host="([0,1]?\d{1,2}|2([0-4][0-9]|5[0-5]))(\.([0,1]?\d{1,2}|2([0-4][0-9]|5[0-5]))){3}"`)
masterHost := hostReg.FindString(body)
portReg, _ := regexp.Compile(`master_port="(\d+)"`)
masterPort := portReg.FindString(body)
for wantedKey, wantedVal := range map[string]int{
masterHost: 5,
masterPort: 5,
} {
if res := strings.Count(body, wantedKey); res != wantedVal {
t.Errorf("Result: %s -> %d, Wanted: %d \nbody: %s", wantedKey, res, wantedVal, body)
}
}
}
func TestParseCommandStats(t *testing.T) {
for _, tst := range []struct {
fieldKey string
fieldValue string
wantSuccess bool
wantExtraStats bool
wantCmd string
wantCalls float64
wantRejectedCalls float64
wantFailedCalls float64
wantUsecTotal float64
}{
{
fieldKey: "cmdstat_get",
fieldValue: "calls=21,usec=175,usec_per_call=8.33",
wantSuccess: true,
wantCmd: "get",
wantCalls: 21,
wantUsecTotal: 175,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "calls=75,usec=1260,usec_per_call=16.80",
wantSuccess: true,
wantCmd: "georadius_ro",
wantCalls: 75,
wantUsecTotal: 1260,
},
{
fieldKey: "borked_stats",
fieldValue: "calls=75,usec=1260,usec_per_call=16.80",
wantSuccess: false,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "borked_values",
wantSuccess: false,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "usec_per_call=16.80",
wantSuccess: false,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "calls=ABC,usec=1260,usec_per_call=16.80",
wantSuccess: false,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "calls=75,usec=DEF,usec_per_call=16.80",
wantSuccess: false,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "calls=75,usec=1024,usec_per_call=16.80,rejected_calls=5,failed_calls=10",
wantCmd: "georadius_ro",
wantCalls: 75,
wantUsecTotal: 1024,
wantSuccess: true,
wantExtraStats: true,
wantFailedCalls: 10,
wantRejectedCalls: 5,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "calls=75,usec=1024,usec_per_call=16.80,rejected_calls=ABC,failed_calls=10",
wantSuccess: false,
},
{
fieldKey: "cmdstat_georadius_ro",
fieldValue: "calls=75,usec=1024,usec_per_call=16.80,rejected_calls=5,failed_calls=ABC",
wantSuccess: false,
},
} {
t.Run(tst.fieldKey+tst.fieldValue, func(t *testing.T) {
cmd, calls, rejectedCalls, failedCalls, usecTotal, _, err := parseMetricsCommandStats(tst.fieldKey, tst.fieldValue)
if tst.wantSuccess && err != nil {
t.Fatalf("err: %s", err)
return
}
if !tst.wantSuccess && err == nil {
t.Fatalf("expected err!")
return
}
if !tst.wantSuccess {
return
}
if cmd != tst.wantCmd {
t.Fatalf("cmd not matching, got: %s, wanted: %s", cmd, tst.wantCmd)
}
if calls != tst.wantCalls {
t.Fatalf("cmd not matching, got: %f, wanted: %f", calls, tst.wantCalls)
}
if rejectedCalls != tst.wantRejectedCalls {
t.Fatalf("cmd not matching, got: %f, wanted: %f", rejectedCalls, tst.wantRejectedCalls)
}
if failedCalls != tst.wantFailedCalls {
t.Fatalf("cmd not matching, got: %f, wanted: %f", failedCalls, tst.wantFailedCalls)
}
if usecTotal != tst.wantUsecTotal {
t.Fatalf("cmd not matching, got: %f, wanted: %f", usecTotal, tst.wantUsecTotal)
}
})
}
}
func TestParseErrorStats(t *testing.T) {
for _, tst := range []struct {
fieldKey string
fieldValue string
wantSuccess bool
wantErrorPrefix string
wantCount float64
}{
{
fieldKey: "errorstat_ERR",
fieldValue: "count=4",
wantSuccess: true,
wantErrorPrefix: "ERR",
wantCount: 4,
},
{
fieldKey: "borked_stats",
fieldValue: "count=4",
wantSuccess: false,
},
{
fieldKey: "errorstat_ERR",
fieldValue: "borked_values",
wantSuccess: false,
},
{
fieldKey: "errorstat_ERR",
fieldValue: "count=ABC",
wantSuccess: false,
},
} {
t.Run(tst.fieldKey+tst.fieldValue, func(t *testing.T) {
errorPrefix, count, err := parseMetricsErrorStats(tst.fieldKey, tst.fieldValue)
if tst.wantSuccess && err != nil {
t.Fatalf("err: %s", err)
return
}
if !tst.wantSuccess && err == nil {
t.Fatalf("expected err!")
return
}
if !tst.wantSuccess {
return
}
if errorPrefix != tst.wantErrorPrefix {
t.Fatalf("cmd not matching, got: %s, wanted: %s", errorPrefix, tst.wantErrorPrefix)
}
if count != tst.wantCount {
t.Fatalf("cmd not matching, got: %f, wanted: %f", count, tst.wantCount)
}
})
}
}
func Test_parseMetricsLatencyStats(t *testing.T) {
type args struct {
fieldKey string
fieldValue string
}
tests := []struct {
name string
args args
wantCmd string
wantPercentileMap map[float64]float64
wantErr bool
}{
{
name: "simple",
args: args{fieldKey: "latency_percentiles_usec_ping", fieldValue: "p50=0.001,p99=1.003,p99.9=3.007"},
wantCmd: "ping",
wantPercentileMap: map[float64]float64{50.0: 0.001, 99.0: 1.003, 99.9: 3.007},
wantErr: false,
},
{
name: "single-percentile",
args: args{fieldKey: "latency_percentiles_usec_ping", fieldValue: "p50=0.001"},
wantCmd: "ping",
wantPercentileMap: map[float64]float64{50.0: 0.001},
wantErr: false,
},
{
name: "empty",
args: args{fieldKey: "latency_percentiles_usec_ping", fieldValue: ""},
wantCmd: "ping",
wantPercentileMap: map[float64]float64{0: 0},
wantErr: false,
},
{
name: "invalid-percentile",
args: args{fieldKey: "latency_percentiles_usec_ping", fieldValue: "p50=a"},
wantCmd: "ping",
wantPercentileMap: map[float64]float64{},
wantErr: true,
},
{
name: "invalid prefix",
args: args{fieldKey: "wrong_prefix_", fieldValue: "p50=0.001,p99=1.003,p99.9=3.007"},
wantCmd: "",
wantPercentileMap: map[float64]float64{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotCmd, gotPercentileMap, err := parseMetricsLatencyStats(tt.args.fieldKey, tt.args.fieldValue)
if (err != nil) != tt.wantErr {
t.Errorf("test %s. parseMetricsLatencyStats() error = %v, wantErr %v", tt.name, err, tt.wantErr)
return
}
if gotCmd != tt.wantCmd {
t.Errorf("parseMetricsLatencyStats() gotCmd = %v, want %v", gotCmd, tt.wantCmd)
}
if !reflect.DeepEqual(gotPercentileMap, tt.wantPercentileMap) {
t.Errorf("parseMetricsLatencyStats() gotPercentileMap = %v, want %v", gotPercentileMap, tt.wantPercentileMap)
}
})
}
}