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 (
"context"
"net"
"go.opencensus.io/trace"
@ -17,29 +18,54 @@ func NewTracingHook() *TracingHook {
return new(TracingHook)
}
func (TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
ctx, span := trace.StartSpan(ctx, cmd.FullName())
span.AddAttributes(trace.StringAttribute("db.system", "redis"),
trace.StringAttribute("redis.cmd", rediscmd.CmdString(cmd)))
func (TracingHook) DialHook(next redis.DialHook) redis.DialHook {
return func(ctx context.Context, network, addr string) (net.Conn, error) {
ctx, span := trace.StartSpan(ctx, "dial")
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 {
span := trace.FromContext(ctx)
if err := cmd.Err(); err != nil {
recordErrorOnOCSpan(ctx, span, err)
conn, err := next(ctx, network, addr)
if err != nil {
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) {
return ctx, nil
func (TracingHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
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 {
return nil
func (TracingHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook {
return next
}
func recordErrorOnOCSpan(ctx context.Context, span *trace.Span, err error) {