-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook.go
214 lines (183 loc) · 4.53 KB
/
hook.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
package redis
import (
"context"
"fmt"
"strconv"
"time"
"unicode/utf8"
"unsafe"
"github.com/go-redis/redis/v8"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
)
type OpenTelemetryHook struct{}
var _ redis.Hook = OpenTelemetryHook{}
func (OpenTelemetryHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
b := make([]byte, 32)
b = appendCmd(b, cmd)
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
if trace {
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
parentCtx := parentSpan.Context()
span := opentracing.StartSpan("redis", opentracing.ChildOf(parentCtx))
ext.SpanKindRPCClient.Set(span)
ext.PeerService.Set(span, "redis")
span.LogFields(log.String("cmd", String(b)))
ctx = opentracing.ContextWithSpan(ctx, span)
}
}
return ctx, nil
}
func (OpenTelemetryHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
if trace {
span := opentracing.SpanFromContext(ctx)
if span != nil {
defer span.Finish()
}
}
return nil
}
func (OpenTelemetryHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
if trace {
const numCmdLimit = 100
const numNameLimit = 10
seen := make(map[string]struct{}, len(cmds))
unqNames := make([]string, 0, len(cmds))
b := make([]byte, 0, 32*len(cmds))
for i, cmd := range cmds {
if i > numCmdLimit {
break
}
if i > 0 {
b = append(b, '\n')
}
b = appendCmd(b, cmd)
if len(unqNames) >= numNameLimit {
continue
}
name := cmd.FullName()
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
unqNames = append(unqNames, name)
}
}
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
parentCtx := parentSpan.Context()
span := opentracing.StartSpan("redis", opentracing.ChildOf(parentCtx))
ext.SpanKindRPCClient.Set(span)
ext.PeerService.Set(span, "redis")
// span.SetTag("redis.cmds", String(b))
span.LogFields(log.String("cmds", String(b)))
ctx = opentracing.ContextWithSpan(ctx, span)
}
}
return ctx, nil
}
func String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func (OpenTelemetryHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
if ctx == nil || ctx.Err() != nil {
ctx = context.TODO()
}
if trace {
span := opentracing.SpanFromContext(ctx)
if span != nil {
defer span.Finish()
}
}
return nil
}
func appendCmd(b []byte, cmd redis.Cmder) []byte {
const lenLimit = 64
for i, arg := range cmd.Args() {
if i > 0 {
b = append(b, ' ')
}
start := len(b)
b = AppendArg(b, arg)
if len(b)-start > lenLimit {
b = append(b[:start+lenLimit], "..."...)
}
}
if err := cmd.Err(); err != nil {
b = append(b, ": "...)
b = append(b, err.Error()...)
}
return b
}
func AppendArg(b []byte, v interface{}) []byte {
switch v := v.(type) {
case nil:
return append(b, "<nil>"...)
case string:
return appendUTF8String(b, v)
case []byte:
return appendUTF8String(b, String(v))
case int:
return strconv.AppendInt(b, int64(v), 10)
case int8:
return strconv.AppendInt(b, int64(v), 10)
case int16:
return strconv.AppendInt(b, int64(v), 10)
case int32:
return strconv.AppendInt(b, int64(v), 10)
case int64:
return strconv.AppendInt(b, v, 10)
case uint:
return strconv.AppendUint(b, uint64(v), 10)
case uint8:
return strconv.AppendUint(b, uint64(v), 10)
case uint16:
return strconv.AppendUint(b, uint64(v), 10)
case uint32:
return strconv.AppendUint(b, uint64(v), 10)
case uint64:
return strconv.AppendUint(b, v, 10)
case float32:
return strconv.AppendFloat(b, float64(v), 'f', -1, 64)
case float64:
return strconv.AppendFloat(b, v, 'f', -1, 64)
case bool:
if v {
return append(b, "true"...)
}
return append(b, "false"...)
case time.Time:
return v.AppendFormat(b, time.RFC3339Nano)
default:
return append(b, fmt.Sprint(v)...)
}
}
func appendUTF8String(b []byte, s string) []byte {
for _, r := range s {
b = appendRune(b, r)
}
return b
}
func appendRune(b []byte, r rune) []byte {
if r < utf8.RuneSelf {
switch c := byte(r); c {
case '\n':
return append(b, "\\n"...)
case '\r':
return append(b, "\\r"...)
default:
return append(b, c)
}
}
l := len(b)
b = append(b, make([]byte, utf8.UTFMax)...)
n := utf8.EncodeRune(b[l:l+utf8.UTFMax], r)
b = b[:l+n]
return b
}