Skip to content

Commit

Permalink
fix duplicate metrics registration in redis client (#6009)
Browse files Browse the repository at this point in the history
* fix duplicate metrics registration in redis client

Signed-off-by: Kama Huang <kamatogo13@gmail.com>

* fixed test

Signed-off-by: Kama Huang <kamatogo13@gmail.com>

Signed-off-by: Kama Huang <kamatogo13@gmail.com>
  • Loading branch information
kama910 authored and GiedriusS committed Jan 4, 2023
1 parent 8f57668 commit 3f2ab65
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/cacheutil/cacheutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import (
)

func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
goleak.VerifyTestMain(
m,
// https://github.com/rueian/rueidis/blob/v0.0.90/pipe.go#L204.
goleak.IgnoreTopFunction("github.com/rueian/rueidis.(*pipe).backgroundPing"),
)
}

func TestDoWithBatch(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cacheutil/redis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func NewRedisClientWithConfig(logger log.Logger, name string, config RedisClient
return nil, err
}

if reg != nil {
reg = prometheus.WrapRegistererWith(prometheus.Labels{"name": name}, reg)
}

var tlsConfig *tls.Config
if config.TLSEnabled {
userTLSConfig := config.TLSConfig
Expand Down
16 changes: 16 additions & 0 deletions pkg/cacheutil/redis_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,19 @@ func TestValidateRedisConfig(t *testing.T) {
}

}

func TestMultipleRedisClient(t *testing.T) {
s, err := miniredis.Run()
if err != nil {
testutil.Ok(t, err)
}
defer s.Close()
cfg := DefaultRedisClientConfig
cfg.Addr = s.Addr()
logger := log.NewLogfmtLogger(os.Stderr)
reg := prometheus.NewRegistry()
_, err = NewRedisClientWithConfig(logger, "test1", cfg, reg)
testutil.Ok(t, err)
_, err = NewRedisClientWithConfig(logger, "test2", cfg, reg)
testutil.Ok(t, err)
}

0 comments on commit 3f2ab65

Please sign in to comment.