-
Notifications
You must be signed in to change notification settings - Fork 4
/
otredis.go
72 lines (64 loc) · 2.26 KB
/
otredis.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
package otredis
import (
"context"
"strings"
"github.com/go-redis/redis"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
// WrapRedisClient adds opentracing measurements for commands and returns cloned client
func WrapRedisClient(ctx context.Context, client *redis.Client) *redis.Client {
if ctx == nil {
return client
}
parentSpan := opentracing.SpanFromContext(ctx)
if parentSpan == nil {
return client
}
// clone using context
ctxClient := client.WithContext(ctx)
opts := ctxClient.Options()
ctxClient.WrapProcess(process(parentSpan, opts))
ctxClient.WrapProcessPipeline(processPipeline(parentSpan, opts))
return ctxClient
}
func process(parentSpan opentracing.Span, opts *redis.Options) func(oldProcess func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(oldProcess func(cmd redis.Cmder) error) func(cmd redis.Cmder) error {
return func(cmd redis.Cmder) error {
dbMethod := formatCommandAsDbMethod(cmd)
span := getSpan(parentSpan, opts, "redis-cmd", dbMethod)
defer span.Finish()
return oldProcess(cmd)
}
}
}
func processPipeline(parentSpan opentracing.Span, opts *redis.Options) func(oldProcess func(cmds []redis.Cmder) error) func(cmds []redis.Cmder) error {
return func(oldProcess func(cmds []redis.Cmder) error) func(cmds []redis.Cmder) error {
return func(cmds []redis.Cmder) error {
dbMethod := formatCommandsAsDbMethods(cmds)
span := getSpan(parentSpan, opts, "redis-pipeline-cmd", dbMethod)
defer span.Finish()
return oldProcess(cmds)
}
}
}
func formatCommandAsDbMethod(cmd redis.Cmder) string {
return cmd.Name()
}
func formatCommandsAsDbMethods(cmds []redis.Cmder) string {
cmdsAsDbMethods := make([]string, len(cmds))
for i, cmd := range cmds {
dbMethod := formatCommandAsDbMethod(cmd)
cmdsAsDbMethods[i] = dbMethod
}
return strings.Join(cmdsAsDbMethods, " -> ")
}
func getSpan(parentSpan opentracing.Span, opts *redis.Options, operationName, dbMethod string) opentracing.Span {
tracer := parentSpan.Tracer()
span := tracer.StartSpan(operationName, opentracing.ChildOf(parentSpan.Context()))
ext.DBType.Set(span, "redis")
ext.PeerAddress.Set(span, opts.Addr)
ext.SpanKind.Set(span, ext.SpanKindEnum("client"))
span.SetTag("db.method", dbMethod)
return span
}