Add RecordError

This commit is contained in:
Vladimir Mihailenco 2020-07-09 10:39:46 +03:00
parent dd5e03f960
commit 1831363467
3 changed files with 34 additions and 23 deletions

View File

@ -622,7 +622,7 @@ func (c *clusterStateHolder) LazyReload(ctx context.Context) {
if err != nil { if err != nil {
return return
} }
time.Sleep(250 * time.Millisecond) time.Sleep(200 * time.Millisecond)
}() }()
} }

View File

@ -63,11 +63,13 @@ 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, "with_reader", func(ctx context.Context) error { return internal.WithSpan(ctx, "with_reader", func(ctx context.Context) error {
err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)) if err := cn.netConn.SetReadDeadline(cn.deadline(ctx, timeout)); err != nil {
if err != nil { return internal.RecordError(ctx, err)
return err
} }
return fn(cn.rd) if err := fn(cn.rd); err != nil {
return internal.RecordError(ctx, err)
}
return nil
}) })
} }
@ -75,21 +77,23 @@ 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, "with_writer", func(ctx context.Context) error { return internal.WithSpan(ctx, "with_writer", func(ctx context.Context) error {
err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)) if err := cn.netConn.SetWriteDeadline(cn.deadline(ctx, timeout)); err != nil {
if err != nil { return internal.RecordError(ctx, err)
return err
} }
if cn.bw.Buffered() > 0 { if cn.bw.Buffered() > 0 {
cn.bw.Reset(cn.netConn) cn.bw.Reset(cn.netConn)
} }
err = fn(cn.wr) if err := fn(cn.wr); err != nil {
if err != nil { return internal.RecordError(ctx, err)
return err
} }
return cn.bw.Flush() if err := cn.bw.Flush(); err != nil {
return internal.RecordError(ctx, err)
}
return nil
}) })
} }

View File

@ -62,17 +62,6 @@ func Unwrap(err error) error {
return u.Unwrap() return u.Unwrap()
} }
func WithSpan(ctx context.Context, name string, fn func(context.Context) error) error {
if !trace.SpanFromContext(ctx).IsRecording() {
return fn(ctx)
}
ctx, span := global.Tracer("go-redis").Start(ctx, name)
defer span.End()
return fn(ctx)
}
func AppendArg(b []byte, v interface{}) []byte { func AppendArg(b []byte, v interface{}) []byte {
switch v := v.(type) { switch v := v.(type) {
case nil: case nil:
@ -143,3 +132,21 @@ func appendRune(b []byte, r rune) []byte {
return b return b
} }
//------------------------------------------------------------------------------
func WithSpan(ctx context.Context, name string, fn func(context.Context) error) error {
if !trace.SpanFromContext(ctx).IsRecording() {
return fn(ctx)
}
ctx, span := global.Tracer("github.com/go-redis/redis").Start(ctx, name)
defer span.End()
return fn(ctx)
}
func RecordError(ctx context.Context, err error) error {
trace.SpanFromContext(ctx).RecordError(ctx, err)
return err
}