Skip to content

Commit

Permalink
Merge pull request #740 from kongfei605/redis_up
Browse files Browse the repository at this point in the history
redis slow log query
  • Loading branch information
kongfei605 committed Dec 21, 2023
2 parents 9cbc269 + 158fe6e commit 788e566
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions conf/input.redis/redis.toml
Expand Up @@ -7,6 +7,17 @@
# password = ""
# pool_size = 2

## 是否开启slowlog 收集
# gather_slowlog = true
## 最多收集少条slowlog
# slowlog_max_len = 100
## 收集距离现在多少秒以内的slowlog
## 注意插件的采集周期,该参数不要小于采集周期,否则会有slowlog查不到
# slowlog_time_window=30

# 指标
# redis_slow_log{ident=dev-01 client_addr=127.0.0.1:56364 client_name= cmd="info ALL" log_id=983} 74 (单位微秒)

# # Optional. Specify redis commands to retrieve values
# commands = [
# {command = ["get", "sample-key1"], metric = "custom_metric_name1"},
Expand Down
34 changes: 34 additions & 0 deletions inputs/redis/redis.go
Expand Up @@ -36,6 +36,10 @@ type Instance struct {
PoolSize int `toml:"pool_size"`
Commands []Command `toml:"commands"`

GatherSlowLog bool `toml:"gather_slowlog"`
SlowLogMaxLen int64 `toml:"slowlog_max_len"`
SlowLogTimeWindow int64 `toml:"slowlog_time_window"`

tls.ClientConfig
client *redis.Client
}
Expand All @@ -61,6 +65,10 @@ func (ins *Instance) Init() error {
}

ins.client = redis.NewClient(redisOptions)

if ins.SlowLogTimeWindow == 0 {
ins.SlowLogTimeWindow = 300
}
return nil
}

Expand Down Expand Up @@ -121,9 +129,35 @@ func (ins *Instance) Gather(slist *types.SampleList) {
}

ins.gatherInfoAll(slist, tags)
ins.gatherSlowLog(slist, tags)
ins.gatherCommandValues(slist, tags)
}

func (ins *Instance) gatherSlowLog(slist *types.SampleList, tags map[string]string) {
if !ins.GatherSlowLog {
return
}
info, err := ins.client.SlowLogGet(context.Background(), ins.SlowLogMaxLen).Result()
if err != nil {
log.Println("E! get slow log err:", err)
return
}
now := time.Now().Unix()
for i := range info {
if now-info[i].Time.Unix() > ins.SlowLogTimeWindow {
continue
}
val := info[i].Duration.Microseconds()
labels := map[string]string{
"client_addr": info[i].ClientAddr,
"client_name": info[i].ClientName,
"log_id": fmt.Sprint(info[i].ID),
"cmd": strings.Join(info[i].Args, " "),
}
slist.PushFront(types.NewSample(inputName, "slow_log", val, labels).SetTime(info[i].Time))
}
}

func (ins *Instance) gatherCommandValues(slist *types.SampleList, tags map[string]string) {
fields := make(map[string]interface{})
for _, cmd := range ins.Commands {
Expand Down

0 comments on commit 788e566

Please sign in to comment.