mirror of https://github.com/go-redis/redis.git
fix: implement `redis.Hook` (v9) for `rediscensus.TracingHook` (#2367)
Fixes #2266
This commit is contained in:
parent
699887efc8
commit
805bfc2c60
|
@ -2,6 +2,7 @@ package rediscensus
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net"
|
||||||
|
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
|
|
||||||
|
@ -17,29 +18,54 @@ func NewTracingHook() *TracingHook {
|
||||||
return new(TracingHook)
|
return new(TracingHook)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
|
func (TracingHook) DialHook(next redis.DialHook) redis.DialHook {
|
||||||
ctx, span := trace.StartSpan(ctx, cmd.FullName())
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
span.AddAttributes(trace.StringAttribute("db.system", "redis"),
|
ctx, span := trace.StartSpan(ctx, "dial")
|
||||||
trace.StringAttribute("redis.cmd", rediscmd.CmdString(cmd)))
|
defer span.End()
|
||||||
|
|
||||||
return ctx, nil
|
span.AddAttributes(
|
||||||
}
|
trace.StringAttribute("db.system", "redis"),
|
||||||
|
trace.StringAttribute("network", network),
|
||||||
|
trace.StringAttribute("addr", addr),
|
||||||
|
)
|
||||||
|
|
||||||
func (TracingHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
|
conn, err := next(ctx, network, addr)
|
||||||
span := trace.FromContext(ctx)
|
if err != nil {
|
||||||
if err := cmd.Err(); err != nil {
|
recordErrorOnOCSpan(ctx, span, err)
|
||||||
recordErrorOnOCSpan(ctx, span, err)
|
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
}
|
}
|
||||||
span.End()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (TracingHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
|
func (TracingHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
|
||||||
return ctx, nil
|
return func(ctx context.Context, cmd redis.Cmder) error {
|
||||||
|
ctx, span := trace.StartSpan(ctx, cmd.FullName())
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
|
span.AddAttributes(
|
||||||
|
trace.StringAttribute("db.system", "redis"),
|
||||||
|
trace.StringAttribute("redis.cmd", rediscmd.CmdString(cmd)),
|
||||||
|
)
|
||||||
|
|
||||||
|
err := next(ctx, cmd)
|
||||||
|
if err != nil {
|
||||||
|
recordErrorOnOCSpan(ctx, span, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = cmd.Err(); err != nil {
|
||||||
|
recordErrorOnOCSpan(ctx, span, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (TracingHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
|
func (TracingHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
||||||
return nil
|
return next
|
||||||
}
|
}
|
||||||
|
|
||||||
func recordErrorOnOCSpan(ctx context.Context, span *trace.Span, err error) {
|
func recordErrorOnOCSpan(ctx context.Context, span *trace.Span, err error) {
|
||||||
|
|
Loading…
Reference in New Issue