You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
3.4 KiB
Go
154 lines
3.4 KiB
Go
3 years ago
|
package redis
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
10 months ago
|
"errors"
|
||
2 years ago
|
"io"
|
||
|
"net"
|
||
3 years ago
|
"strings"
|
||
|
"time"
|
||
|
|
||
10 months ago
|
red "github.com/redis/go-redis/v9"
|
||
2 years ago
|
"github.com/zeromicro/go-zero/core/breaker"
|
||
3 years ago
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
"github.com/zeromicro/go-zero/core/mapping"
|
||
|
"github.com/zeromicro/go-zero/core/timex"
|
||
3 years ago
|
"github.com/zeromicro/go-zero/core/trace"
|
||
3 years ago
|
"go.opentelemetry.io/otel/attribute"
|
||
|
"go.opentelemetry.io/otel/codes"
|
||
|
oteltrace "go.opentelemetry.io/otel/trace"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
// spanName is the span name of the redis calls.
|
||
|
const spanName = "redis"
|
||
|
|
||
3 years ago
|
var (
|
||
9 months ago
|
defaultDurationHook = durationHook{}
|
||
3 years ago
|
redisCmdsAttributeKey = attribute.Key("redis.cmds")
|
||
3 years ago
|
)
|
||
|
|
||
9 months ago
|
type durationHook struct {
|
||
|
}
|
||
3 years ago
|
|
||
9 months ago
|
func (h durationHook) DialHook(next red.DialHook) red.DialHook {
|
||
10 months ago
|
return next
|
||
3 years ago
|
}
|
||
|
|
||
9 months ago
|
func (h durationHook) ProcessHook(next red.ProcessHook) red.ProcessHook {
|
||
10 months ago
|
return func(ctx context.Context, cmd red.Cmder) error {
|
||
|
start := timex.Now()
|
||
|
ctx, endSpan := h.startSpan(ctx, cmd)
|
||
3 years ago
|
|
||
10 months ago
|
err := next(ctx, cmd)
|
||
3 years ago
|
|
||
10 months ago
|
endSpan(err)
|
||
|
duration := timex.Since(start)
|
||
3 years ago
|
|
||
10 months ago
|
if duration > slowThreshold.Load() {
|
||
|
logDuration(ctx, []red.Cmder{cmd}, duration)
|
||
|
metricSlowCount.Inc(cmd.Name())
|
||
|
}
|
||
2 years ago
|
|
||
10 months ago
|
metricReqDur.Observe(duration.Milliseconds(), cmd.Name())
|
||
|
if msg := formatError(err); len(msg) > 0 {
|
||
|
metricReqErr.Inc(cmd.Name(), msg)
|
||
|
}
|
||
3 years ago
|
|
||
10 months ago
|
return err
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
|
||
9 months ago
|
func (h durationHook) ProcessPipelineHook(next red.ProcessPipelineHook) red.ProcessPipelineHook {
|
||
10 months ago
|
return func(ctx context.Context, cmds []red.Cmder) error {
|
||
|
if len(cmds) == 0 {
|
||
|
return next(ctx, cmds)
|
||
3 years ago
|
}
|
||
|
|
||
10 months ago
|
start := timex.Now()
|
||
|
ctx, endSpan := h.startSpan(ctx, cmds...)
|
||
3 years ago
|
|
||
10 months ago
|
err := next(ctx, cmds)
|
||
3 years ago
|
|
||
10 months ago
|
endSpan(err)
|
||
|
duration := timex.Since(start)
|
||
|
if duration > slowThreshold.Load()*time.Duration(len(cmds)) {
|
||
|
logDuration(ctx, cmds, duration)
|
||
|
}
|
||
3 years ago
|
|
||
10 months ago
|
metricReqDur.Observe(duration.Milliseconds(), "Pipeline")
|
||
|
if msg := formatError(err); len(msg) > 0 {
|
||
|
metricReqErr.Inc("Pipeline", msg)
|
||
|
}
|
||
3 years ago
|
|
||
10 months ago
|
return err
|
||
2 years ago
|
}
|
||
3 years ago
|
}
|
||
|
|
||
9 months ago
|
func (h durationHook) startSpan(ctx context.Context, cmds ...red.Cmder) (context.Context, func(err error)) {
|
||
9 months ago
|
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)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func formatError(err error) string {
|
||
10 months ago
|
if err == nil || errors.Is(err, red.Nil) {
|
||
2 years ago
|
return ""
|
||
|
}
|
||
|
|
||
10 months ago
|
var opErr *net.OpError
|
||
|
ok := errors.As(err, &opErr)
|
||
2 years ago
|
if ok && opErr.Timeout() {
|
||
|
return "timeout"
|
||
|
}
|
||
|
|
||
10 months ago
|
switch {
|
||
9 months ago
|
case errors.Is(err, io.EOF):
|
||
2 years ago
|
return "eof"
|
||
10 months ago
|
case errors.Is(err, context.DeadlineExceeded):
|
||
2 years ago
|
return "context deadline"
|
||
10 months ago
|
case errors.Is(err, breaker.ErrServiceUnavailable):
|
||
|
return "breaker open"
|
||
2 years ago
|
default:
|
||
|
return "unexpected error"
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
func logDuration(ctx context.Context, cmds []red.Cmder, duration time.Duration) {
|
||
3 years ago
|
var buf strings.Builder
|
||
2 years ago
|
for k, cmd := range cmds {
|
||
|
if k > 0 {
|
||
|
buf.WriteByte('\n')
|
||
3 years ago
|
}
|
||
2 years ago
|
var build strings.Builder
|
||
|
for i, arg := range cmd.Args() {
|
||
|
if i > 0 {
|
||
|
build.WriteByte(' ')
|
||
|
}
|
||
|
build.WriteString(mapping.Repr(arg))
|
||
|
}
|
||
2 years ago
|
buf.WriteString(build.String())
|
||
3 years ago
|
}
|
||
|
logx.WithContext(ctx).WithDuration(duration).Slowf("[REDIS] slowcall on executing: %s", buf.String())
|
||
|
}
|