Skip to content

Commit 28ab5c9

Browse files
committed
Add support for filtering commands when tracing
Signed-off-by: Jason Parraga <[email protected]>
1 parent 0dcfeef commit 28ab5c9

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

extra/redisotel/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redisotel
22

33
import (
4+
"github.com/redis/go-redis/v9"
45
"go.opentelemetry.io/otel"
56
"go.opentelemetry.io/otel/attribute"
67
"go.opentelemetry.io/otel/metric"
@@ -21,6 +22,7 @@ type config struct {
2122

2223
dbStmtEnabled bool
2324
callerEnabled bool
25+
filter func(cmd redis.Cmder) bool
2426

2527
// Metrics options.
2628

@@ -124,6 +126,14 @@ func WithCallerEnabled(on bool) TracingOption {
124126
})
125127
}
126128

129+
// WithCommandFilter allows filtering of commands when tracing to omit commands that may have sensitive details like
130+
// passwords.
131+
func WithCommandFilter(filter func(cmd redis.Cmder) bool) TracingOption {
132+
return tracingOption(func(conf *config) {
133+
conf.filter = filter
134+
})
135+
}
136+
127137
//------------------------------------------------------------------------------
128138

129139
type MetricsOption interface {

extra/redisotel/tracing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ func (th *tracingHook) DialHook(hook redis.DialHook) redis.DialHook {
102102
func (th *tracingHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
103103
return func(ctx context.Context, cmd redis.Cmder) error {
104104

105+
// Check if the command should be filtered out
106+
if th.conf.filter != nil && th.conf.filter(cmd) {
107+
// If so, just call the next hook
108+
return hook(ctx, cmd)
109+
}
110+
105111
attrs := make([]attribute.KeyValue, 0, 8)
106112
if th.conf.callerEnabled {
107113
fn, file, line := funcFileLine("github.com/redis/go-redis")

extra/redisotel/tracing_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,63 @@ func TestWithoutCaller(t *testing.T) {
9595
}
9696
}
9797

98+
func TestWithCommandFilter(t *testing.T) {
99+
100+
t.Run("filter out ping command", func(t *testing.T) {
101+
provider := sdktrace.NewTracerProvider()
102+
hook := newTracingHook(
103+
"",
104+
WithTracerProvider(provider),
105+
WithCommandFilter(func(cmd redis.Cmder) bool {
106+
return cmd.Name() == "ping"
107+
}),
108+
)
109+
ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test")
110+
cmd := redis.NewCmd(ctx, "ping")
111+
defer span.End()
112+
113+
processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error {
114+
innerSpan := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan)
115+
if innerSpan.Name() != "redis-test" || innerSpan.Name() == "ping" {
116+
t.Fatalf("ping command should not be traced")
117+
}
118+
119+
return nil
120+
})
121+
err := processHook(ctx, cmd)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
})
126+
127+
t.Run("do not filter ping command", func(t *testing.T) {
128+
provider := sdktrace.NewTracerProvider()
129+
hook := newTracingHook(
130+
"",
131+
WithTracerProvider(provider),
132+
WithCommandFilter(func(cmd redis.Cmder) bool {
133+
return false // never filter
134+
}),
135+
)
136+
ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test")
137+
cmd := redis.NewCmd(ctx, "ping")
138+
defer span.End()
139+
140+
processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error {
141+
innerSpan := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan)
142+
if innerSpan.Name() != "ping" {
143+
t.Fatalf("ping command should be traced")
144+
}
145+
146+
return nil
147+
})
148+
err := processHook(ctx, cmd)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
})
153+
}
154+
98155
func TestTracingHook_DialHook(t *testing.T) {
99156
imsb := tracetest.NewInMemoryExporter()
100157
provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb))

0 commit comments

Comments
 (0)