forked from lateefj/slowgrog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler.go
70 lines (61 loc) · 1.45 KB
/
sampler.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
package main
import
//"fmt"
"strings"
const ()
func SampleInfo(cmds DataCmds, status *RedisStatus) (string, error) {
Logger.Debug("Sampling INFO...")
info, err := cmds.InfoCmd()
lines := strings.Split(info, "\n")
for _, l := range lines {
if strings.IndexAny(l, ":") > -1 {
l = strings.Trim(l, "\n")
l = strings.Trim(l, "\r")
kv := strings.Split(l, ":")
if len(kv) == 2 {
status.Info[kv[0]] = kv[1]
}
}
}
return info, err
}
func SampleSlowlog(cmds DataCmds, status *RedisStatus) ([]Slowlog, error) {
Logger.Debug("Sampling slowlog...")
logs, err := cmds.SlowlogCmd()
if err != nil {
Logger.Errorf("DataCmds slowlog failed ewith error %s", err)
return logs, err
}
status.Slowlogs = logs
return logs, nil
}
func SampleMonitor(cmds DataCmds, stopper chan bool, status *RedisStatus) {
replies := cmds.MonitorCmd(stopper)
replyIndex := 0
for {
reply, ok := <-replies
if !ok {
continue
}
cmdMon, err := ParseMonitorLine(reply)
if err != nil {
Logger.Errorf("Failed to parse line: %s", reply)
}
if cmdMon != nil {
// Append if room else write over them
if len(status.MonitorSample) <= replyIndex {
status.MonitorSample = append(status.MonitorSample, cmdMon)
} else {
status.MonitorSample[replyIndex] = cmdMon
}
// Stats tracking
status.stats.IncCmdCount(cmdMon.Text)
// Increment index else reset to 0
if replyIndex < CmdLimit-1 {
replyIndex++
} else {
replyIndex = 0
}
}
}
}