diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7d847bb3..6649308a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -42,10 +42,11 @@ jobs: make test - - name: Run tests - valkey 7 + - name: Run tests - valkey 8 env: LOG_LEVEL: "info" - TEST_REDIS_URI: "redis://localhost:16384" + TEST_REDIS_URI: "redis://localhost:16382" + TEST_VALKEY8_TLS_URI: "valkeys://localhost:16386" TEST_PWD_REDIS_URI: "redis://:redis-password@localhost:16380" run: | go test -v -race -p 1 ./... diff --git a/Makefile b/Makefile index c1a58b05..58490303 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,8 @@ test: TEST_VALKEY7_URI="valkey://localhost:16384" \ TEST_VALKEY8_URI="valkey://localhost:16382" \ - + TEST_VALKEY8_TLS_URI="valkeys://localhost:16386" \ + TEST_REDIS7_TLS_URI="rediss://localhost:16386" \ TEST_REDIS_URI="redis://localhost:16385" \ TEST_REDIS7_URI="redis://localhost:16385" \ TEST_REDIS5_URI="redis://localhost:16383" \ diff --git a/docker-compose.yml b/docker-compose.yml index 493c9b66..fe625d0d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,12 +7,38 @@ services: - "16385:6379" - "6379:6379" + redis74-tls: + image: redis:7.4 + volumes: + - ./contrib/tls:/tls + command: | + valkey-server --enable-debug-command yes --protected-mode no + --tls-port 6379 --port 0 + --tls-cert-file /tls/redis.crt + --tls-key-file /tls/redis.key + --tls-ca-cert-file /tls/ca.crt + ports: + - "16387:6379" + valkey8: image: valkey/valkey:8 command: "valkey-server --enable-debug-command yes --protected-mode no" ports: - "16382:6379" + valkey8-tls: + image: valkey/valkey:8 + volumes: + - ./contrib/tls:/tls + command: | + valkey-server --enable-debug-command yes --protected-mode no + --tls-port 6379 --port 0 + --tls-cert-file /tls/redis.crt + --tls-key-file /tls/redis.key + --tls-ca-cert-file /tls/ca.crt + ports: + - "16386:6379" + valkey7: image: valkey/valkey:7.2 command: "valkey-server --enable-debug-command yes --protected-mode no" diff --git a/exporter/exporter.go b/exporter/exporter.go index 1796c80e..f0252d4c 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -86,11 +86,20 @@ type Options struct { } // NewRedisExporter returns a new exporter of Redis metrics. -func NewRedisExporter(redisURI string, opts Options) (*Exporter, error) { +func NewRedisExporter(uri string, opts Options) (*Exporter, error) { log.Debugf("NewRedisExporter options: %#v", opts) + switch { + case strings.HasPrefix(uri, "valkey://"): + uri = strings.Replace(uri, "valkey://", "redis://", 1) + case strings.HasPrefix(uri, "valkeys://"): + uri = strings.Replace(uri, "valkeys://", "rediss://", 1) + } + + log.Debugf("NewRedisExporter = using redis uri: %s", uri) + e := &Exporter{ - redisAddr: redisURI, + redisAddr: uri, options: opts, namespace: opts.Namespace, diff --git a/exporter/exporter_test.go b/exporter/exporter_test.go index 6cdec3d0..2a8ef161 100644 --- a/exporter/exporter_test.go +++ b/exporter/exporter_test.go @@ -160,7 +160,7 @@ func setupDBKeys(t *testing.T, uri string) error { } func setupDBKeysCluster(t *testing.T, uri string) error { - e := Exporter{redisAddr: uri} + e, _ := NewRedisExporter(uri, Options{}) c, err := e.connectToRedisCluster() if err != nil { return err @@ -193,7 +193,7 @@ func deleteKeysFromDB(t *testing.T, addr string) error { } func deleteKeysFromDBCluster(addr string) error { - e := Exporter{redisAddr: addr} + e, _ := NewRedisExporter(addr, Options{}) c, err := e.connectToRedisCluster() if err != nil { return err diff --git a/exporter/http_test.go b/exporter/http_test.go index e5924a4c..89e0d20f 100644 --- a/exporter/http_test.go +++ b/exporter/http_test.go @@ -229,6 +229,11 @@ func TestSimultaneousMetricsHttpRequests(t *testing.T) { os.Getenv("TEST_REDIS_URI"), os.Getenv("TEST_REDIS_2_8_URI"), + os.Getenv("TEST_REDIS7_URI"), + os.Getenv("TEST_REDIS7_TLS_URI"), + + os.Getenv("TEST_VALKEY8_URI"), + os.Getenv("TEST_KEYDB01_URI"), os.Getenv("TEST_KEYDB02_URI"), diff --git a/exporter/redis.go b/exporter/redis.go index 05bc6b98..3d9be768 100644 --- a/exporter/redis.go +++ b/exporter/redis.go @@ -45,13 +45,6 @@ func (e *Exporter) connectToRedis() (redis.Conn, error) { uri = "redis://" + uri } - switch { - case strings.HasPrefix(uri, "valkey://"): - uri = strings.Replace(uri, "valkey://", "redis://", 1) - case strings.HasPrefix(uri, "valkeys://"): - uri = strings.Replace(uri, "valkeys://", "rediss://", 1) - } - options, err := e.configureOptions(uri) if err != nil { return nil, err diff --git a/exporter/redis_test.go b/exporter/redis_test.go index b265a910..76515ff3 100644 --- a/exporter/redis_test.go +++ b/exporter/redis_test.go @@ -31,7 +31,6 @@ func TestHostVariations(t *testing.T) { } } -// todo: also test valkeys://... func TestValkeyScheme(t *testing.T) { host := os.Getenv("TEST_VALKEY8_URI") diff --git a/exporter/tls_test.go b/exporter/tls_test.go index 0789a217..5133e3c2 100644 --- a/exporter/tls_test.go +++ b/exporter/tls_test.go @@ -1,7 +1,11 @@ package exporter import ( + "os" + "strings" "testing" + + "github.com/prometheus/client_golang/prometheus" ) func TestCreateClientTLSConfig(t *testing.T) { @@ -40,6 +44,57 @@ func TestCreateClientTLSConfig(t *testing.T) { } } +func TestValkeyTLSScheme(t *testing.T) { + + for _, host := range []string{ + os.Getenv("TEST_REDIS7_TLS_URI"), + os.Getenv("TEST_VALKEY8_TLS_URI"), + } { + + e, _ := NewRedisExporter(host, + Options{ + SkipTLSVerification: true, + ClientCertFile: "../contrib/tls/redis.crt", + ClientKeyFile: "../contrib/tls/redis.key", + }, + ) + c, err := e.connectToRedis() + if err != nil { + t.Fatalf("connectToRedis() err: %s", err) + } + + if _, err := c.Do("PING", ""); err != nil { + t.Errorf("PING err: %s", err) + } + + c.Close() + + chM := make(chan prometheus.Metric) + go func() { + e.Collect(chM) + close(chM) + }() + + tsts := []struct { + in string + found bool + }{ + {in: "db_keys"}, + {in: "commands_total"}, + {in: "total_connections_received"}, + {in: "used_memory"}, + } + for m := range chM { + desc := m.Desc().String() + for i := range tsts { + if strings.Contains(desc, tsts[i].in) { + tsts[i].found = true + } + } + } + } +} + func TestCreateServerTLSConfig(t *testing.T) { e := getTestExporter()