/
hook.go
141 lines (110 loc) · 3.28 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
package pprueidis
import (
"bytes"
"context"
"strings"
"time"
"github.com/pinpoint-apm/pinpoint-go-agent"
"github.com/redis/rueidis"
_ "github.com/redis/rueidis/rueidishook"
)
type Hook struct {
endpoint string
}
// NewHook creates a ruedis.Hook to instrument redis calls.
func NewHook(opts rueidis.ClientOption) *Hook {
h := Hook{}
if opts.InitAddress == nil {
h.endpoint = "unknown"
} else {
h.endpoint = strings.Join(opts.InitAddress, ",")
}
return &h
}
func (h *Hook) Do(client rueidis.Client, ctx context.Context, cmd rueidis.Completed) (resp rueidis.RedisResult) {
tracer := h.newSpanEvent(ctx, "rueidis.Do()", strings.Join(cmd.Commands(), ","))
defer tracer.EndSpanEvent()
resp = client.Do(ctx, cmd)
if resp.Error() != nil {
tracer.SpanEvent().SetError(resp.Error())
}
return
}
func (h *Hook) DoMulti(client rueidis.Client, ctx context.Context, multi ...rueidis.Completed) (resps []rueidis.RedisResult) {
tracer := h.newSpanEvent(ctx, "rueidis.DoMulti()", cmdCompletedName(multi))
defer tracer.EndSpanEvent()
resps = client.DoMulti(ctx, multi...)
err := multiResultError(resps)
if err != nil {
tracer.SpanEvent().SetError(err)
}
return
}
func (h *Hook) DoCache(client rueidis.Client, ctx context.Context, cmd rueidis.Cacheable, ttl time.Duration) (resp rueidis.RedisResult) {
tracer := h.newSpanEvent(ctx, "rueidis.DoCache()", strings.Join(cmd.Commands(), ","))
defer tracer.EndSpanEvent()
resp = client.DoCache(ctx, cmd, ttl)
if resp.Error() != nil {
tracer.SpanEvent().SetError(resp.Error())
}
return
}
func (h *Hook) DoMultiCache(client rueidis.Client, ctx context.Context, multi ...rueidis.CacheableTTL) (resps []rueidis.RedisResult) {
tracer := h.newSpanEvent(ctx, "rueidis.DoMultiCache()", cmdCacheableName(multi))
defer tracer.EndSpanEvent()
resps = client.DoMultiCache(ctx, multi...)
err := multiResultError(resps)
if err != nil {
tracer.SpanEvent().SetError(err)
}
return
}
func (h *Hook) Receive(client rueidis.Client, ctx context.Context, subscribe rueidis.Completed, fn func(msg rueidis.PubSubMessage)) (err error) {
tracer := h.newSpanEvent(ctx, "rueidis.Receive()", strings.Join(subscribe.Commands(), ","))
defer tracer.EndSpanEvent()
err = client.Receive(ctx, subscribe, fn)
if err != nil {
tracer.SpanEvent().SetError(err)
}
return err
}
func (h *Hook) newSpanEvent(ctx context.Context, operation string, cmd string) pinpoint.Tracer {
tracer := pinpoint.FromContext(ctx)
se := tracer.NewSpanEvent(operation).SpanEvent()
se.SetServiceType(pinpoint.ServiceTypeRedis)
se.SetDestination("REDIS")
se.SetEndPoint(h.endpoint)
if cmd != "" {
se.Annotations().AppendString(pinpoint.AnnotationArgs0, cmd)
}
return tracer
}
func cmdCompletedName(cmds []rueidis.Completed) string {
var buf bytes.Buffer
for i, cmd := range cmds {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(strings.Join(cmd.Commands(), ","))
}
return buf.String()
}
func cmdCacheableName(cmds []rueidis.CacheableTTL) string {
var buf bytes.Buffer
for i, cmd := range cmds {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(strings.Join(cmd.Cmd.Commands(), ","))
}
return buf.String()
}
func multiResultError(cmds []rueidis.RedisResult) error {
for _, cmd := range cmds {
err := cmd.Error()
if err != nil {
return err
}
}
return nil
}