mirror of https://github.com/go-redis/redis.git
Optimize Otel instrumentation
This commit is contained in:
parent
7c5bbc37bd
commit
1b77706c0c
|
@ -9,7 +9,6 @@ import (
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8/internal"
|
"github.com/go-redis/redis/v8/internal"
|
||||||
"github.com/go-redis/redis/v8/internal/proto"
|
"github.com/go-redis/redis/v8/internal/proto"
|
||||||
"go.opentelemetry.io/otel/trace"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var noDeadline = time.Time{}
|
var noDeadline = time.Time{}
|
||||||
|
@ -66,7 +65,9 @@ func (cn *Conn) RemoteAddr() net.Addr {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(rd *proto.Reader) error) error {
|
||||||
return internal.WithSpan(ctx, "redis.with_reader", func(ctx context.Context, span trace.Span) error {
|
ctx, span := internal.StartSpan(ctx, "redis.with_reader")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
|
if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
|
||||||
return internal.RecordError(ctx, span, err)
|
return internal.RecordError(ctx, span, err)
|
||||||
}
|
}
|
||||||
|
@ -74,13 +75,14 @@ func (cn *Conn) WithReader(ctx context.Context, timeout time.Duration, fn func(r
|
||||||
return internal.RecordError(ctx, span, err)
|
return internal.RecordError(ctx, span, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) WithWriter(
|
func (cn *Conn) WithWriter(
|
||||||
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
|
ctx context.Context, timeout time.Duration, fn func(wr *proto.Writer) error,
|
||||||
) error {
|
) error {
|
||||||
return internal.WithSpan(ctx, "redis.with_writer", func(ctx context.Context, span trace.Span) error {
|
ctx, span := internal.StartSpan(ctx, "redis.with_writer")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
|
if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
|
||||||
return internal.RecordError(ctx, span, err)
|
return internal.RecordError(ctx, span, err)
|
||||||
}
|
}
|
||||||
|
@ -100,7 +102,6 @@ func (cn *Conn) WithWriter(
|
||||||
internal.WritesCounter.Add(ctx, 1)
|
internal.WritesCounter.Add(ctx, 1)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cn *Conn) Close() error {
|
func (cn *Conn) Close() error {
|
||||||
|
|
|
@ -11,7 +11,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Sleep(ctx context.Context, dur time.Duration) error {
|
func Sleep(ctx context.Context, dur time.Duration) error {
|
||||||
return WithSpan(ctx, "time.Sleep", func(ctx context.Context, span trace.Span) error {
|
_, span := StartSpan(ctx, "time.Sleep")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
t := time.NewTimer(dur)
|
t := time.NewTimer(dur)
|
||||||
defer t.Stop()
|
defer t.Stop()
|
||||||
|
|
||||||
|
@ -21,7 +23,6 @@ func Sleep(ctx context.Context, dur time.Duration) error {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ToLower(s string) string {
|
func ToLower(s string) string {
|
||||||
|
@ -54,15 +55,11 @@ func isLower(s string) bool {
|
||||||
|
|
||||||
var tracer = otel.Tracer("github.com/go-redis/redis")
|
var tracer = otel.Tracer("github.com/go-redis/redis")
|
||||||
|
|
||||||
func WithSpan(ctx context.Context, name string, fn func(context.Context, trace.Span) error) error {
|
func StartSpan(ctx context.Context, name string) (context.Context, trace.Span) {
|
||||||
if span := trace.SpanFromContext(ctx); !span.IsRecording() {
|
if span := trace.SpanFromContext(ctx); !span.IsRecording() {
|
||||||
return fn(ctx, span)
|
return ctx, span
|
||||||
}
|
}
|
||||||
|
return tracer.Start(ctx, name)
|
||||||
ctx, span := tracer.Start(ctx, name)
|
|
||||||
defer span.End()
|
|
||||||
|
|
||||||
return fn(ctx, span)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RecordError(ctx context.Context, span trace.Span, err error) error {
|
func RecordError(ctx context.Context, span trace.Span, err error) error {
|
||||||
|
|
22
options.go
22
options.go
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/go-redis/redis/v8/internal"
|
"github.com/go-redis/redis/v8/internal"
|
||||||
"github.com/go-redis/redis/v8/internal/pool"
|
"github.com/go-redis/redis/v8/internal/pool"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/trace"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Limiter is the interface of a rate limiter or a circuit breaker.
|
// Limiter is the interface of a rate limiter or a circuit breaker.
|
||||||
|
@ -292,20 +291,21 @@ func getUserPassword(u *url.URL) (string, string) {
|
||||||
func newConnPool(opt *Options) *pool.ConnPool {
|
func newConnPool(opt *Options) *pool.ConnPool {
|
||||||
return pool.NewConnPool(&pool.Options{
|
return pool.NewConnPool(&pool.Options{
|
||||||
Dialer: func(ctx context.Context) (net.Conn, error) {
|
Dialer: func(ctx context.Context) (net.Conn, error) {
|
||||||
var conn net.Conn
|
ctx, span := internal.StartSpan(ctx, "redis.dial")
|
||||||
err := internal.WithSpan(ctx, "redis.dial", func(ctx context.Context, span trace.Span) error {
|
defer span.End()
|
||||||
|
|
||||||
|
if span.IsRecording() {
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("db.connection_string", opt.Addr),
|
attribute.String("db.connection_string", opt.Addr),
|
||||||
)
|
)
|
||||||
|
|
||||||
var err error
|
|
||||||
conn, err = opt.Dialer(ctx, opt.Network, opt.Addr)
|
|
||||||
if err != nil {
|
|
||||||
_ = internal.RecordError(ctx, span, err)
|
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
})
|
cn, err := opt.Dialer(ctx, opt.Network, opt.Addr)
|
||||||
return conn, err
|
if err != nil {
|
||||||
|
return nil, internal.RecordError(ctx, span, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return cn, nil
|
||||||
},
|
},
|
||||||
PoolSize: opt.PoolSize,
|
PoolSize: opt.PoolSize,
|
||||||
MinIdleConns: opt.MinIdleConns,
|
MinIdleConns: opt.MinIdleConns,
|
||||||
|
|
43
redis.go
43
redis.go
|
@ -11,7 +11,6 @@ import (
|
||||||
"github.com/go-redis/redis/v8/internal/pool"
|
"github.com/go-redis/redis/v8/internal/pool"
|
||||||
"github.com/go-redis/redis/v8/internal/proto"
|
"github.com/go-redis/redis/v8/internal/proto"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/trace"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Nil reply returned by Redis when key does not exist.
|
// Nil reply returned by Redis when key does not exist.
|
||||||
|
@ -214,10 +213,7 @@ func (c *baseClient) _getConn(ctx context.Context) (*pool.Conn, error) {
|
||||||
return cn, nil
|
return cn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = internal.WithSpan(ctx, "redis.init_conn", func(ctx context.Context, span trace.Span) error {
|
if err := c.initConn(ctx, cn); err != nil {
|
||||||
return c.initConn(ctx, cn)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
c.connPool.Remove(ctx, cn, err)
|
c.connPool.Remove(ctx, cn, err)
|
||||||
if err := errors.Unwrap(err); err != nil {
|
if err := errors.Unwrap(err); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -241,6 +237,9 @@ func (c *baseClient) initConn(ctx context.Context, cn *pool.Conn) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx, span := internal.StartSpan(ctx, "redis.init_conn")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
connPool := pool.NewSingleConnPool(c.connPool, cn)
|
connPool := pool.NewSingleConnPool(c.connPool, cn)
|
||||||
conn := newConn(ctx, c.opt, connPool)
|
conn := newConn(ctx, c.opt, connPool)
|
||||||
|
|
||||||
|
@ -288,7 +287,9 @@ func (c *baseClient) releaseConn(ctx context.Context, cn *pool.Conn, err error)
|
||||||
func (c *baseClient) withConn(
|
func (c *baseClient) withConn(
|
||||||
ctx context.Context, fn func(context.Context, *pool.Conn) error,
|
ctx context.Context, fn func(context.Context, *pool.Conn) error,
|
||||||
) error {
|
) error {
|
||||||
return internal.WithSpan(ctx, "redis.with_conn", func(ctx context.Context, span trace.Span) error {
|
ctx, span := internal.StartSpan(ctx, "redis.with_conn")
|
||||||
|
defer span.End()
|
||||||
|
|
||||||
cn, err := c.getConn(ctx)
|
cn, err := c.getConn(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -324,7 +325,6 @@ func (c *baseClient) withConn(
|
||||||
case err = <-errc:
|
case err = <-errc:
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *baseClient) process(ctx context.Context, cmd Cmder) error {
|
func (c *baseClient) process(ctx context.Context, cmd Cmder) error {
|
||||||
|
@ -332,11 +332,20 @@ func (c *baseClient) process(ctx context.Context, cmd Cmder) error {
|
||||||
for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
|
for attempt := 0; attempt <= c.opt.MaxRetries; attempt++ {
|
||||||
attempt := attempt
|
attempt := attempt
|
||||||
|
|
||||||
var retry bool
|
retry, err := c._process(ctx, cmd, attempt)
|
||||||
err := internal.WithSpan(ctx, "redis.process", func(ctx context.Context, span trace.Span) error {
|
if err == nil || !retry {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
lastErr = err
|
||||||
|
}
|
||||||
|
return lastErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *baseClient) _process(ctx context.Context, cmd Cmder, attempt int) (bool, error) {
|
||||||
if attempt > 0 {
|
if attempt > 0 {
|
||||||
if err := internal.Sleep(ctx, c.retryBackoff(attempt)); err != nil {
|
if err := internal.Sleep(ctx, c.retryBackoff(attempt)); err != nil {
|
||||||
return err
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,17 +369,11 @@ func (c *baseClient) process(ctx context.Context, cmd Cmder) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return false, nil
|
||||||
}
|
}
|
||||||
retry = shouldRetry(err, atomic.LoadUint32(&retryTimeout) == 1)
|
|
||||||
return err
|
retry := shouldRetry(err, atomic.LoadUint32(&retryTimeout) == 1)
|
||||||
})
|
return retry, err
|
||||||
if err == nil || !retry {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
lastErr = err
|
|
||||||
}
|
|
||||||
return lastErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *baseClient) retryBackoff(attempt int) time.Duration {
|
func (c *baseClient) retryBackoff(attempt int) time.Duration {
|
||||||
|
|
Loading…
Reference in New Issue