diff --git a/go.mod b/go.mod index 563b7e1226..504f36d041 100644 --- a/go.mod +++ b/go.mod @@ -77,7 +77,7 @@ require ( github.com/prometheus/client_golang v1.23.2 github.com/prometheus/client_model v0.6.2 github.com/r3labs/diff/v3 v3.0.2 - github.com/redis/go-redis/v9 v9.8.0 + github.com/redis/go-redis/v9 v9.17.2 github.com/robfig/cron/v3 v3.0.2-0.20210106135023-bc59245fe10e github.com/sirupsen/logrus v1.9.3 github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 diff --git a/go.sum b/go.sum index 3366b520e6..d93994810a 100644 --- a/go.sum +++ b/go.sum @@ -823,8 +823,8 @@ github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlT github.com/r3labs/diff/v3 v3.0.2 h1:yVuxAY1V6MeM4+HNur92xkS39kB/N+cFi2hMkY06BbA= github.com/r3labs/diff/v3 v3.0.2/go.mod h1:Cy542hv0BAEmhDYWtGxXRQ4kqRsVIcEjG9gChUlTmkw= github.com/redis/go-redis/v9 v9.0.0-rc.4/go.mod h1:Vo3EsyWnicKnSKCA7HhgnvnyA74wOA69Cd2Meli5mmA= -github.com/redis/go-redis/v9 v9.8.0 h1:q3nRvjrlge/6UD7eTu/DSg2uYiU2mCL0G/uzBWqhicI= -github.com/redis/go-redis/v9 v9.8.0/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6Q5lHuCH/Iw= +github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI= +github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370= github.com/robfig/cron/v3 v3.0.2-0.20210106135023-bc59245fe10e h1:0xChnl3lhHiXbgSJKgChye0D+DvoItkOdkGcwelDXH0= github.com/robfig/cron/v3 v3.0.2-0.20210106135023-bc59245fe10e/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= diff --git a/util/cache/redis.go b/util/cache/redis.go index c60bdcbc2f..bda6af00fc 100644 --- a/util/cache/redis.go +++ b/util/cache/redis.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "net" + "strings" "sync" "time" @@ -177,6 +178,23 @@ type redisHook struct { registry MetricsRegistry } +// ignoredRedisCommandNames are commands that go-redis may issue during connection setup / bookkeeping +// and which we don't want to count as application-level requests in metrics. +var ignoredRedisCommandNames = map[string]struct{}{ + "hello": {}, + "client": {}, + // Optional: we can enable if we want also want to exclude other setup/noise commands. + // "auth": {}, + // "select": {}, + // "ping": {}, +} + +func shouldIgnoreRedisCmd(cmd redis.Cmder) bool { + name := strings.ToLower(strings.TrimSpace(cmd.Name())) + _, ok := ignoredRedisCommandNames[name] + return ok +} + func (rh *redisHook) DialHook(next redis.DialHook) redis.DialHook { return func(ctx context.Context, network, addr string) (net.Conn, error) { conn, err := next(ctx, network, addr) @@ -189,6 +207,11 @@ func (rh *redisHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook { startTime := time.Now() err := next(ctx, cmd) + + if shouldIgnoreRedisCmd(cmd) { + return err + } + rh.registry.IncRedisRequest(err != nil && !errors.Is(err, redis.Nil)) rh.registry.ObserveRedisRequestDuration(time.Since(startTime)) diff --git a/util/cache/redis_test.go b/util/cache/redis_test.go index ee8f7a432d..9ebf2c871d 100644 --- a/util/cache/redis_test.go +++ b/util/cache/redis_test.go @@ -133,6 +133,7 @@ func TestRedisMetrics(t *testing.T) { ms := NewMockMetricsServer() redisClient := redis.NewClient(&redis.Options{Addr: mr.Addr()}) faultyRedisClient := redis.NewClient(&redis.Options{Addr: "invalidredishost.invalid:12345"}) + CollectMetrics(redisClient, ms, nil) CollectMetrics(faultyRedisClient, ms, nil)