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-10-05 16:11:46 +03:00
|
|
|
goredis "github.com/redis/go-redis/v9"
|
2024-07-04 15:13:19 +03:00
|
|
|
"go.unistack.org/micro/v3/tracer"
|
|
|
|
)
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func setTracing(rdb goredis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) {
|
2024-07-04 15:13:19 +03:00
|
|
|
switch rdb := rdb.(type) {
|
2024-10-05 16:11:46 +03:00
|
|
|
case *goredis.Client:
|
2024-07-04 15:13:19 +03:00
|
|
|
opt := rdb.Options()
|
|
|
|
connString := formatDBConnString(opt.Network, opt.Addr)
|
2024-07-05 08:57:29 +03:00
|
|
|
rdb.AddHook(newTracingHook(connString, tr))
|
2024-10-05 16:11:46 +03:00
|
|
|
case *goredis.ClusterClient:
|
|
|
|
rdb.OnNewNode(func(rdb *goredis.Client) {
|
2024-07-04 15:13:19 +03:00
|
|
|
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
|
|
|
})
|
2024-10-05 16:11:46 +03:00
|
|
|
case *goredis.Ring:
|
|
|
|
rdb.OnNewNode(func(rdb *goredis.Client) {
|
2024-07-04 15:13:19 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
var _ goredis.Hook = (*tracingHook)(nil)
|
2024-07-04 15:13:19 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func (h *tracingHook) DialHook(hook goredis.DialHook) goredis.DialHook {
|
2024-07-04 15:13:19 +03:00
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2024-07-18 11:28:45 +03:00
|
|
|
/*
|
2024-10-05 16:11:46 +03:00
|
|
|
_, span := h.tr.Start(ctx, "goredis.dial", h.opts...)
|
2024-07-18 11:28:45 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-05 16:11:46 +03:00
|
|
|
func (h *tracingHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook {
|
|
|
|
return func(ctx context.Context, cmd goredis.Cmder) error {
|
2024-07-04 15:13:19 +03:00
|
|
|
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:
|
2024-11-22 16:06:59 +03:00
|
|
|
_, span := h.tr.Start(ctx, "sdk.database", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
2024-07-05 13:44:29 +03:00
|
|
|
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-10-05 16:11:46 +03:00
|
|
|
func (h *tracingHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) goredis.ProcessPipelineHook {
|
|
|
|
return func(ctx context.Context, cmds []goredis.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(
|
2024-11-22 16:06:59 +03:00
|
|
|
"db.database.num_cmd", strconv.Itoa(len(cmds)),
|
2024-07-04 15:13:19 +03:00
|
|
|
"db.statement", cmdsString,
|
|
|
|
))
|
|
|
|
|
2024-11-22 16:06:59 +03:00
|
|
|
_, span := h.tr.Start(ctx, "sdk.database", 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) {
|
2024-10-05 16:11:46 +03:00
|
|
|
if err == nil || err == goredis.Nil {
|
2024-07-05 09:16:08 +03:00
|
|
|
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-10-05 16:11:46 +03:00
|
|
|
if err != nil && err != goredis.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)
|
|
|
|
}
|