Skip to content

Commit d3df060

Browse files
committed
Merge branch 'otel-ignore-raw-cmd'
2 parents aaba70a + 3129e09 commit d3df060

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

extra/redisotel/redisotel.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const (
1818
)
1919

2020
type TracingHook struct {
21-
tracer trace.Tracer
22-
attrs []attribute.KeyValue
21+
tracer trace.Tracer
22+
attrs []attribute.KeyValue
23+
dbStmtEnabled bool
2324
}
2425

2526
func NewTracingHook(opts ...Option) *TracingHook {
@@ -28,6 +29,7 @@ func NewTracingHook(opts ...Option) *TracingHook {
2829
attrs: []attribute.KeyValue{
2930
semconv.DBSystemRedis,
3031
},
32+
dbStmtEnabled: true,
3133
}
3234
for _, opt := range opts {
3335
opt.apply(cfg)
@@ -37,7 +39,7 @@ func NewTracingHook(opts ...Option) *TracingHook {
3739
defaultTracerName,
3840
trace.WithInstrumentationVersion("semver:"+redis.Version()),
3941
)
40-
return &TracingHook{tracer: tracer, attrs: cfg.attrs}
42+
return &TracingHook{tracer: tracer, attrs: cfg.attrs, dbStmtEnabled: cfg.dbStmtEnabled}
4143
}
4244

4345
func (th *TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
@@ -48,9 +50,10 @@ func (th *TracingHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (cont
4850
opts := []trace.SpanStartOption{
4951
trace.WithSpanKind(trace.SpanKindClient),
5052
trace.WithAttributes(th.attrs...),
51-
trace.WithAttributes(
52-
semconv.DBStatementKey.String(rediscmd.CmdString(cmd)),
53-
),
53+
}
54+
55+
if th.dbStmtEnabled {
56+
opts = append(opts, trace.WithAttributes(semconv.DBStatementKey.String(rediscmd.CmdString(cmd))))
5457
}
5558

5659
ctx, _ = th.tracer.Start(ctx, cmd.FullName(), opts...)
@@ -67,22 +70,26 @@ func (th *TracingHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error
6770
return nil
6871
}
6972

70-
func (th *TracingHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
73+
func (th *TracingHook) BeforeProcessPipeline(
74+
ctx context.Context, cmds []redis.Cmder,
75+
) (context.Context, error) {
7176
if !trace.SpanFromContext(ctx).IsRecording() {
7277
return ctx, nil
7378
}
7479

75-
summary, cmdsString := rediscmd.CmdsString(cmds)
76-
7780
opts := []trace.SpanStartOption{
7881
trace.WithSpanKind(trace.SpanKindClient),
7982
trace.WithAttributes(th.attrs...),
8083
trace.WithAttributes(
81-
semconv.DBStatementKey.String(cmdsString),
8284
attribute.Int("db.redis.num_cmd", len(cmds)),
8385
),
8486
}
8587

88+
summary, cmdsString := rediscmd.CmdsString(cmds)
89+
if th.dbStmtEnabled {
90+
opts = append(opts, trace.WithAttributes(semconv.DBStatementKey.String(cmdsString)))
91+
}
92+
8693
ctx, _ = th.tracer.Start(ctx, "pipeline "+summary, opts...)
8794

8895
return ctx, nil
@@ -105,8 +112,9 @@ func recordError(ctx context.Context, span trace.Span, err error) {
105112
}
106113

107114
type config struct {
108-
tp trace.TracerProvider
109-
attrs []attribute.KeyValue
115+
tp trace.TracerProvider
116+
attrs []attribute.KeyValue
117+
dbStmtEnabled bool
110118
}
111119

112120
// Option specifies instrumentation configuration options.
@@ -136,3 +144,10 @@ func WithAttributes(attrs ...attribute.KeyValue) Option {
136144
cfg.attrs = append(cfg.attrs, attrs...)
137145
})
138146
}
147+
148+
// WithDBStatement tells the tracing hook not to log raw redis commands.
149+
func WithDBStatement(on bool) Option {
150+
return optionFunc(func(cfg *config) {
151+
cfg.dbStmtEnabled = on
152+
})
153+
}

extra/redisotel/redisotel_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,30 @@ func TestNewWithAttributes(t *testing.T) {
6868
t.Fatalf("expected attrs[2] to be semconv.DBStatementKey.String(\"ping\"), got: %v", attrs[2])
6969
}
7070
}
71+
72+
func TestWithDBStatement(t *testing.T) {
73+
provider := sdktrace.NewTracerProvider()
74+
hook := redisotel.NewTracingHook(
75+
redisotel.WithTracerProvider(provider),
76+
redisotel.WithDBStatement(false),
77+
)
78+
ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test")
79+
cmd := redis.NewCmd(ctx, "ping")
80+
defer span.End()
81+
82+
ctx, err := hook.BeforeProcess(ctx, cmd)
83+
if err != nil {
84+
t.Fatal(err)
85+
}
86+
err = hook.AfterProcess(ctx, cmd)
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes()
92+
for _, attr := range attrs {
93+
if attr.Key == semconv.DBStatementKey {
94+
t.Fatal("Attribute with db statement should not exist")
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)