Skip to content

Commit 491b3cd

Browse files
authored
feat!: rename WithDBStatement to EnableDBQueryText (#42)
1 parent f59d5ed commit 491b3cd

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

Diff for: config.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type config struct {
2020
tp trace.TracerProvider
2121
tracer trace.Tracer
2222

23-
dbStmtEnabled bool
23+
dbQueryTextEnabled bool
2424

2525
// Metrics options.
2626

@@ -50,7 +50,7 @@ func newConfig(opts ...Option) *config {
5050
tp: otel.GetTracerProvider(),
5151
tracer: nil,
5252

53-
dbStmtEnabled: false,
53+
dbQueryTextEnabled: false,
5454

5555
metricsEnabled: true,
5656

@@ -106,10 +106,10 @@ func WithTracerProvider(provider trace.TracerProvider) Option {
106106
})
107107
}
108108

109-
// WithDBStatement tells the tracing hook not to log raw redis commands.
110-
func WithDBStatement(on bool) Option {
109+
// EnableDBQueryText tells the tracing hook to log raw redis commands.
110+
func EnableDBQueryText() Option {
111111
return option(func(conf *config) {
112-
conf.dbStmtEnabled = on
112+
conf.dbQueryTextEnabled = true
113113
})
114114
}
115115

Diff for: hook.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (ch *clientHook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
161161
semconv.DBOperationName(oprName),
162162
)
163163

164-
if ch.conf.dbStmtEnabled {
164+
if ch.conf.dbQueryTextEnabled {
165165
cmdString := rediscmd.CmdString(cmd)
166166
attrs = append(attrs, semconv.DBQueryText(cmdString))
167167
}
@@ -204,7 +204,7 @@ func (ch *clientHook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.
204204
semconv.DBOperationBatchSize(len(cmds)),
205205
)
206206

207-
if ch.conf.dbStmtEnabled {
207+
if ch.conf.dbQueryTextEnabled {
208208
attrs = append(attrs, semconv.DBQueryText(cmdsString))
209209
}
210210

Diff for: hook_test.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,10 @@ func Test_clientHook_ProcessHook(t *testing.T) { //nolint:maintidx //table drive
399399
})
400400
},
401401
}, {
402-
name: "enable WithDBStatement option",
402+
name: "enable DBQueryText option",
403403
fields: fields{
404404
rdsOpt: &redis.Options{Addr: "10.1.1.1:6379", DB: 3},
405-
opts: []Option{WithDBStatement(true)},
405+
opts: []Option{EnableDBQueryText()},
406406
},
407407
args: args{
408408
hook: func(ctx context.Context, cmd redis.Cmder) error { return nil },
@@ -537,6 +537,61 @@ func Test_clientHook_ProcessPipelineHook(t *testing.T) {
537537
semconv.ServerPort(6379),
538538
})
539539

540+
assertUseTime(t, sm.Metrics[1], []attribute.KeyValue{
541+
semconv.DBSystemNameRedis,
542+
semconv.DBClientConnectionPoolName("10.1.1.1:6379/3"),
543+
attribute.String("status", "ok"),
544+
})
545+
},
546+
}, {
547+
name: "enable DBQueryText option",
548+
fields: fields{
549+
rdsOpt: &redis.Options{Addr: "10.1.1.1:6379", DB: 3},
550+
opts: []Option{EnableDBQueryText()},
551+
},
552+
args: args{
553+
hook: func(ctx context.Context, cmds []redis.Cmder) error { return nil },
554+
cmds: []redis.Cmder{
555+
redis.NewCmd(context.Background(), "set", "key", "value"),
556+
redis.NewCmd(context.Background(), "get", "key1"),
557+
redis.NewCmd(context.Background(), "get", "key2"),
558+
},
559+
},
560+
checkSpan: func(t *testing.T, span sdktrace.ReadOnlySpan) {
561+
t.Helper()
562+
assert.Equal(t, "pipeline set get", span.Name())
563+
assert.Equal(t, sdktrace.Status{Code: codes.Unset}, span.Status())
564+
t.Logf("attrs: %v", span.Attributes())
565+
566+
wantAttrs := []attribute.KeyValue{
567+
semconv.DBSystemNameRedis,
568+
semconv.DBNamespace("3"),
569+
semconv.DBOperationName("pipeline set get"),
570+
semconv.ServerAddress("10.1.1.1"),
571+
semconv.ServerPort(6379),
572+
semconv.DBQueryText("set key value\nget key1\nget key2"),
573+
}
574+
assert.Subset(t, span.Attributes(), wantAttrs)
575+
576+
wantNotExistAttrs := []attribute.Key{semconv.DBResponseStatusCodeKey}
577+
attrs := attrMap(span.Attributes())
578+
for _, key := range wantNotExistAttrs {
579+
assert.NotContains(t, attrs, key)
580+
}
581+
},
582+
checkMetrics: func(t *testing.T, sm metricdata.ScopeMetrics) {
583+
t.Helper()
584+
require.Len(t, sm.Metrics, 2)
585+
586+
assertOprDuration(t, sm.Metrics[0], []attribute.KeyValue{
587+
semconv.DBSystemNameRedis,
588+
semconv.DBNamespace("3"),
589+
semconv.DBOperationName("pipeline set get"),
590+
semconv.DBOperationBatchSize(3),
591+
semconv.ServerAddress("10.1.1.1"),
592+
semconv.ServerPort(6379),
593+
})
594+
540595
assertUseTime(t, sm.Metrics[1], []attribute.KeyValue{
541596
semconv.DBSystemNameRedis,
542597
semconv.DBClientConnectionPoolName("10.1.1.1:6379/3"),

0 commit comments

Comments
 (0)