-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook.go
63 lines (51 loc) · 1.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
package opentracing_redis
import (
"context"
"fmt"
"strings"
"github.com/go-redis/redis/v8"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
func Wrap(client redis.UniversalClient) {
client.AddHook(&tracingHook{})
}
type tracingHook struct{}
func (h *tracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, command(cmd))
tagging(span)
Customize(span, cmd)
return ctx, nil
}
func (h *tracingHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
span := opentracing.SpanFromContext(ctx)
span.Finish()
return nil
}
func (h *tracingHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, command(cmds...))
tagging(span)
for _, cmd := range cmds {
Customize(span, cmd)
}
return ctx, nil
}
func (h *tracingHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
span := opentracing.SpanFromContext(ctx)
span.Finish()
return nil
}
func command(cmds ...redis.Cmder) string {
var commands []string
for _, cmd := range cmds {
commands = append(commands, fmt.Sprintf("%v", cmd.String()))
}
return strings.Join(commands, ", ")
}
func tagging(span opentracing.Span) {
ext.Component.Set(span, "redis")
ext.DBType.Set(span, "redis")
ext.SpanKind.Set(span, "client")
}
type customizer func(opentracing.Span, redis.Cmder)
var Customize customizer = func(opentracing.Span, redis.Cmder) {}