fix: implement `redis.Hook` (v9) for `rediscensus.TracingHook` (#2367)

Fixes #2266
This commit is contained in:
Daniel Cormier 2023-01-26 01:23:14 -05:00 committed by GitHub
parent 699887efc8
commit 805bfc2c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 16 deletions

View File

@ -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) {