@@ -18,8 +18,9 @@ const (
18
18
)
19
19
20
20
type TracingHook struct {
21
- tracer trace.Tracer
22
- attrs []attribute.KeyValue
21
+ tracer trace.Tracer
22
+ attrs []attribute.KeyValue
23
+ dbStmtEnabled bool
23
24
}
24
25
25
26
func NewTracingHook (opts ... Option ) * TracingHook {
@@ -28,6 +29,7 @@ func NewTracingHook(opts ...Option) *TracingHook {
28
29
attrs : []attribute.KeyValue {
29
30
semconv .DBSystemRedis ,
30
31
},
32
+ dbStmtEnabled : true ,
31
33
}
32
34
for _ , opt := range opts {
33
35
opt .apply (cfg )
@@ -37,7 +39,7 @@ func NewTracingHook(opts ...Option) *TracingHook {
37
39
defaultTracerName ,
38
40
trace .WithInstrumentationVersion ("semver:" + redis .Version ()),
39
41
)
40
- return & TracingHook {tracer : tracer , attrs : cfg .attrs }
42
+ return & TracingHook {tracer : tracer , attrs : cfg .attrs , dbStmtEnabled : cfg . dbStmtEnabled }
41
43
}
42
44
43
45
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
48
50
opts := []trace.SpanStartOption {
49
51
trace .WithSpanKind (trace .SpanKindClient ),
50
52
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 ))))
54
57
}
55
58
56
59
ctx , _ = th .tracer .Start (ctx , cmd .FullName (), opts ... )
@@ -67,22 +70,26 @@ func (th *TracingHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error
67
70
return nil
68
71
}
69
72
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 ) {
71
76
if ! trace .SpanFromContext (ctx ).IsRecording () {
72
77
return ctx , nil
73
78
}
74
79
75
- summary , cmdsString := rediscmd .CmdsString (cmds )
76
-
77
80
opts := []trace.SpanStartOption {
78
81
trace .WithSpanKind (trace .SpanKindClient ),
79
82
trace .WithAttributes (th .attrs ... ),
80
83
trace .WithAttributes (
81
- semconv .DBStatementKey .String (cmdsString ),
82
84
attribute .Int ("db.redis.num_cmd" , len (cmds )),
83
85
),
84
86
}
85
87
88
+ summary , cmdsString := rediscmd .CmdsString (cmds )
89
+ if th .dbStmtEnabled {
90
+ opts = append (opts , trace .WithAttributes (semconv .DBStatementKey .String (cmdsString )))
91
+ }
92
+
86
93
ctx , _ = th .tracer .Start (ctx , "pipeline " + summary , opts ... )
87
94
88
95
return ctx , nil
@@ -105,8 +112,9 @@ func recordError(ctx context.Context, span trace.Span, err error) {
105
112
}
106
113
107
114
type config struct {
108
- tp trace.TracerProvider
109
- attrs []attribute.KeyValue
115
+ tp trace.TracerProvider
116
+ attrs []attribute.KeyValue
117
+ dbStmtEnabled bool
110
118
}
111
119
112
120
// Option specifies instrumentation configuration options.
@@ -136,3 +144,10 @@ func WithAttributes(attrs ...attribute.KeyValue) Option {
136
144
cfg .attrs = append (cfg .attrs , attrs ... )
137
145
})
138
146
}
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