Skip to content

Commit 62cc004

Browse files
committed
Dont create OTEL trace of to be executed redis command, when it was part of the commandExclusions (#3479)
1 parent 617aca0 commit 62cc004

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

extra/redisotel/tracing.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strconv"
99
"strings"
1010

11+
"slices"
12+
1113
"go.opentelemetry.io/otel/attribute"
1214
"go.opentelemetry.io/otel/codes"
1315
semconv "go.opentelemetry.io/otel/semconv/v1.24.0"
@@ -102,6 +104,12 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
102104
func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
103105
return func(ctx context.Context, cmd redis.Cmder) error {
104106

107+
cmdString := rediscmd.CmdString(cmd)
108+
109+
if slices.Contains(th.conf.commandExclusions, cmdString) {
110+
return nil
111+
}
112+
105113
attrs := make([]attribute.KeyValue, 0, 8)
106114
if th.conf.callerEnabled {
107115
fn, file, line := funcFileLine("github.com/redis/go-redis")
@@ -113,7 +121,6 @@ func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
113121
}
114122

115123
if th.conf.dbStmtEnabled {
116-
cmdString := rediscmd.CmdString(cmd)
117124
attrs = append(attrs, semconv.DBStatement(cmdString))
118125
}
119126

extra/redisotel/tracing_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,83 @@ func TestTracingHook_ProcessPipelineHook_LongCommands(t *testing.T) {
415415
}
416416
}
417417

418+
func TestTracingHook_ProcessHook_LongCommand_WithExlusions(t *testing.T) {
419+
imsb := tracetest.NewInMemoryExporter()
420+
provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb))
421+
hook := newTracingHook(
422+
"redis://localhost:6379",
423+
WithTracerProvider(provider),
424+
)
425+
longValue := strings.Repeat("a", 102400)
426+
427+
tests := []struct {
428+
name string
429+
cmd redis.Cmder
430+
expected string
431+
}{
432+
{
433+
name: "short command",
434+
cmd: redis.NewCmd(context.Background(), "SET", "key", "value"),
435+
expected: "SET key value",
436+
},
437+
{
438+
name: "set command with long key",
439+
cmd: redis.NewCmd(context.Background(), "SET", longValue, "value"),
440+
expected: "SET " + longValue + " value",
441+
},
442+
{
443+
name: "set command with long value",
444+
cmd: redis.NewCmd(context.Background(), "SET", "key", longValue),
445+
expected: "SET key " + longValue,
446+
},
447+
{
448+
name: "set command with long key and value",
449+
cmd: redis.NewCmd(context.Background(), "SET", longValue, longValue),
450+
expected: "SET " + longValue + " " + longValue,
451+
},
452+
{
453+
name: "short command with many arguments",
454+
cmd: redis.NewCmd(context.Background(), "MSET", "key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"),
455+
expected: "MSET key1 value1 key2 value2 key3 value3 key4 value4 key5 value5",
456+
},
457+
{
458+
name: "long command",
459+
cmd: redis.NewCmd(context.Background(), longValue, "key", "value"),
460+
expected: longValue + " key value",
461+
},
462+
}
463+
464+
for _, tt := range tests {
465+
t.Run(tt.name, func(t *testing.T) {
466+
defer imsb.Reset()
467+
468+
processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error {
469+
return nil
470+
})
471+
472+
if err := processHook(context.Background(), tt.cmd); err != nil {
473+
t.Fatal(err)
474+
}
475+
476+
assertEqual(t, 1, len(imsb.GetSpans()))
477+
478+
spanData := imsb.GetSpans()[0]
479+
480+
var dbStatement string
481+
for _, attr := range spanData.Attributes {
482+
if attr.Key == semconv.DBStatementKey {
483+
dbStatement = attr.Value.AsString()
484+
break
485+
}
486+
}
487+
488+
if dbStatement != tt.expected {
489+
t.Errorf("Expected DB statement: %q\nGot: %q", tt.expected, dbStatement)
490+
}
491+
})
492+
}
493+
}
494+
418495
func assertEqual(t *testing.T, expected, actual interface{}) {
419496
t.Helper()
420497
if expected != actual {

0 commit comments

Comments
 (0)