From 24befd7182123e3d6361da40f1eb38489c17b63e Mon Sep 17 00:00:00 2001 From: Allan Guigou <34221163+AllanGuigou@users.noreply.github.com> Date: Tue, 15 Jul 2025 14:57:24 +0000 Subject: [PATCH] Add tracing hooks for existing connections Previously the redis/go-redis library would call newTracingHook with no connString as default tracing, but that was recently removed in 9.8.0 which has caused our clients to lose oteltracing if connections are created before tracing can be instrumented. --- extra/redisotel/tracing.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/extra/redisotel/tracing.go b/extra/redisotel/tracing.go index 40df5a202..95f7031f2 100644 --- a/extra/redisotel/tracing.go +++ b/extra/redisotel/tracing.go @@ -21,7 +21,7 @@ const ( instrumName = "github.com/redis/go-redis/extra/redisotel" ) -func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error { +func InstrumentTracing(ctx context.Context, rdb redis.UniversalClient, opts ...TracingOption) error { switch rdb := rdb.(type) { case *redis.Client: opt := rdb.Options() @@ -36,6 +36,14 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error { connString := formatDBConnString(opt.Network, opt.Addr) rdb.AddHook(newTracingHook(connString, opts...)) }) + + rdb.ForEachShard(ctx, func(ctx context.Context, rdb *redis.Client) error { + opt := rdb.Options() + opts = addServerAttributes(opts, opt.Addr) + connString := formatDBConnString(opt.Network, opt.Addr) + rdb.AddHook(newTracingHook(connString, opts...)) + return nil + }) return nil case *redis.Ring: rdb.OnNewNode(func(rdb *redis.Client) { @@ -44,6 +52,14 @@ func InstrumentTracing(rdb redis.UniversalClient, opts ...TracingOption) error { connString := formatDBConnString(opt.Network, opt.Addr) rdb.AddHook(newTracingHook(connString, opts...)) }) + + rdb.ForEachShard(ctx, func(ctx context.Context, rdb *redis.Client) error { + opt := rdb.Options() + opts = addServerAttributes(opts, opt.Addr) + connString := formatDBConnString(opt.Network, opt.Addr) + rdb.AddHook(newTracingHook(connString, opts...)) + return nil + }) return nil default: return fmt.Errorf("redisotel: %T not supported", rdb)