Skip to content

Commit

Permalink
fix: disable db.query.text by default
Browse files Browse the repository at this point in the history
  • Loading branch information
wzy9607 committed Nov 16, 2024
1 parent eedb0c6 commit 328d4ba
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 40 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func newConfig(opts ...Option) *config {
tp: otel.GetTracerProvider(),
tracer: nil,

dbStmtEnabled: true,
dbStmtEnabled: false,

metricsEnabled: true,

Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require (
github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0
github.com/redis/go-redis/v9 v9.7.0
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/metric v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
Expand All @@ -13,9 +14,12 @@ require (

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ go.opentelemetry.io/otel/trace v1.32.0 h1:WIC9mYrXf8TmY/EXuULKc8hR17vE+Hjv2cssQD
go.opentelemetry.io/otel/trace v1.32.0/go.mod h1:+i4rkvCraA+tG6AzwloGaCtkx53Fa+L+V8e9a7YvhT8=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
81 changes: 81 additions & 0 deletions hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package redisotel

import (
"context"
"testing"

"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
"go.opentelemetry.io/otel/trace"
)

func Test_clientHook_ProcessHookWithDBStatement(t *testing.T) {
t.Parallel()
provider := sdktrace.NewTracerProvider()
type fields struct {
conf *config
}
tests := []struct {
name string
fields fields
checkFn func(t *testing.T) func(ctx context.Context, cmd redis.Cmder) error
}{
{
name: "disabled by default",
fields: fields{
conf: newConfig(WithTracerProvider(provider)),
},
checkFn: func(t *testing.T) func(ctx context.Context, cmd redis.Cmder) error {
t.Helper()
return func(ctx context.Context, cmd redis.Cmder) error {
attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes()
for _, attr := range attrs {
if attr.Key == semconv.DBQueryTextKey {
t.Fatal("DBQueryText attribute should not exist")
}
}
return nil
}
},
}, {
name: "enable by option",
fields: fields{
conf: newConfig(WithTracerProvider(provider), WithDBStatement(true)),
},
checkFn: func(t *testing.T) func(ctx context.Context, cmd redis.Cmder) error {
t.Helper()
return func(ctx context.Context, cmd redis.Cmder) error {
attrs := trace.SpanFromContext(ctx).(sdktrace.ReadOnlySpan).Attributes()
contains := false
for _, attr := range attrs {
if attr.Key == semconv.DBQueryTextKey {
assert.Equal(t, attr, semconv.DBQueryText("set key value"))
contains = true
}
}
assert.Truef(t, contains, "should have DBQueryText attribute when enabled")
return nil
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hook, err := newClientHook(nil, tt.fields.conf)
if err != nil {
t.Fatal(err)
}
ctx, span := provider.Tracer("redis-test").Start(context.Background(), "redis-test")
cmd := redis.NewCmd(ctx, "set", "key", "value")
defer span.End()

processHook := hook.ProcessHook(tt.checkFn(t))
err = processHook(ctx, cmd)
if err != nil {
t.Fatal(err)
}
})
}
}
39 changes: 0 additions & 39 deletions redisotel_test.go

This file was deleted.

0 comments on commit 328d4ba

Please sign in to comment.