forked from oliver006/redis_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslowlog_test.go
139 lines (113 loc) · 2.67 KB
/
slowlog_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
package exporter
import (
"os"
"strings"
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)
func TestSlowLog(t *testing.T) {
e := getTestExporter()
chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
oldSlowLogID := float64(0)
for m := range chM {
switch m := m.(type) {
case prometheus.Gauge:
if strings.Contains(m.Desc().String(), "slowlog_last_id") {
got := &dto.Metric{}
m.Write(got)
oldSlowLogID = got.GetGauge().GetValue()
}
}
}
setupSlowLog(t, os.Getenv("TEST_REDIS_URI"))
defer resetSlowLog(t, os.Getenv("TEST_REDIS_URI"))
chM = make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
for m := range chM {
switch m := m.(type) {
case prometheus.Gauge:
if strings.Contains(m.Desc().String(), "slowlog_last_id") {
got := &dto.Metric{}
m.Write(got)
val := got.GetGauge().GetValue()
if oldSlowLogID > val {
t.Errorf("no new slowlogs found")
}
}
if strings.Contains(m.Desc().String(), "slowlog_length") {
got := &dto.Metric{}
m.Write(got)
val := got.GetGauge().GetValue()
if val == 0 {
t.Errorf("slowlog length is zero")
}
}
}
}
resetSlowLog(t, os.Getenv("TEST_REDIS_URI"))
chM = make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
for m := range chM {
switch m := m.(type) {
case prometheus.Gauge:
if strings.Contains(m.Desc().String(), "slowlog_length") {
got := &dto.Metric{}
m.Write(got)
val := got.GetGauge().GetValue()
if val != 0 {
t.Errorf("Slowlog was not reset")
}
}
}
}
}
func setupSlowLog(t *testing.T, addr string) error {
c, err := redis.DialURL(addr)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
defer c.Close()
_, err = c.Do("CONFIG", "SET", "SLOWLOG-LOG-SLOWER-THAN", 10000)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
// Have to pass in the sleep time in seconds so we have to divide
// the number of milliseconds by 1000 to get number of seconds
_, err = c.Do("DEBUG", "SLEEP", TimeToSleep/1000.0)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
time.Sleep(time.Millisecond * 50)
return nil
}
func resetSlowLog(t *testing.T, addr string) error {
c, err := redis.DialURL(addr)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
defer c.Close()
_, err = c.Do("SLOWLOG", "RESET")
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
time.Sleep(time.Millisecond * 50)
return nil
}