| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http/httptest" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/gomodule/redigo/redis" |
| 11 | "github.com/prometheus/client_golang/prometheus" |
| 12 | dto "github.com/prometheus/client_model/go" |
| 13 | ) |
| 14 | |
| 15 | func TestSentinelExtractInfoMetrics(t *testing.T) { |
| 16 | if os.Getenv("TEST_VALKEY_SENTINEL_URI") == "" { |
| 17 | t.Skipf("TEST_VALKEY_SENTINEL_URI not set - skipping") |
| 18 | } |
| 19 | addr := os.Getenv("TEST_VALKEY_SENTINEL_URI") |
| 20 | e, _ := NewRedisExporter( |
| 21 | addr, |
| 22 | Options{Namespace: "test"}, |
| 23 | ) |
| 24 | c, err := redis.DialURL(addr) |
| 25 | if err != nil { |
| 26 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 27 | } |
| 28 | |
| 29 | infoAll, err := redis.String(doRedisCmd(c, "INFO", "ALL")) |
| 30 | if err != nil { |
| 31 | t.Logf("Redis INFO ALL err: %s", err) |
| 32 | infoAll, err = redis.String(doRedisCmd(c, "INFO")) |
| 33 | if err != nil { |
| 34 | t.Fatalf("Redis INFO err: %s", err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | chM := make(chan prometheus.Metric) |
| 39 | go func() { |
| 40 | e.extractInfoMetrics(chM, infoAll, 0) |
| 41 | close(chM) |
| 42 | }() |
| 43 | want := map[string]bool{ |
| 44 | "sentinel_tilt": false, |
| 45 | "sentinel_running_scripts": false, |
| 46 | "sentinel_scripts_queue_length": false, |
| 47 | "sentinel_simulate_failure_flags": false, |
| 48 | "sentinel_masters": false, |
| 49 | "sentinel_master_status": false, |
| 50 | "sentinel_master_slaves": false, |
| 51 | "sentinel_master_sentinels": false, |
| 52 | } |
| 53 | |
| 54 | for m := range chM { |
| 55 | for k := range want { |
| 56 | if strings.Contains(m.Desc().String(), k) { |
| 57 | want[k] = true |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | for k, found := range want { |
| 62 | if !found { |
| 63 | t.Errorf("didn't find %s", k) |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | type sentinelData struct { |
| 70 | k, v string |
| 71 | name, status, address string |
| 72 | slaves, sentinels float64 |
| 73 | ok bool |
| 74 | } |
| 75 | |
| 76 | func TestSentinelParseSentinelMasterString(t *testing.T) { |
| 77 | tsts := []sentinelData{ |
| 78 | {k: "master0", v: "name=user03,status=sdown,address=192.169.2.52:6381,slaves=1,sentinels=5", name: "user03", status: "sdown", address: "192.169.2.52:6381", slaves: 1, sentinels: 5, ok: true}, |
| 79 | {k: "master1", v: "name=master,status=ok,address=127.0.0.1:6379,slaves=999,sentinels=500", name: "master", status: "ok", address: "127.0.0.1:6379", slaves: 999, sentinels: 500, ok: true}, |
| 80 | |
| 81 | {k: "master", v: "name=user03", ok: false}, |
| 82 | {k: "masterA", v: "status=ko", ok: false}, |
| 83 | {k: "master0", v: "slaves=abc,sentinels=0", ok: false}, |
| 84 | {k: "master0", v: "slaves=0,sentinels=abc", ok: false}, |
| 85 | } |
| 86 | |
| 87 | for _, tst := range tsts { |
| 88 | name := fmt.Sprintf("%s---%s", tst.k, tst.v) |
| 89 | t.Run(name, func(t *testing.T) { |
| 90 | if masterName, masterStatus, masterAddress, masterSlaves, masterSentinels, ok := parseSentinelMasterString(tst.k, tst.v); true { |
| 91 | if ok != tst.ok { |
| 92 | t.Errorf("failed for: master:%s data:%s", tst.k, tst.v) |
| 93 | return |
| 94 | } |
| 95 | if masterName != tst.name || masterStatus != tst.status || masterAddress != tst.address || masterSlaves != tst.slaves || masterSentinels != tst.sentinels { |
| 96 | t.Errorf("values not matching:\nstring:%s\ngot:%s %s %s %f %f", tst.v, masterName, masterStatus, masterAddress, masterSlaves, masterSentinels) |
| 97 | } |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestSentinelExtractSentinelMetricsForRedis(t *testing.T) { |
| 104 | if os.Getenv("TEST_REDIS_URI") == "" { |
| 105 | t.Skipf("TEST_REDIS_URI not set - skipping") |
| 106 | } |
| 107 | addr := os.Getenv("TEST_REDIS_URI") |
| 108 | e, _ := NewRedisExporter( |
| 109 | addr, |
| 110 | Options{Namespace: "test"}, |
| 111 | ) |
| 112 | c, err := redis.DialURL(addr) |
| 113 | if err != nil { |
| 114 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 115 | } |
| 116 | defer c.Close() |
| 117 | |
| 118 | chM := make(chan prometheus.Metric) |
| 119 | go func() { |
| 120 | e.extractSentinelMetrics(chM, c) |
| 121 | close(chM) |
| 122 | }() |
| 123 | |
| 124 | want := map[string]bool{ |
| 125 | "sentinel_master_ok_sentinels": false, |
| 126 | "sentinel_master_ok_slaves": false, |
| 127 | } |
| 128 | |
| 129 | for m := range chM { |
| 130 | for k := range want { |
| 131 | if strings.Contains(m.Desc().String(), k) { |
| 132 | want[k] = true |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | for k, found := range want { |
| 137 | if found { |
| 138 | t.Errorf("Found sentinel metric %s for redis instance", k) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestSentinelExtractSentinelMetricsForSentinel(t *testing.T) { |
| 144 | if os.Getenv("TEST_VALKEY_SENTINEL_URI") == "" { |
| 145 | t.Skipf("TEST_VALKEY_SENTINEL_URI not set - skipping") |
| 146 | } |
| 147 | addr := os.Getenv("TEST_VALKEY_SENTINEL_URI") |
| 148 | e, _ := NewRedisExporter( |
| 149 | addr, |
| 150 | Options{Namespace: "test"}, |
| 151 | ) |
| 152 | c, err := redis.DialURL(addr) |
| 153 | if err != nil { |
| 154 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 155 | } |
| 156 | defer c.Close() |
| 157 | |
| 158 | infoAll, err := redis.String(doRedisCmd(c, "INFO", "ALL")) |
| 159 | if err != nil { |
| 160 | t.Logf("Redis INFO ALL err: %s", err) |
| 161 | infoAll, err = redis.String(doRedisCmd(c, "INFO")) |
| 162 | if err != nil { |
| 163 | t.Fatalf("Redis INFO err: %s", err) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | chM := make(chan prometheus.Metric) |
| 168 | if strings.Contains(infoAll, "# Sentinel") { |
| 169 | go func() { |
| 170 | e.extractSentinelMetrics(chM, c) |
| 171 | close(chM) |
| 172 | }() |
| 173 | } else { |
| 174 | t.Fatalf("Couldn't find sentinel section in Redis INFO: %s", infoAll) |
| 175 | } |
| 176 | |
| 177 | want := map[string]bool{ |
| 178 | "sentinel_master_ok_sentinels": false, |
| 179 | "sentinel_master_ok_slaves": false, |
| 180 | "sentinel_master_ckquorum_status": false, |
| 181 | "sentinel_master_setting_ckquorum": false, |
| 182 | "sentinel_master_setting_failover_timeout": false, |
| 183 | "sentinel_master_setting_parallel_syncs": false, |
| 184 | "sentinel_master_setting_down_after_milliseconds": false, |
| 185 | } |
| 186 | |
| 187 | for m := range chM { |
| 188 | for k := range want { |
| 189 | if strings.Contains(m.Desc().String(), k) { |
| 190 | want[k] = true |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | for k, found := range want { |
| 195 | if !found { |
| 196 | t.Errorf("didn't find metric %s", k) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | type sentinelSentinelsData struct { |
| 202 | name string |
| 203 | sentinelDetails []any |
| 204 | labels []string |
| 205 | expectedMetricValue map[string]int |
| 206 | expectedPeerInfoCount int |
| 207 | } |
| 208 | |
| 209 | func TestSentinelProcessSentinels(t *testing.T) { |
| 210 | if os.Getenv("TEST_VALKEY_SENTINEL_URI") == "" { |
| 211 | t.Skipf("TEST_VALKEY_SENTINEL_URI not set - skipping") |
| 212 | } |
| 213 | addr := os.Getenv("TEST_VALKEY_SENTINEL_URI") |
| 214 | e, _ := NewRedisExporter( |
| 215 | addr, |
| 216 | Options{Namespace: "test", InclSentinelPeerInfo: true}, |
| 217 | ) |
| 218 | |
| 219 | oneOkSentinelExpectedMetricValue := map[string]int{ |
| 220 | "sentinel_master_ok_sentinels": 1, |
| 221 | } |
| 222 | twoOkSentinelExpectedMetricValue := map[string]int{ |
| 223 | "sentinel_master_ok_sentinels": 2, |
| 224 | } |
| 225 | tsts := []sentinelSentinelsData{ |
| 226 | {"1/1 okay sentinel", []any{[]any{[]byte("")}}, []string{"mymaster", "172.17.0.7:26379"}, oneOkSentinelExpectedMetricValue, 0}, |
| 227 | {"1/3 okay sentinel", []any{[]any{[]byte("name"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("ip"), []byte("172.17.0.8"), []byte("port"), []byte("26379"), []byte("runid"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("flags"), []byte("o_down,s_down,sentinel"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823816"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}, []any{[]byte("name"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("ip"), []byte("172.17.0.7"), []byte("port"), []byte("26379"), []byte("runid"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("flags"), []byte("s_down,sentinel"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823815"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}}, []string{"mymaster", "172.17.0.7:26379"}, oneOkSentinelExpectedMetricValue, 2}, |
| 228 | {"2/3 okay sentinel(string is not byte slice)", []any{[]any{[]byte("name"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("ip"), []byte("172.17.0.8"), []byte("port"), []byte("26379"), []byte("runid"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("flags"), []byte("sentinel"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823816"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}, []any{[]byte("name"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("ip"), []byte("172.17.0.7"), []byte("port"), []byte("26379"), []byte("runid"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("flags"), "sentinel", []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823815"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}}, []string{"mymaster", "172.17.0.7:26379"}, twoOkSentinelExpectedMetricValue, 1}, |
| 229 | {"2/3 okay sentinel", []any{[]any{[]byte("name"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("ip"), []byte("172.17.0.8"), []byte("port"), []byte("26379"), []byte("runid"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("flags"), []byte("sentinel"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823816"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}, []any{[]byte("name"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("ip"), []byte("172.17.0.7"), []byte("port"), []byte("26379"), []byte("runid"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("flags"), []byte("s_down,sentinel"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823815"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}}, []string{"mymaster", "172.17.0.7:26379"}, twoOkSentinelExpectedMetricValue, 2}, |
| 230 | {"2/3 okay sentinel(missing flags)", []any{[]any{[]byte("name"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("ip"), []byte("172.17.0.8"), []byte("port"), []byte("26379"), []byte("runid"), []byte("284bc2ef46881bd71e81610152cb96031d211d28"), []byte("flags"), []byte("sentinel"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823816"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}, []any{[]byte("name"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("ip"), []byte("172.17.0.7"), []byte("port"), []byte("26379"), []byte("runid"), []byte("c3ab3cdcaeb193bb49b16d4d3da88def984ab3bf"), []byte("link-pending-commands"), []byte("38"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("11828891"), []byte("last-ok-ping-reply"), []byte("11829539"), []byte("last-ping-reply"), []byte("11829539"), []byte("s-down-time"), []byte("11823815"), []byte("down-after-milliseconds"), []byte("5000"), []byte("last-hello-message"), []byte("11829434"), []byte("voted-leader"), []byte("?"), []byte("voted-leader-epoch"), []byte("0")}}, []string{"mymaster", "172.17.0.7:26379"}, twoOkSentinelExpectedMetricValue, 2}, |
| 231 | } |
| 232 | for _, tst := range tsts { |
| 233 | t.Run(tst.name, func(t *testing.T) { |
| 234 | chM := make(chan prometheus.Metric) |
| 235 | go func() { |
| 236 | e.processSentinelSentinels(chM, tst.sentinelDetails, tst.labels...) |
| 237 | close(chM) |
| 238 | }() |
| 239 | want := map[string]bool{ |
| 240 | "sentinel_master_ok_sentinels": false, |
| 241 | "sentinel_peer_info": false, |
| 242 | } |
| 243 | peerInfoCount := 0 |
| 244 | |
| 245 | for m := range chM { |
| 246 | descStr := m.Desc().String() |
| 247 | if strings.Contains(descStr, "sentinel_peer_info") { |
| 248 | peerInfoCount++ |
| 249 | want["sentinel_peer_info"] = true |
| 250 | } |
| 251 | for k := range want { |
| 252 | if k == "sentinel_peer_info" { |
| 253 | continue |
| 254 | } |
| 255 | if strings.Contains(descStr, k) { |
| 256 | want[k] = true |
| 257 | got := &dto.Metric{} |
| 258 | m.Write(got) |
| 259 | |
| 260 | val := got.GetGauge().GetValue() |
| 261 | if int(val) != tst.expectedMetricValue[k] { |
| 262 | t.Errorf("Expected metric value %d didn't match to reported value %d for test %s", tst.expectedMetricValue[k], int(val), tst.name) |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | if tst.expectedPeerInfoCount > 0 { |
| 268 | if peerInfoCount != tst.expectedPeerInfoCount { |
| 269 | t.Errorf("sentinel_peer_info: expected count %d, got %d", tst.expectedPeerInfoCount, peerInfoCount) |
| 270 | } |
| 271 | } |
| 272 | for k, found := range want { |
| 273 | if k == "sentinel_peer_info" && tst.expectedPeerInfoCount == 0 { |
| 274 | continue |
| 275 | } |
| 276 | if !found { |
| 277 | t.Errorf("didn't find metric %s", k) |
| 278 | } |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | type sentinelSlavesData struct { |
| 285 | name string |
| 286 | slaveDetails []any |
| 287 | labels []string |
| 288 | expectedMetricValue map[string]int |
| 289 | } |
| 290 | |
| 291 | // TestSentinelPeerInfoMetric verifies sentinel_peer_info is emitted with correct labels (no live Sentinel required). |
| 292 | func TestSentinelPeerInfoMetric(t *testing.T) { |
| 293 | e, err := NewRedisExporter("redis://localhost:26379", Options{Namespace: "test", InclSentinelPeerInfo: true}) |
| 294 | if err != nil { |
| 295 | t.Fatalf("NewRedisExporter: %v", err) |
| 296 | } |
| 297 | // One peer with all labels; one peer with missing "flags" (tests safe map access). |
| 298 | sentinelDetails := []any{ |
| 299 | []any{ |
| 300 | []byte("name"), []byte("runid-peer1"), |
| 301 | []byte("ip"), []byte("10.0.0.1"), |
| 302 | []byte("port"), []byte("26379"), |
| 303 | []byte("runid"), []byte("runid-peer1"), |
| 304 | []byte("flags"), []byte("sentinel"), |
| 305 | }, |
| 306 | []any{ |
| 307 | []byte("name"), []byte("runid-peer2"), |
| 308 | []byte("ip"), []byte("10.0.0.2"), |
| 309 | []byte("port"), []byte("26380"), |
| 310 | []byte("runid"), []byte("runid-peer2"), |
| 311 | // no "flags" key - must not panic, label should be empty |
| 312 | }, |
| 313 | } |
| 314 | labels := []string{"mymaster", "127.0.0.1:6379"} |
| 315 | |
| 316 | chM := make(chan prometheus.Metric, 16) |
| 317 | go func() { |
| 318 | e.processSentinelSentinels(chM, sentinelDetails, labels...) |
| 319 | close(chM) |
| 320 | }() |
| 321 | |
| 322 | var peerInfoMetrics []*dto.Metric |
| 323 | for m := range chM { |
| 324 | if strings.Contains(m.Desc().String(), "sentinel_peer_info") { |
| 325 | got := &dto.Metric{} |
| 326 | _ = m.Write(got) |
| 327 | peerInfoMetrics = append(peerInfoMetrics, got) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if len(peerInfoMetrics) != 2 { |
| 332 | t.Fatalf("expected 2 sentinel_peer_info metrics, got %d", len(peerInfoMetrics)) |
| 333 | } |
| 334 | |
| 335 | labelMap := func(metric *dto.Metric) map[string]string { |
| 336 | out := make(map[string]string) |
| 337 | for _, lp := range metric.GetLabel() { |
| 338 | out[lp.GetName()] = lp.GetValue() |
| 339 | } |
| 340 | return out |
| 341 | } |
| 342 | |
| 343 | // First peer: all labels set |
| 344 | l0 := labelMap(peerInfoMetrics[0]) |
| 345 | if l0["master_name"] != "mymaster" || l0["master_address"] != "127.0.0.1:6379" { |
| 346 | t.Errorf("first peer: wrong master labels: master_name=%q master_address=%q", l0["master_name"], l0["master_address"]) |
| 347 | } |
| 348 | if l0["name"] != "runid-peer1" || l0["ip"] != "10.0.0.1" || l0["port"] != "26379" || l0["runid"] != "runid-peer1" || l0["flags"] != "sentinel" { |
| 349 | t.Errorf("first peer: wrong peer labels: name=%q ip=%q port=%q runid=%q flags=%q", l0["name"], l0["ip"], l0["port"], l0["runid"], l0["flags"]) |
| 350 | } |
| 351 | |
| 352 | // Second peer: flags missing in input -> must be empty string (no panic) |
| 353 | l1 := labelMap(peerInfoMetrics[1]) |
| 354 | if l1["name"] != "runid-peer2" || l1["ip"] != "10.0.0.2" || l1["port"] != "26380" || l1["runid"] != "runid-peer2" { |
| 355 | t.Errorf("second peer: wrong labels: name=%q ip=%q port=%q runid=%q", l1["name"], l1["ip"], l1["port"], l1["runid"]) |
| 356 | } |
| 357 | if l1["flags"] != "" { |
| 358 | t.Errorf("second peer: expected empty flags when key missing, got %q", l1["flags"]) |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | func TestInclSentinelPeerInfoDisabled(t *testing.T) { |
| 363 | e, err := NewRedisExporter("redis://localhost:26379", Options{Namespace: "test"}) |
| 364 | if err != nil { |
| 365 | t.Fatalf("NewRedisExporter: %v", err) |
| 366 | } |
| 367 | sentinelDetails := []any{ |
| 368 | []any{ |
| 369 | []byte("name"), []byte("peer-a"), |
| 370 | []byte("ip"), []byte("10.0.0.1"), |
| 371 | []byte("port"), []byte("26379"), |
| 372 | []byte("runid"), []byte("rid-a"), |
| 373 | []byte("flags"), []byte("sentinel"), |
| 374 | }, |
| 375 | } |
| 376 | chM := make(chan prometheus.Metric, 8) |
| 377 | go func() { |
| 378 | e.processSentinelSentinels(chM, sentinelDetails, "mymaster", "127.0.0.1:6379") |
| 379 | close(chM) |
| 380 | }() |
| 381 | var peerInfo, okSentinels int |
| 382 | for m := range chM { |
| 383 | ds := m.Desc().String() |
| 384 | if strings.Contains(ds, "sentinel_peer_info") { |
| 385 | peerInfo++ |
| 386 | } |
| 387 | if strings.Contains(ds, "sentinel_master_ok_sentinels") { |
| 388 | okSentinels++ |
| 389 | } |
| 390 | } |
| 391 | if peerInfo != 0 { |
| 392 | t.Errorf("expected no sentinel_peer_info when InclSentinelPeerInfo is false, got %d", peerInfo) |
| 393 | } |
| 394 | if okSentinels != 1 { |
| 395 | t.Errorf("expected sentinel_master_ok_sentinels, got count %d", okSentinels) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | func TestSentinelProcessSentinelsWithoutLabels(t *testing.T) { |
| 400 | e, err := NewRedisExporter("redis://localhost:26379", Options{Namespace: "test", InclSentinelPeerInfo: true}) |
| 401 | if err != nil { |
| 402 | t.Fatalf("NewRedisExporter: %v", err) |
| 403 | } |
| 404 | |
| 405 | sentinelDetails := []any{ |
| 406 | []any{ |
| 407 | []byte("name"), []byte("peer-a"), |
| 408 | []byte("ip"), []byte("10.0.0.1"), |
| 409 | []byte("port"), []byte("26379"), |
| 410 | []byte("runid"), []byte("rid-a"), |
| 411 | []byte("flags"), []byte("sentinel"), |
| 412 | }, |
| 413 | } |
| 414 | |
| 415 | chM := make(chan prometheus.Metric, 8) |
| 416 | go func() { |
| 417 | e.processSentinelSentinels(chM, sentinelDetails) |
| 418 | close(chM) |
| 419 | }() |
| 420 | |
| 421 | gotAny := false |
| 422 | for range chM { |
| 423 | gotAny = true |
| 424 | } |
| 425 | |
| 426 | if gotAny { |
| 427 | t.Errorf("expected no sentinel metrics when labels are missing") |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | func TestSentinelProcessSlavesWithoutLabels(t *testing.T) { |
| 432 | e, err := NewRedisExporter("redis://localhost:26379", Options{Namespace: "test"}) |
| 433 | if err != nil { |
| 434 | t.Fatalf("NewRedisExporter: %v", err) |
| 435 | } |
| 436 | |
| 437 | slaveDetails := []any{ |
| 438 | []any{ |
| 439 | []byte("name"), []byte("172.17.0.3:6379"), |
| 440 | []byte("ip"), []byte("172.17.0.3"), |
| 441 | []byte("port"), []byte("6379"), |
| 442 | []byte("runid"), []byte("rid-a"), |
| 443 | []byte("flags"), []byte("slave"), |
| 444 | }, |
| 445 | } |
| 446 | |
| 447 | chM := make(chan prometheus.Metric, 8) |
| 448 | go func() { |
| 449 | e.processSentinelSlaves(chM, slaveDetails) |
| 450 | close(chM) |
| 451 | }() |
| 452 | |
| 453 | gotAny := false |
| 454 | for range chM { |
| 455 | gotAny = true |
| 456 | } |
| 457 | |
| 458 | if gotAny { |
| 459 | t.Errorf("expected no slave metrics when labels are missing") |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | func TestSentinelProcessSlaves(t *testing.T) { |
| 464 | if os.Getenv("TEST_VALKEY_SENTINEL_URI") == "" { |
| 465 | t.Skipf("TEST_VALKEY_SENTINEL_URI not set - skipping") |
| 466 | } |
| 467 | addr := os.Getenv("TEST_VALKEY_SENTINEL_URI") |
| 468 | e, _ := NewRedisExporter( |
| 469 | addr, |
| 470 | Options{Namespace: "test"}, |
| 471 | ) |
| 472 | zeroOkSlaveExpectedMetricValue := map[string]int{ |
| 473 | "sentinel_master_ok_slaves": 0, |
| 474 | } |
| 475 | oneOkSlaveExpectedMetricValue := map[string]int{ |
| 476 | "sentinel_master_ok_slaves": 1, |
| 477 | } |
| 478 | twoOkSlaveExpectedMetricValue := map[string]int{ |
| 479 | "sentinel_master_ok_slaves": 2, |
| 480 | } |
| 481 | |
| 482 | tsts := []sentinelSlavesData{ |
| 483 | {"0/1 okay slave(string is not byte slice)", []any{[]any{[]string{"name"}, []byte("172.17.0.3:6379"), []byte("ip"), []byte("172.17.0.3"), []byte("port"), []byte("6379"), []byte("runid"), []byte("42ebb784f2bd560903de9fb7d4533263d5db558a"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("0"), []byte("last-ok-ping-reply"), []byte("490"), []byte("last-ping-reply"), []byte("490"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("2636"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("48279581"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("765829")}}, []string{"mymaster", "172.17.0.7:26379"}, zeroOkSlaveExpectedMetricValue}, |
| 484 | {"1/1 okay slave", []any{[]any{[]byte("name"), []byte("172.17.0.3:6379"), []byte("ip"), []byte("172.17.0.3"), []byte("port"), []byte("6379"), []byte("runid"), []byte("42ebb784f2bd560903de9fb7d4533263d5db558a"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("0"), []byte("last-ok-ping-reply"), []byte("490"), []byte("last-ping-reply"), []byte("490"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("2636"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("48279581"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("765829")}}, []string{"mymaster", "172.17.0.7:26379"}, oneOkSlaveExpectedMetricValue}, |
| 485 | {"1/3 okay slave", []any{[]any{[]byte("name"), []byte("172.17.0.6:6379"), []byte("ip"), []byte("172.17.0.6"), []byte("port"), []byte("6379"), []byte("runid"), []byte("254576b435fcd73121a6497d3b03f3a464de9a10"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("last-ok-ping-reply"), []byte("1021"), []byte("last-ping-reply"), []byte("1021"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("6293"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("36490"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1316759")}, []any{[]byte("name"), []byte("172.17.0.3:6379"), []byte("ip"), []byte("172.17.0.3"), []byte("port"), []byte("6379"), []byte("runid"), []byte("42ebb784f2bd560903de9fb7d4533263d5db558a"), []byte("flags"), []byte("s_down,slave"), []byte("link-pending-commands"), []byte("0"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("0"), []byte("last-ok-ping-reply"), []byte("655"), []byte("last-ping-reply"), []byte("655"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("6394"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("56525539"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1316759")}, []any{[]byte("name"), []byte("172.17.0.5:6379"), []byte("ip"), []byte("172.17.0.5"), []byte("port"), []byte("6379"), []byte("runid"), []byte("8f4b14e820fab7b38cad640208803dfb9fa225ca"), []byte("flags"), []byte("o_down,s_down,slave"), []byte("link-pending-commands"), []byte("100"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("23792"), []byte("last-ok-ping-reply"), []byte("23902"), []byte("last-ping-reply"), []byte("23902"), []byte("s-down-time"), []byte("18785"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("26352"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("36493"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("redis-master"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1315493")}}, []string{"mymaster", "172.17.0.7:26379"}, oneOkSlaveExpectedMetricValue}, |
| 486 | {"2/3 okay slave", []any{[]any{[]byte("name"), []byte("172.17.0.6:6379"), []byte("ip"), []byte("172.17.0.6"), []byte("port"), []byte("6379"), []byte("runid"), []byte("254576b435fcd73121a6497d3b03f3a464de9a10"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("last-ok-ping-reply"), []byte("1021"), []byte("last-ping-reply"), []byte("1021"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("6293"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("36490"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1316759")}, []any{[]byte("name"), []byte("172.17.0.3:6379"), []byte("ip"), []byte("172.17.0.3"), []byte("port"), []byte("6379"), []byte("runid"), []byte("42ebb784f2bd560903de9fb7d4533263d5db558a"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("0"), []byte("last-ok-ping-reply"), []byte("655"), []byte("last-ping-reply"), []byte("655"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("6394"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("56525539"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1316759")}, []any{[]byte("name"), []byte("172.17.0.5:6379"), []byte("ip"), []byte("172.17.0.5"), []byte("port"), []byte("6379"), []byte("runid"), []byte("8f4b14e820fab7b38cad640208803dfb9fa225ca"), []byte("flags"), []byte("s_down,slave"), []byte("link-pending-commands"), []byte("100"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("23792"), []byte("last-ok-ping-reply"), []byte("23902"), []byte("last-ping-reply"), []byte("23902"), []byte("s-down-time"), []byte("18785"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("26352"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("36493"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("redis-master"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1315493")}}, []string{"mymaster", "172.17.0.7:26379"}, twoOkSlaveExpectedMetricValue}, |
| 487 | {"2/3 okay slave(missing flags)", []any{[]any{[]byte("name"), []byte("172.17.0.6:6379"), []byte("ip"), []byte("172.17.0.6"), []byte("port"), []byte("6379"), []byte("runid"), []byte("254576b435fcd73121a6497d3b03f3a464de9a10"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("last-ok-ping-reply"), []byte("1021"), []byte("last-ping-reply"), []byte("1021"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("6293"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("36490"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1316759")}, []any{[]byte("name"), []byte("172.17.0.3:6379"), []byte("ip"), []byte("172.17.0.3"), []byte("port"), []byte("6379"), []byte("runid"), []byte("42ebb784f2bd560903de9fb7d4533263d5db558a"), []byte("flags"), []byte("slave"), []byte("link-pending-commands"), []byte("0"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("0"), []byte("last-ok-ping-reply"), []byte("655"), []byte("last-ping-reply"), []byte("655"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("6394"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("56525539"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("172.17.0.2"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1316759")}, []any{[]byte("name"), []byte("172.17.0.5:6379"), []byte("ip"), []byte("172.17.0.5"), []byte("port"), []byte("6379"), []byte("runid"), []byte("8f4b14e820fab7b38cad640208803dfb9fa225ca"), []byte("link-pending-commands"), []byte("100"), []byte("link-refcount"), []byte("1"), []byte("last-ping-sent"), []byte("23792"), []byte("last-ok-ping-reply"), []byte("23902"), []byte("last-ping-reply"), []byte("23902"), []byte("s-down-time"), []byte("18785"), []byte("down-after-milliseconds"), []byte("5000"), []byte("info-refresh"), []byte("26352"), []byte("role-reported"), []byte("slave"), []byte("role-reported-time"), []byte("36493"), []byte("master-link-down-time"), []byte("0"), []byte("master-link-status"), []byte("ok"), []byte("master-host"), []byte("redis-master"), []byte("master-port"), []byte("6379"), []byte("slave-priority"), []byte("100"), []byte("slave-repl-offset"), []byte("1315493")}}, []string{"mymaster", "172.17.0.7:26379"}, twoOkSlaveExpectedMetricValue}, |
| 488 | } |
| 489 | for _, tst := range tsts { |
| 490 | t.Run(tst.name, func(t *testing.T) { |
| 491 | chM := make(chan prometheus.Metric) |
| 492 | go func() { |
| 493 | e.processSentinelSlaves(chM, tst.slaveDetails, tst.labels...) |
| 494 | close(chM) |
| 495 | }() |
| 496 | want := map[string]bool{ |
| 497 | "sentinel_master_ok_slaves": false, |
| 498 | } |
| 499 | |
| 500 | for m := range chM { |
| 501 | for k := range want { |
| 502 | if strings.Contains(m.Desc().String(), k) { |
| 503 | want[k] = true |
| 504 | got := &dto.Metric{} |
| 505 | m.Write(got) |
| 506 | |
| 507 | val := got.GetGauge().GetValue() |
| 508 | if int(val) != tst.expectedMetricValue[k] { |
| 509 | t.Errorf("Expected metric value %d didn't match to reported value %d for test %s", tst.expectedMetricValue[k], int(val), tst.name) |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | for k, found := range want { |
| 515 | if !found { |
| 516 | t.Errorf("didn't find metric %s", k) |
| 517 | } |
| 518 | } |
| 519 | }) |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | func TestSentinelScrapeRedisHostSentinelPath(t *testing.T) { |
| 524 | if os.Getenv("TEST_VALKEY_SENTINEL_URI") == "" { |
| 525 | t.Skipf("TEST_VALKEY_SENTINEL_URI not set - skipping") |
| 526 | } |
| 527 | addr := os.Getenv("TEST_VALKEY_SENTINEL_URI") |
| 528 | e, _ := NewRedisExporter( |
| 529 | addr, |
| 530 | Options{Namespace: "test"}, |
| 531 | ) |
| 532 | |
| 533 | chM := make(chan prometheus.Metric, 1000) |
| 534 | go func() { |
| 535 | e.scrapeRedisHost(chM) |
| 536 | close(chM) |
| 537 | }() |
| 538 | |
| 539 | found := false |
| 540 | for m := range chM { |
| 541 | if strings.Contains(m.Desc().String(), "sentinel") { |
| 542 | found = true |
| 543 | break |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | if !found { |
| 548 | t.Error("Expected to find sentinel metrics when scraping sentinel host via scrapeRedisHost()") |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | func TestSentinelScrapeAllConfig(t *testing.T) { |
| 553 | if os.Getenv("TEST_VALKEY_SENTINEL_URI") == "" { |
| 554 | t.Skipf("TEST_VALKEY_SENTINEL_URI not set - skipping") |
| 555 | } |
| 556 | addr := os.Getenv("TEST_VALKEY_SENTINEL_URI") |
| 557 | for _, inc := range []bool{false, true} { |
| 558 | e, _ := NewRedisExporter( |
| 559 | addr, |
| 560 | Options{Namespace: "test", |
| 561 | InclConfigMetrics: inc, |
| 562 | }, |
| 563 | ) |
| 564 | |
| 565 | ts := httptest.NewServer(e) |
| 566 | defer ts.Close() |
| 567 | |
| 568 | body := downloadURL(t, ts.URL+"/metrics") |
| 569 | for _, want := range []string{ |
| 570 | "sentinel_config_key_value", |
| 571 | "sentinel_config_value", |
| 572 | } { |
| 573 | if inc && !strings.Contains(body, want) { |
| 574 | t.Fatalf("didn't find metrics with sentinel_config, want: %s, body: %s", want, body) |
| 575 | return |
| 576 | } else if !inc && strings.Contains(body, want) { |
| 577 | t.Errorf("did NOT want metrics to include sentinel_config, have:\n%s", body) |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |