Skip to content

Commit ad74b2a

Browse files
authored
Add some missing metrics and other stuff (#993)
* Add some missing metrics from the memory section * github.com/prometheus/client_golang/prometheus@v1.22.0 * cluster connect errors and tests * TestConnectToClusterUsingPasswordFile
1 parent 53eb193 commit ad74b2a

File tree

8 files changed

+65
-41
lines changed

8 files changed

+65
-41
lines changed

exporter/exporter.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"sync"
1111
"time"
1212

13+
// see https://github.com/prometheus/client_golang/releases/tag/v1.22.0
14+
_ "github.com/prometheus/client_golang/prometheus/promhttp/zstd"
15+
1316
"github.com/gomodule/redigo/redis"
1417
"github.com/prometheus/client_golang/prometheus"
1518
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -155,6 +158,7 @@ func NewRedisExporter(uri string, opts Options) (*Exporter, error) {
155158
"allocator_resident": "allocator_resident_bytes",
156159
"allocator_frag_ratio": "allocator_frag_ratio",
157160
"allocator_frag_bytes": "allocator_frag_bytes",
161+
"allocator_muzzy": "allocator_muzzy_bytes",
158162
"allocator_rss_ratio": "allocator_rss_ratio",
159163
"allocator_rss_bytes": "allocator_rss_bytes",
160164

@@ -186,6 +190,9 @@ func NewRedisExporter(uri string, opts Options) (*Exporter, error) {
186190
"mem_fragmentation_bytes": "mem_fragmentation_bytes",
187191
"mem_clients_slaves": "mem_clients_slaves",
188192
"mem_clients_normal": "mem_clients_normal",
193+
"mem_cluster_links": "mem_cluster_links_bytes",
194+
"mem_aof_buffer": "mem_aof_buffer_bytes",
195+
"mem_replication_backlog": "mem_replication_backlog_bytes",
189196

190197
"expired_stale_perc": "expired_stale_percentage",
191198

@@ -196,6 +203,7 @@ func NewRedisExporter(uri string, opts Options) (*Exporter, error) {
196203
"mem_overhead_db_hashtable_rehashing": "mem_overhead_db_hashtable_rehashing_bytes", // Added in Redis 7.4
197204

198205
"lazyfree_pending_objects": "lazyfree_pending_objects",
206+
"lazyfreed_objects": "lazyfreed_objects",
199207
"active_defrag_running": "active_defrag_running",
200208

201209
"migrate_cached_sockets": "migrate_cached_sockets_total",

exporter/http.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (e *Exporter) discoverClusterNodesHandler(w http.ResponseWriter, r *http.Re
112112

113113
c, err := e.connectToRedisCluster()
114114
if err != nil {
115-
http.Error(w, "Couldn't connect to redis cluster", http.StatusInternalServerError)
115+
http.Error(w, fmt.Sprintf("Couldn't connect to redis cluster: %s", err), http.StatusInternalServerError)
116116
return
117117
}
118118
defer c.Close()

exporter/http_test.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,48 +322,70 @@ func TestHttpHandlers(t *testing.T) {
322322
}
323323

324324
func TestHttpDiscoverClusterNodesHandlers(t *testing.T) {
325-
if os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") == "" {
326-
t.Skipf("TEST_REDIS_CLUSTER_MASTER_URI not set - skipping")
325+
clusterAddr := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
326+
nonClusterAddr := os.Getenv("TEST_REDIS_URI")
327+
if clusterAddr == "" || nonClusterAddr == "" {
328+
t.Skipf("TEST_REDIS_CLUSTER_MASTER_URI or TEST_REDIS_URI not set - skipping")
327329
}
328330

329331
tests := []struct {
330-
path string
332+
addr string
331333
want string
332334
isCluster bool
333335
}{
334336
{
335-
path: "/discover-cluster-nodes",
337+
addr: clusterAddr,
336338
want: "redis://127.0.0.1:7000",
337339
isCluster: true,
338340
},
339341
{
340-
path: "/discover-cluster-nodes",
342+
addr: clusterAddr,
341343
want: "redis://127.0.0.1:7001",
342344
isCluster: true,
343345
},
344346
{
345-
path: "/discover-cluster-nodes",
347+
addr: clusterAddr,
346348
want: "redis://127.0.0.1:7002",
347349
isCluster: true,
348350
},
349351
{
350-
path: "/discover-cluster-nodes",
352+
addr: clusterAddr,
351353
want: "The discovery endpoint is only available on a redis cluster",
352354
isCluster: false,
353355
},
356+
{
357+
addr: nonClusterAddr,
358+
want: "The discovery endpoint is only available on a redis cluster",
359+
isCluster: false,
360+
},
361+
{
362+
addr: nonClusterAddr,
363+
want: "ouldn't connect to redis cluster: Cluster refresh failed",
364+
isCluster: true,
365+
},
366+
{
367+
addr: "doesnt-exist:9876",
368+
want: "The discovery endpoint is only available on a redis cluster",
369+
isCluster: false,
370+
},
371+
{
372+
addr: "doesnt-exist:9876",
373+
want: "Couldn't connect to redis cluster: Cluster refresh failed: redisc: all nodes failed",
374+
isCluster: true,
375+
},
354376
}
355377

356378
for _, tst := range tests {
357-
t.Run(fmt.Sprintf("path: %s, isCluster: %v", tst.path, tst.isCluster), func(t *testing.T) {
358-
e, _ := NewRedisExporter(os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI"), Options{
379+
t.Run(fmt.Sprintf("addr: %s, isCluster: %v", tst.addr, tst.isCluster), func(t *testing.T) {
380+
e, _ := NewRedisExporter(tst.addr, Options{
359381
Namespace: "test",
360382
Registry: prometheus.NewRegistry(),
361383
IsCluster: tst.isCluster,
362384
})
363385
ts := httptest.NewServer(e)
364386
defer ts.Close()
365387

366-
body := downloadURL(t, ts.URL+tst.path)
388+
body := downloadURL(t, ts.URL+"/discover-cluster-nodes")
367389
if !strings.Contains(body, tst.want) {
368390
t.Fatalf(`error, expected string "%s" in body, got body: \n\n%s`, tst.want, body)
369391
}

exporter/nodes_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
)
88

99
func TestNodesGetClusterNodes(t *testing.T) {
10-
if os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") == "" {
10+
host := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
11+
if host == "" {
1112
t.Skipf("TEST_REDIS_CLUSTER_MASTER_URI not set - skipping")
1213
}
1314

14-
host := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
1515
e, _ := NewRedisExporter(host, Options{})
1616
c, err := e.connectToRedisCluster()
1717
if err != nil {

exporter/redis.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,20 @@ func (e *Exporter) connectToRedisCluster() (redis.Conn, error) {
120120
log.Debugf("Running refresh on cluster object")
121121
if err := cluster.Refresh(); err != nil {
122122
log.Errorf("Cluster refresh failed: %v", err)
123+
return nil, fmt.Errorf("Cluster refresh failed: %w", err)
123124
}
124125

125126
log.Debugf("Creating redis connection object")
126127
conn, err := cluster.Dial()
127128
if err != nil {
128129
log.Errorf("Dial failed: %v", err)
130+
return nil, fmt.Errorf("Dial failed: %w", err)
129131
}
130132

131133
c, err := redisc.RetryConn(conn, 10, 100*time.Millisecond)
132134
if err != nil {
133135
log.Errorf("RetryConn failed: %v", err)
136+
return nil, fmt.Errorf("RetryConn failed: %w", err)
134137
}
135138

136139
return c, err

exporter/redis_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package exporter
22

33
import (
4-
"bytes"
54
"net/http/httptest"
65
"net/url"
76
"os"
87
"strings"
98
"testing"
109

1110
"github.com/prometheus/client_golang/prometheus"
12-
log "github.com/sirupsen/logrus"
1311
)
1412

1513
func TestHostVariations(t *testing.T) {
@@ -158,21 +156,14 @@ func TestConnectToClusterUsingPasswordFile(t *testing.T) {
158156
PasswordMap: tst.passMap,
159157
IsCluster: tst.isCluster,
160158
})
161-
var buf bytes.Buffer
162-
log.SetOutput(&buf)
163-
defer func() {
164-
log.SetOutput(os.Stderr)
165-
}()
166159
_, err := e.connectToRedisCluster()
167160
t.Logf("connectToRedisCluster() err: %s", err)
168-
if strings.Contains(buf.String(), "Cluster refresh failed:") && !tst.refreshError {
169-
t.Errorf("Test Cluster connection Failed error")
161+
if err != nil && strings.Contains(err.Error(), "Cluster refresh failed:") && !tst.refreshError {
162+
t.Fatalf("Test Cluster connection Failed error")
170163
}
171-
if err != nil {
172-
t.Errorf("Test Cluster connection Failed-connection error")
164+
if !tst.refreshError && err != nil {
165+
t.Fatalf("Test Cluster connection Failed, err: %s", err)
173166
}
174-
175167
})
176168
}
177-
178169
}

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
module github.com/oliver006/redis_exporter
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/gomodule/redigo v1.9.2
77
github.com/mna/redisc v1.4.0
8-
github.com/prometheus/client_golang v1.21.1
8+
github.com/prometheus/client_golang v1.22.0
99
github.com/prometheus/client_model v0.6.1
1010
github.com/sirupsen/logrus v1.9.3
1111
)
1212

1313
require (
1414
github.com/beorn7/perks v1.0.1 // indirect
1515
github.com/cespare/xxhash/v2 v2.3.0 // indirect
16-
github.com/klauspost/compress v1.17.11 // indirect
16+
github.com/klauspost/compress v1.18.0 // indirect
1717
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
1818
github.com/prometheus/common v0.62.0 // indirect
1919
github.com/prometheus/procfs v0.15.1 // indirect
20-
golang.org/x/sys v0.28.0 // indirect
21-
google.golang.org/protobuf v1.36.1 // indirect
20+
golang.org/x/sys v0.30.0 // indirect
21+
google.golang.org/protobuf v1.36.5 // indirect
2222
)

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
88
github.com/gomodule/redigo v1.8.5/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
99
github.com/gomodule/redigo v1.9.2 h1:HrutZBLhSIU8abiSfW8pj8mPhOyMYjZT/wcA4/L9L9s=
1010
github.com/gomodule/redigo v1.9.2/go.mod h1:KsU3hiK/Ay8U42qpaJk+kuNa3C+spxapWpM+ywhcgtw=
11-
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
12-
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13-
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
14-
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
11+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
12+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
13+
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
14+
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
1515
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
1616
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
1717
github.com/mna/redisc v1.4.0 h1:rBKXyGO/39SGmYoRKCyzXcBpoMMKqkikg8E1G8YIfSA=
@@ -20,8 +20,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
2020
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
2121
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2222
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
23-
github.com/prometheus/client_golang v1.21.1 h1:DOvXXTqVzvkIewV/CDPFdejpMCGeMcbGCQ8YOmu+Ibk=
24-
github.com/prometheus/client_golang v1.21.1/go.mod h1:U9NM32ykUErtVBxdvD3zfi+EuFkkaBvMb09mIfe0Zgg=
23+
github.com/prometheus/client_golang v1.22.0 h1:rb93p9lokFEsctTys46VnV1kLCDpVZ0a/Y92Vm0Zc6Q=
24+
github.com/prometheus/client_golang v1.22.0/go.mod h1:R7ljNsLXhuQXYZYtw6GAE9AZg8Y7vEW5scdCXrWRXC0=
2525
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
2626
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
2727
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
@@ -36,10 +36,10 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
3636
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
3737
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3838
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
39-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
40-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
41-
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
42-
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
39+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
40+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
41+
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
42+
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
4343
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4444
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4545
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)