diff --git a/Taskfile.yml b/Taskfile.yml index 36cad1d..6ed0c61 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -42,7 +42,7 @@ tasks: env: OTEL_EXPORTER_OTLP_ENDPOINT: http://localhost:4317 ENVOY_AUTHZ_SECRET_KEY: "G_TdvPJ9T8C4p&A?Wr3YAUYW$*9vn4?t" - ENVOY_AUTHZ_REDIS_URL: "redis:///0" + ENVOY_AUTHZ_REDIS_URL: "redis:///0?tracing=true" ENVOY_AUTHZ_PROVIDERS_CONFIG: run/config/providers.yaml ENVOY_AUTHZ_LOG_LEVEL: debug cmds: diff --git a/store/parser.go b/store/parser.go index 0f658d9..16fbd6e 100644 --- a/store/parser.go +++ b/store/parser.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/redis/go-redis/extra/redisotel/v9" "github.com/redis/go-redis/v9" ) @@ -55,6 +56,22 @@ func GetRedisClient(url *url.URL) (redis.UniversalClient, error) { return nil, fmt.Errorf("unknown scheme found in redis connection URL: %s", url.Scheme) } + var tracing bool + for k, v := range url.Query() { + switch param := replacer.Replace(strings.ToLower(k)); param { + case "tracing": + tracing, _ = strconv.ParseBool(v[0]) + default: + // do nothing + } + } + + if tracing { + if err := redisotel.InstrumentTracing(client); err != nil { + return nil, err + } + } + return client, nil } @@ -149,6 +166,7 @@ func getRedisOptions(uri *url.URL) (*redis.UniversalOptions, error) { opts.SentinelUsername = v[0] case "sentinelpassword": opts.SentinelPassword = v[0] + case "tracing": default: return nil, fmt.Errorf("detected unknown configuration option '%s'", param) } diff --git a/store/parser_test.go b/store/parser_test.go index c1081ac..7f17961 100644 --- a/store/parser_test.go +++ b/store/parser_test.go @@ -462,6 +462,16 @@ func TestIPv6HostAddress(t *testing.T) { } } +func TestEnableTracing(t *testing.T) { + uri, _ := url.Parse("redis://myredis/0?tracing=true") + universalClient, err := GetRedisClient(uri) + _, ok := universalClient.(*redis.Client) + + if err != nil || !ok { + t.Fail() + } +} + func TestConvertStringToDurationNoUnit(t *testing.T) { uri, _ := url.Parse("redis://myredis/0?timeout=200") opts := uriMustGetRedisOptions(uri) diff --git a/store/store.go b/store/store.go index 00b9f20..9f093b2 100644 --- a/store/store.go +++ b/store/store.go @@ -10,7 +10,6 @@ import ( gocache_store "github.com/eko/gocache/store/go_cache/v4" redis_store "github.com/eko/gocache/store/redis/v4" gocache "github.com/patrickmn/go-cache" - "github.com/redis/go-redis/extra/redisotel/v9" ) type Store struct { @@ -33,9 +32,6 @@ func NewStore(url *url.URL, expiration time.Duration) *Store { slog.Error("Failed to connect to Redis", slog.String("err", err.Error())) panic(err) } - if err := redisotel.InstrumentTracing(redisClient); err != nil { - panic(err) - } slog.Info("Using Redis cache", slog.String("url", url.String())) redisStore := redis_store.NewRedis(redisClient, store.WithExpiration(expiration))