@@ -18,8 +18,9 @@ const (
1818)
1919
2020type TracingHook struct {
21- tracer trace.Tracer
22- attrs []attribute.KeyValue
21+ tracer trace.Tracer
22+ attrs []attribute.KeyValue
23+ dbStmtEnabled bool
2324}
2425
2526func 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
4345func (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
107114type 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+ }
0 commit comments