-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
durationhook.go
153 lines (125 loc) · 3.41 KB
/
durationhook.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
package redis
import (
"context"
"errors"
"io"
"net"
"strings"
"time"
red "github.com/redis/go-redis/v9"
"github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/mapping"
"github.com/zeromicro/go-zero/core/timex"
"github.com/zeromicro/go-zero/core/trace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
oteltrace "go.opentelemetry.io/otel/trace"
)
// spanName is the span name of the redis calls.
const spanName = "redis"
var (
defaultDurationHook = durationHook{}
redisCmdsAttributeKey = attribute.Key("redis.cmds")
)
type durationHook struct {
}
func (h durationHook) DialHook(next red.DialHook) red.DialHook {
return next
}
func (h durationHook) ProcessHook(next red.ProcessHook) red.ProcessHook {
return func(ctx context.Context, cmd red.Cmder) error {
start := timex.Now()
ctx, endSpan := h.startSpan(ctx, cmd)
err := next(ctx, cmd)
endSpan(err)
duration := timex.Since(start)
if duration > slowThreshold.Load() {
logDuration(ctx, []red.Cmder{cmd}, duration)
metricSlowCount.Inc(cmd.Name())
}
metricReqDur.Observe(duration.Milliseconds(), cmd.Name())
if msg := formatError(err); len(msg) > 0 {
metricReqErr.Inc(cmd.Name(), msg)
}
return err
}
}
func (h durationHook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook {
return func(ctx context.Context, cmds []red.Cmder) error {
if len(cmds) == 0 {
return next(ctx, cmds)
}
start := timex.Now()
ctx, endSpan := h.startSpan(ctx, cmds...)
err := next(ctx, cmds)
endSpan(err)
duration := timex.Since(start)
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
logDuration(ctx, cmds, duration)
}
metricReqDur.Observe(duration.Milliseconds(), "Pipeline")
if msg := formatError(err); len(msg) > 0 {
metricReqErr.Inc("Pipeline", msg)
}
return err
}
}
func (h durationHook) startSpan(ctx context.Context, cmds ...red.Cmder) (context.Context, func(err error)) {
tracer := trace.TracerFromContext(ctx)
ctx, span := tracer.Start(ctx,
spanName,
oteltrace.WithSpanKind(oteltrace.SpanKindClient),
)
cmdStrs := make([]string, 0, len(cmds))
for _, cmd := range cmds {
cmdStrs = append(cmdStrs, cmd.Name())
}
span.SetAttributes(redisCmdsAttributeKey.StringSlice(cmdStrs))
return ctx, func(err error) {
defer span.End()
if err == nil || errors.Is(err, red.Nil) {
span.SetStatus(codes.Ok, "")
return
}
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
}
}
func formatError(err error) string {
if err == nil || errors.Is(err, red.Nil) {
return ""
}
var opErr *net.OpError
ok := errors.As(err, &opErr)
if ok && opErr.Timeout() {
return "timeout"
}
switch {
case errors.Is(err, io.EOF):
return "eof"
case errors.Is(err, context.DeadlineExceeded):
return "context deadline"
case errors.Is(err, breaker.ErrServiceUnavailable):
return "breaker open"
default:
return "unexpected error"
}
}
func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration) {
var buf strings.Builder
for k, cmd := range cmds {
if k > 0 {
buf.WriteByte('\n')
}
var build strings.Builder
for i, arg := range cmd.Args() {
if i > 0 {
build.WriteByte(' ')
}
build.WriteString(mapping.Repr(arg))
}
buf.WriteString(build.String())
}
logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
}