switch to universal client
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
38
tracer.go
38
tracer.go
@@ -7,24 +7,24 @@ import (
|
||||
"strconv"
|
||||
|
||||
rediscmd "github.com/redis/go-redis/extra/rediscmd/v9"
|
||||
"github.com/redis/go-redis/v9"
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
"go.unistack.org/micro/v3/tracer"
|
||||
)
|
||||
|
||||
func setTracing(rdb redis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) {
|
||||
func setTracing(rdb goredis.UniversalClient, tr tracer.Tracer, opts ...tracer.SpanOption) {
|
||||
switch rdb := rdb.(type) {
|
||||
case *redis.Client:
|
||||
case *goredis.Client:
|
||||
opt := rdb.Options()
|
||||
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||
rdb.AddHook(newTracingHook(connString, tr))
|
||||
case *redis.ClusterClient:
|
||||
rdb.OnNewNode(func(rdb *redis.Client) {
|
||||
case *goredis.ClusterClient:
|
||||
rdb.OnNewNode(func(rdb *goredis.Client) {
|
||||
opt := rdb.Options()
|
||||
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||
rdb.AddHook(newTracingHook(connString, tr))
|
||||
})
|
||||
case *redis.Ring:
|
||||
rdb.OnNewNode(func(rdb *redis.Client) {
|
||||
case *goredis.Ring:
|
||||
rdb.OnNewNode(func(rdb *goredis.Client) {
|
||||
opt := rdb.Options()
|
||||
connString := formatDBConnString(opt.Network, opt.Addr)
|
||||
rdb.AddHook(newTracingHook(connString, tr))
|
||||
@@ -37,7 +37,7 @@ type tracingHook struct {
|
||||
opts []tracer.SpanOption
|
||||
}
|
||||
|
||||
var _ redis.Hook = (*tracingHook)(nil)
|
||||
var _ goredis.Hook = (*tracingHook)(nil)
|
||||
|
||||
func newTracingHook(connString string, tr tracer.Tracer, opts ...tracer.SpanOption) *tracingHook {
|
||||
opts = append(opts, tracer.WithSpanKind(tracer.SpanKindClient))
|
||||
@@ -51,10 +51,10 @@ func newTracingHook(connString string, tr tracer.Tracer, opts ...tracer.SpanOpti
|
||||
}
|
||||
}
|
||||
|
||||
func (h *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
|
||||
func (h *tracingHook) DialHook(hook goredis.DialHook) goredis.DialHook {
|
||||
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
/*
|
||||
_, span := h.tr.Start(ctx, "redis.dial", h.opts...)
|
||||
_, span := h.tr.Start(ctx, "goredis.dial", h.opts...)
|
||||
defer span.Finish()
|
||||
*/
|
||||
conn, err := hook(ctx, network, addr)
|
||||
@@ -64,8 +64,8 @@ func (h *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
||||
return func(ctx context.Context, cmd redis.Cmder) error {
|
||||
func (h *tracingHook) ProcessHook(hook goredis.ProcessHook) goredis.ProcessHook {
|
||||
return func(ctx context.Context, cmd goredis.Cmder) error {
|
||||
cmdString := rediscmd.CmdString(cmd)
|
||||
var err error
|
||||
|
||||
@@ -73,7 +73,7 @@ func (h *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
||||
case "cluster slots":
|
||||
break
|
||||
default:
|
||||
_, span := h.tr.Start(ctx, "redis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
||||
_, span := h.tr.Start(ctx, "goredis.process", append(h.opts, tracer.WithSpanLabels("db.statement", cmdString))...)
|
||||
defer func() {
|
||||
recordError(span, err)
|
||||
span.Finish()
|
||||
@@ -86,16 +86,16 @@ func (h *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *tracingHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
||||
return func(ctx context.Context, cmds []redis.Cmder) error {
|
||||
func (h *tracingHook) ProcessPipelineHook(hook goredis.ProcessPipelineHook) goredis.ProcessPipelineHook {
|
||||
return func(ctx context.Context, cmds []goredis.Cmder) error {
|
||||
_, cmdsString := rediscmd.CmdsString(cmds)
|
||||
|
||||
opts := append(h.opts, tracer.WithSpanLabels(
|
||||
"db.redis.num_cmd", strconv.Itoa(len(cmds)),
|
||||
"db.goredis.num_cmd", strconv.Itoa(len(cmds)),
|
||||
"db.statement", cmdsString,
|
||||
))
|
||||
|
||||
_, span := h.tr.Start(ctx, "redis.process_pipeline", opts...)
|
||||
_, span := h.tr.Start(ctx, "goredis.process_pipeline", opts...)
|
||||
defer span.Finish()
|
||||
|
||||
err := hook(ctx, cmds)
|
||||
@@ -106,7 +106,7 @@ func (h *tracingHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.
|
||||
}
|
||||
|
||||
func setSpanError(ctx context.Context, err error) {
|
||||
if err == nil || err == redis.Nil {
|
||||
if err == nil || err == goredis.Nil {
|
||||
return
|
||||
}
|
||||
if sp, ok := tracer.SpanFromContext(ctx); !ok && sp != nil {
|
||||
@@ -115,7 +115,7 @@ func setSpanError(ctx context.Context, err error) {
|
||||
}
|
||||
|
||||
func recordError(span tracer.Span, err error) {
|
||||
if err != nil && err != redis.Nil {
|
||||
if err != nil && err != goredis.Nil {
|
||||
span.SetStatus(tracer.SpanStatusError, err.Error())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user