diff --git a/config.go b/config.go index 3a758c1..da0da5c 100644 --- a/config.go +++ b/config.go @@ -49,7 +49,7 @@ func newConfig(opts ...Option) *config { tp: otel.GetTracerProvider(), tracer: nil, - dbStmtEnabled: true, + dbStmtEnabled: false, metricsEnabled: true, diff --git a/go.mod b/go.mod index e48ff28..9d24624 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/go.sum b/go.sum index f62e94a..b43f78d 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/hook_test.go b/hook_test.go new file mode 100644 index 0000000..45eb4b0 --- /dev/null +++ b/hook_test.go @@ -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) + } + }) + } +} diff --git a/redisotel_test.go b/redisotel_test.go deleted file mode 100644 index c335cc9..0000000 --- a/redisotel_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package redisotel - -import ( - "context" - "testing" - - sdktrace "go.opentelemetry.io/otel/sdk/trace" - semconv "go.opentelemetry.io/otel/semconv/v1.27.0" - "go.opentelemetry.io/otel/trace" - - "github.com/redis/go-redis/v9" -) - -func TestWithDBStatement(t *testing.T) { - t.Parallel() - provider := sdktrace.NewTracerProvider() - conf := newConfig(WithTracerProvider(provider), WithDBStatement(false)) - hook, err := newClientHook(nil, conf) - if err != nil { - t.Fatal(err) - } - ctx, span := provider.Tracer("redis-test").Start(context.TODO(), "redis-test") - cmd := redis.NewCmd(ctx, "ping") - defer span.End() - - processHook := hook.ProcessHook(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("Attribute with db statement should not exist") - } - } - return nil - }) - err = processHook(ctx, cmd) - if err != nil { - t.Fatal(err) - } -}