forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_hook_util.go
176 lines (154 loc) · 3.96 KB
/
redis_hook_util.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
package gorr
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"reflect"
"strings"
"github.com/go-redis/redis"
)
var (
redisHasHook = false
redisHasProcessWrap = true
errRedisPipeNorm = fmt.Errorf("redis dummy error for pipelinek")
)
func init() {
var c *redis.Client
_, redisHasHook = reflect.TypeOf(c).MethodByName("AddHook")
_, redisHasProcessWrap = reflect.TypeOf(c).MethodByName("WrapProcess")
}
func buildRedisClientId(c *redis.Client) string {
opt := c.Options()
id := fmt.Sprintf("redis_client_id@%s@%s", opt.Addr, opt.Network)
return id
}
func buildRedisClusterClientId(c *redis.ClusterClient) string {
opt := c.Options()
id := fmt.Sprintf("redis_cluster_client_id@%s", strings.Join(opt.Addrs, "#"))
return id
}
func buildRedisCmdKey(id string, cmd redis.Cmder) string {
args := cmd.Args()
ss := make([]string, 0, len(args)+1)
switch cmd.(type) {
case *redis.StringCmd:
ss = append(ss, "StringCmd@")
case *redis.StatusCmd:
ss = append(ss, "StatusCmd@")
case *redis.IntCmd:
ss = append(ss, "IntCmd@")
case *redis.FloatCmd:
ss = append(ss, "FloatCmd@")
case *redis.BoolCmd:
ss = append(ss, "BoolCmd@")
case *redis.StringSliceCmd:
ss = append(ss, "StringSliceCmd@")
default:
// panic("not supported redis cmd type")
ss = append(ss, cmd.Name())
}
for _, arg := range args {
ss = append(ss, fmt.Sprint(arg))
}
cs := strings.Join(ss, "@")
key := fmt.Sprintf("%s@%s@%s", GlobalMgr.GetCurTraceId(), id, cs)
return key
}
func saveRedisCmdValue(key string, cmd redis.Cmder) {
var err error
var buff bytes.Buffer
switch c := cmd.(type) {
case *redis.StringCmd:
var val string
val, err = c.Result()
binary.Write(&buff, binary.LittleEndian, []byte(val))
case *redis.StatusCmd:
var val string
val, err = c.Result()
binary.Write(&buff, binary.LittleEndian, []byte(val))
case *redis.IntCmd:
var val int64
val, err = c.Result()
binary.Write(&buff, binary.LittleEndian, val)
case *redis.FloatCmd:
var val float64
val, err = c.Result()
binary.Write(&buff, binary.LittleEndian, val)
case *redis.BoolCmd:
var val bool
val, err = c.Result()
binary.Write(&buff, binary.LittleEndian, val)
case *redis.StringSliceCmd:
var val []string
val, err = c.Result()
binary.Write(&buff, binary.LittleEndian, int32(len(val)))
for _, v := range val {
binary.Write(&buff, binary.LittleEndian, int32(len(v)))
binary.Write(&buff, binary.LittleEndian, []byte(v))
}
default:
GlobalMgr.notifier("redis cmd recording for not-supported cmd", key, []byte(""))
return
}
if err == nil || err == redis.Nil {
GlobalMgr.StoreValue(key, buff.Bytes())
GlobalMgr.notifier("redis cmd recording", key, buff.Bytes())
} else {
GlobalMgr.notifier("redis cmd not recording", fmt.Sprintf("cmd error occurs, key:%s, err:%s", key, err.Error()), nil)
}
}
func addKeyToRedisCmd(cmd redis.Cmder, key string) {
switch c := cmd.(type) {
case *redis.StringCmd:
arg := c.Args()
arg[0] = key
case *redis.StatusCmd:
arg := c.Args()
arg[0] = key
case *redis.IntCmd:
arg := c.Args()
arg[0] = key
case *redis.FloatCmd:
arg := c.Args()
arg[0] = key
case *redis.BoolCmd:
arg := c.Args()
arg[0] = key
case *redis.StringSliceCmd:
arg := c.Args()
arg[0] = key
default:
// panic("not supported redis cmd type")
GlobalMgr.notifier("redis cmd replaying not supported cmd", key, []byte(""))
}
}
func getStoredValue(args []interface{}) ([]byte, error) {
var err error
var value []byte
var key string
var ok bool
sz := len(args)
for {
if sz <= 0 {
err = errors.New("invalid arg size, key unavailable")
break
}
key, ok = args[0].(string)
if !ok {
err = errors.New("get key from args failed, invalid type")
break
}
value, err = GlobalMgr.GetValue(key)
break
}
if err == nil {
GlobalMgr.notifier("redis cmd replaying done", key, value)
} else {
GlobalMgr.notifier("redis cmd replaying failed", key, []byte(err.Error()))
}
if err == nil && len(value) == 0 {
return nil, redis.Nil
}
return value, err
}