go-redis icon indicating copy to clipboard operation
go-redis copied to clipboard

feat: extract dialer to `DefaultDialer` to allow wrapping

Open nkcmr opened this issue 3 years ago • 0 comments

Allowing the default dialing function to be wrapped allows for library users to let the library continue to own the logic for dialing and let users wrap the function for more observability.

My use case is to override Options.Dialer and add Jaeger tracing to gain insight into the cost of new connections on a latency-sensitive API.

defDialer := redis.DefaultDialer(opts)
opts.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) {
    span, ctx := opentracing.StartSpanFromContext(ctx, "cache-repo-redis: new redis connection")
    defer span.Finish()

    return defDialer(ctx, network, addr)
}

Without this, I end up needing to copy-paste the code from the internal code, which is less-than-ideal since I don't want to own the maintenance of this logic.

nkcmr avatar Jan 14 '22 21:01 nkcmr