Skip to content

Commit

Permalink
feat: toggle redis tracing with uri query param
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjostro committed Aug 28, 2024
1 parent 6266542 commit a940270
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions store/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
)

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions store/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit a940270

Please sign in to comment.