Skip to content

Commit 69626a1

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 69626a1

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
type config struct {
1414
rawCommand bool
1515
serviceName string
16+
errCheck func(err error) bool
1617
}
1718

1819
// Option represents an option that can be used to create or wrap a client.
@@ -40,3 +41,11 @@ func WithServiceName(name string) Option {
4041
cfg.serviceName = name
4142
}
4243
}
44+
45+
// WithErrorCheck specifies a function fn which determines whether the passed
46+
// error should be marked as an error.
47+
func WithErrorCheck(fn func(err error) bool) Option {
48+
return func(cfg *config) {
49+
cfg.errCheck = fn
50+
}
51+
}

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

+19-13
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ func (c *client) Do(ctx context.Context, cmd valkey.Completed) valkey.ValkeyResu
8080
span, ctx := c.startSpan(ctx, processCommand(&cmd))
8181
resp := c.client.Do(ctx, cmd)
8282
setClientCacheTags(span, resp)
83-
span.Finish(tracer.WithError(resp.Error()))
83+
c.finishSpan(span, resp.Error())
8484
return resp
8585
}
8686

8787
func (c *client) DoMulti(ctx context.Context, multi ...valkey.Completed) []valkey.ValkeyResult {
8888
span, ctx := c.startSpan(ctx, processCommandMulti(multi))
8989
resp := c.client.DoMulti(ctx, multi...)
90-
c.finishSpan(span, firstError(resp))
90+
c.finishSpan(span, c.firstError(resp))
9191
return resp
9292
}
9393

@@ -109,7 +109,7 @@ func (c *client) DoCache(ctx context.Context, cmd valkey.Cacheable, ttl time.Dur
109109
func (c *client) DoMultiCache(ctx context.Context, multi ...valkey.CacheableTTL) []valkey.ValkeyResult {
110110
span, ctx := c.startSpan(ctx, processCommandMultiCache(multi))
111111
resp := c.client.DoMultiCache(ctx, multi...)
112-
c.finishSpan(span, firstError(resp))
112+
c.finishSpan(span, c.firstError(resp))
113113
return resp
114114
}
115115

@@ -207,12 +207,27 @@ func (c *client) startSpan(ctx context.Context, cmd command) (tracer.Span, conte
207207

208208
func (c *client) finishSpan(span tracer.Span, err error) {
209209
var opts []tracer.FinishOption
210-
if err != nil && !valkey.IsValkeyNil(err) {
210+
if c.isTracedError(err) {
211211
opts = append(opts, tracer.WithError(err))
212212
}
213213
span.Finish(opts...)
214214
}
215215

216+
func (c *client) firstError(s []valkey.ValkeyResult) error {
217+
for _, result := range s {
218+
if err := result.Error(); c.isTracedError(err) {
219+
return err
220+
}
221+
}
222+
return nil
223+
}
224+
225+
func (c *client) isTracedError(err error) bool {
226+
return err != nil &&
227+
!valkey.IsValkeyNil(err) &&
228+
(c.cfg.errCheck == nil || c.cfg.errCheck(err))
229+
}
230+
216231
type commander interface {
217232
Commands() []string
218233
}
@@ -267,15 +282,6 @@ func multiCommand(cmds []command) command {
267282
}
268283
}
269284

270-
func firstError(s []valkey.ValkeyResult) error {
271-
for _, result := range s {
272-
if err := result.Error(); err != nil && !valkey.IsValkeyNil(err) {
273-
return err
274-
}
275-
}
276-
return nil
277-
}
278-
279285
func setClientCacheTags(s tracer.Span, result valkey.ValkeyResult) {
280286
s.SetTag(ext.ValkeyClientCacheHit, result.IsCacheHit())
281287
s.SetTag(ext.ValkeyClientCacheTTL, result.CacheTTL())

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

+27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package valkey
66

77
import (
88
"context"
9+
"errors"
910
"fmt"
1011
"os"
1112
"testing"
@@ -260,6 +261,32 @@ func TestNewClient(t *testing.T) {
260261
},
261262
wantServiceName: "global-service",
262263
},
264+
{
265+
name: "Test SET command with canceled context and custom error check",
266+
opts: []Option{
267+
WithErrorCheck(func(err error) bool {
268+
return !errors.Is(err, context.Canceled)
269+
}),
270+
},
271+
runTest: func(t *testing.T, ctx context.Context, client valkey.Client) {
272+
ctx, cancel := context.WithCancel(ctx)
273+
cancel()
274+
require.Error(t, client.Do(ctx, client.B().Set().Key("test_key").Value("test_value").Build()).Error())
275+
},
276+
assertSpans: func(t *testing.T, spans []mocktracer.Span) {
277+
require.Len(t, spans, 1)
278+
279+
span := spans[0]
280+
assert.Equal(t, "SET", span.Tag(ext.ResourceName))
281+
assert.Nil(t, span.Tag(ext.ValkeyRawCommand))
282+
assert.Equal(t, false, span.Tag(ext.ValkeyClientCacheHit))
283+
assert.Less(t, span.Tag(ext.ValkeyClientCacheTTL), int64(0))
284+
assert.Less(t, span.Tag(ext.ValkeyClientCachePXAT), int64(0))
285+
assert.Less(t, span.Tag(ext.ValkeyClientCachePTTL), int64(0))
286+
assert.Nil(t, span.Tag(ext.Error))
287+
},
288+
wantServiceName: "global-service",
289+
},
263290
}
264291
for _, tt := range tests {
265292
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)