2024-07-04 15:13:19 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2024-07-18 11:28:45 +03:00
|
|
|
rediscmd "github.com/redis/go-redis/extra/rediscmd/v9"
|
2024-07-04 15:13:19 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"go.unistack.org/micro/v3/tracer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setTracing(rdb redis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) {
|
|
|
|
switch rdb := rdb.(type) {
|
|
|
|
case *redis.Client:
|
|
|
|
opt := rdb.Options()
|
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
2024-07-05 08:57:29 +03:00
|
|
|
rdb.AddHook(newTracingHook(connString, tr))
|
2024-07-04 15:13:19 +03:00
|
|
|
case *redis.ClusterClient:
|
|
|
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
|
|
|
opt := rdb.Options()
|
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
2024-07-05 08:57:29 +03:00
|
|
|
rdb.AddHook(newTracingHook(connString, tr))
|
2024-07-04 15:13:19 +03:00
|
|
|
})
|
|
|
|
case *redis.Ring:
|
|
|
|
rdb.OnNewNode(func(rdb *redis.Client) {
|
|
|
|
opt := rdb.Options()
|
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
2024-07-05 08:57:29 +03:00
|
|
|
rdb.AddHook(newTracingHook(connString, tr))
|
2024-07-04 15:13:19 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tracingHook struct {
|
|
|
|
tr tracer.Tracer
|
|
|
|
opts []tracer.SpanOption
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ redis.Hook = (*tracingHook)(nil)
|
|
|
|
|
|
|
|
func newTracingHook(connString string, tr tracer.Tracer, opts ...tracer.SpanOption) *tracingHook {
|
|
|
|
opts = append(opts, tracer.WithSpanKind(tracer.SpanKindClient))
|
|
|
|
if connString != "" {
|
|
|
|
opts = append(opts, tracer.WithSpanLabels("db.connection_string", connString))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tracingHook{
|
|
|
|
tr: tr,
|
|
|
|
opts: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
|
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2024-07-18 11:28:45 +03:00
|
|
|
/*
|
|
|
|
_, span := h.tr.Start(ctx, "redis.dial", h.opts...)
|
|
|
|
defer span.Finish()
|
|
|
|
*/
|
2024-07-05 12:59:05 +03:00
|
|
|
conn, err := hook(ctx, network, addr)
|
2024-07-18 11:28:45 +03:00
|
|
|
// recordError(span, err)
|
2024-07-05 08:57:29 +03:00
|
|
|
|
|
|
|
return conn, err
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
|
|
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
|
|
|
cmdString := rediscmd.CmdString(cmd)
|
2024-07-05 13:44:29 +03:00
|
|
|
var err error
|
2024-07-04 15:13:19 +03:00
|
|
|
|
2024-07-05 13:44:29 +03:00
|
|
|
switch cmdString {
|
|
|
|
case "cluster slots":
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
_, span := h.tr.Start(ctx, "redis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
|
|
|
defer func() {
|
|
|
|
recordError(span, err)
|
|
|
|
span.Finish()
|
|
|
|
}()
|
|
|
|
}
|
2024-07-04 15:13:19 +03:00
|
|
|
|
2024-07-05 13:44:29 +03:00
|
|
|
err = hook(ctx, cmd)
|
2024-07-05 08:57:29 +03:00
|
|
|
|
|
|
|
return err
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 08:57:29 +03:00
|
|
|
func (h *tracingHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
2024-07-04 15:13:19 +03:00
|
|
|
return func(ctx context.Context, cmds []redis.Cmder) error {
|
2024-07-05 08:57:29 +03:00
|
|
|
_, cmdsString := rediscmd.CmdsString(cmds)
|
2024-07-04 15:13:19 +03:00
|
|
|
|
|
|
|
opts := append(h.opts, tracer.WithSpanLabels(
|
|
|
|
"db.redis.num_cmd", strconv.Itoa(len(cmds)),
|
|
|
|
"db.statement", cmdsString,
|
|
|
|
))
|
|
|
|
|
2024-07-05 12:59:05 +03:00
|
|
|
_, span := h.tr.Start(ctx, "redis.process_pipeline", opts...)
|
2024-07-04 15:13:19 +03:00
|
|
|
defer span.Finish()
|
|
|
|
|
2024-07-05 12:59:05 +03:00
|
|
|
err := hook(ctx, cmds)
|
2024-07-05 08:57:29 +03:00
|
|
|
recordError(span, err)
|
|
|
|
|
|
|
|
return err
|
2024-07-04 15:13:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 09:16:08 +03:00
|
|
|
func setSpanError(ctx context.Context, err error) {
|
|
|
|
if err == nil || err == redis.Nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sp, ok := tracer.SpanFromContext(ctx); !ok && sp != nil {
|
|
|
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-04 15:13:19 +03:00
|
|
|
func recordError(span tracer.Span, err error) {
|
2024-07-05 08:57:29 +03:00
|
|
|
if err != nil && err != redis.Nil {
|
2024-07-04 15:13:19 +03:00
|
|
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatDBConnString(network, addr string) string {
|
|
|
|
if network == "tcp" {
|
|
|
|
network = "redis"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s://%s", network, addr)
|
|
|
|
}
|