Skip to content

Commit 880a392

Browse files
committed
fix(valkey): Make error checking configurable
All tracer.WithError calls first run through a configurable error check function.
1 parent e459938 commit 880a392

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Diff for: contrib/valkey-go/option.go

+15
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
package valkey
77

88
import (
9+
"github.com/valkey-io/valkey-go"
910
"gopkg.in/DataDog/dd-trace-go.v1/internal"
1011
"gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema"
1112
)
1213

1314
type config struct {
1415
rawCommand bool
1516
serviceName string
17+
errCheck func(err error) bool
18+
}
19+
20+
var defaultErrCheck = func(err error) bool {
21+
return err != nil && !valkey.IsValkeyNil(err)
1622
}
1723

1824
// Option represents an option that can be used to create or wrap a client.
@@ -23,6 +29,7 @@ func defaultConfig() *config {
2329
// Do not include the raw command by default since it could contain sensitive data.
2430
rawCommand: internal.BoolEnv("DD_TRACE_VALKEY_RAW_COMMAND", false),
2531
serviceName: namingschema.ServiceName(defaultServiceName),
32+
errCheck: defaultErrCheck,
2633
}
2734
}
2835

@@ -40,3 +47,11 @@ func WithServiceName(name string) Option {
4047
cfg.serviceName = name
4148
}
4249
}
50+
51+
// WithErrorCheck specifies a function fn which determines whether the passed
52+
// error should be marked as an error.
53+
func WithErrorCheck(fn func(err error) bool) Option {
54+
return func(cfg *config) {
55+
cfg.errCheck = fn
56+
}
57+
}

Diff for: contrib/valkey-go/valkey.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func NewClient(clientOption valkey.ClientOption, opts ...Option) (valkey.Client,
6060
for _, fn := range opts {
6161
fn(cfg)
6262
}
63+
if cfg.errCheck == nil {
64+
cfg.errCheck = defaultErrCheck
65+
}
6366
tClient := &client{
6467
client: valkeyClient,
6568
cfg: cfg,
@@ -80,14 +83,14 @@ func (c *client) Do(ctx context.Context, cmd valkey.Completed) valkey.ValkeyResu
8083
span, ctx := c.startSpan(ctx, processCommand(&cmd))
8184
resp := c.client.Do(ctx, cmd)
8285
setClientCacheTags(span, resp)
83-
span.Finish(tracer.WithError(resp.Error()))
86+
c.finishSpan(span, resp.Error())
8487
return resp
8588
}
8689

8790
func (c *client) DoMulti(ctx context.Context, multi ...valkey.Completed) []valkey.ValkeyResult {
8891
span, ctx := c.startSpan(ctx, processCommandMulti(multi))
8992
resp := c.client.DoMulti(ctx, multi...)
90-
c.finishSpan(span, firstError(resp))
93+
c.finishSpan(span, firstError(c.cfg.errCheck, resp))
9194
return resp
9295
}
9396

@@ -109,7 +112,7 @@ func (c *client) DoCache(ctx context.Context, cmd valkey.Cacheable, ttl time.Dur
109112
func (c *client) DoMultiCache(ctx context.Context, multi ...valkey.CacheableTTL) []valkey.ValkeyResult {
110113
span, ctx := c.startSpan(ctx, processCommandMultiCache(multi))
111114
resp := c.client.DoMultiCache(ctx, multi...)
112-
c.finishSpan(span, firstError(resp))
115+
c.finishSpan(span, firstError(c.cfg.errCheck, resp))
113116
return resp
114117
}
115118

@@ -207,7 +210,7 @@ func (c *client) startSpan(ctx context.Context, cmd command) (tracer.Span, conte
207210

208211
func (c *client) finishSpan(span tracer.Span, err error) {
209212
var opts []tracer.FinishOption
210-
if err != nil && !valkey.IsValkeyNil(err) {
213+
if c.cfg.errCheck(err) {
211214
opts = append(opts, tracer.WithError(err))
212215
}
213216
span.Finish(opts...)
@@ -267,9 +270,9 @@ func multiCommand(cmds []command) command {
267270
}
268271
}
269272

270-
func firstError(s []valkey.ValkeyResult) error {
273+
func firstError(errCheck func(error) bool, s []valkey.ValkeyResult) error {
271274
for _, result := range s {
272-
if err := result.Error(); err != nil && !valkey.IsValkeyNil(err) {
275+
if err := result.Error(); errCheck(err) {
273276
return err
274277
}
275278
}

0 commit comments

Comments
 (0)