| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "net/http/httptest" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/gomodule/redigo/redis" |
| 10 | "github.com/prometheus/client_golang/prometheus" |
| 11 | log "github.com/sirupsen/logrus" |
| 12 | ) |
| 13 | |
| 14 | type scanStreamFixture struct { |
| 15 | name string |
| 16 | stream string |
| 17 | streamInfo streamInfo |
| 18 | groups []streamGroupsInfo |
| 19 | consumers []streamGroupConsumersInfo |
| 20 | } |
| 21 | |
| 22 | var ( |
| 23 | TestStreamTimestamps = []string{ |
| 24 | "1638006862416-0", |
| 25 | "1638006862417-2", |
| 26 | } |
| 27 | ) |
| 28 | |
| 29 | func isNotTestTimestamp(returned string) bool { |
| 30 | for _, expected := range TestStreamTimestamps { |
| 31 | if parseStreamItemId(expected) == parseStreamItemId(returned) { |
| 32 | return false |
| 33 | } |
| 34 | } |
| 35 | return true |
| 36 | } |
| 37 | |
| 38 | func TestStreamsGetStreamInfo(t *testing.T) { |
| 39 | if os.Getenv("TEST_REDIS6_URI") == "" { |
| 40 | t.Skipf("TEST_REDIS6_URI not set - skipping") |
| 41 | } |
| 42 | |
| 43 | addr := os.Getenv("TEST_REDIS6_URI") |
| 44 | c, err := redis.DialURL(addr) |
| 45 | if err != nil { |
| 46 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 47 | } |
| 48 | defer c.Close() |
| 49 | |
| 50 | setupTestKeys(t, addr) |
| 51 | defer deleteTestKeys(t, addr) |
| 52 | |
| 53 | if _, err = c.Do("SELECT", dbNumStr); err != nil { |
| 54 | t.Errorf("Couldn't select database %#v", dbNumStr) |
| 55 | } |
| 56 | |
| 57 | tsts := []scanStreamFixture{ |
| 58 | { |
| 59 | name: "Stream test", |
| 60 | stream: TestKeyNameStream, |
| 61 | streamInfo: streamInfo{ |
| 62 | Length: 2, |
| 63 | RadixTreeKeys: 1, |
| 64 | RadixTreeNodes: 2, |
| 65 | Groups: 2, |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | for _, tst := range tsts { |
| 71 | t.Run(tst.name, func(t *testing.T) { |
| 72 | info, err := getStreamInfo(c, tst.stream) |
| 73 | if err != nil { |
| 74 | t.Fatalf("Error getting stream info for %#v: %s", tst.stream, err) |
| 75 | } |
| 76 | |
| 77 | if info.Length != tst.streamInfo.Length { |
| 78 | t.Errorf("Stream length mismatch.\nActual: %#v \nExpected: %#v", info.Length, tst.streamInfo.Length) |
| 79 | } |
| 80 | if info.RadixTreeKeys != tst.streamInfo.RadixTreeKeys { |
| 81 | t.Errorf("Stream RadixTreeKeys mismatch.\nActual: %#v \nExpected: %#v", info.RadixTreeKeys, tst.streamInfo.RadixTreeKeys) |
| 82 | } |
| 83 | if info.RadixTreeNodes != tst.streamInfo.RadixTreeNodes { |
| 84 | t.Errorf("Stream RadixTreeNodes mismatch.\nActual: %#v \nExpected: %#v", info.RadixTreeNodes, tst.streamInfo.RadixTreeNodes) |
| 85 | } |
| 86 | if info.Groups != tst.streamInfo.Groups { |
| 87 | t.Errorf("Stream Groups mismatch.\nActual: %#v \nExpected: %#v", info.Groups, tst.streamInfo.Groups) |
| 88 | } |
| 89 | if isNotTestTimestamp(info.LastGeneratedId) { |
| 90 | t.Errorf("Stream LastGeneratedId mismatch.\nActual: %#v \nExpected any of: %#v", info.LastGeneratedId, TestStreamTimestamps) |
| 91 | } |
| 92 | if info.FirstEntryId != TestStreamTimestamps[0] { |
| 93 | t.Errorf("Stream FirstEntryId mismatch.\nActual: %#v \nExpected any of: %#v", info.FirstEntryId, TestStreamTimestamps) |
| 94 | } |
| 95 | if info.LastEntryId != TestStreamTimestamps[len(TestStreamTimestamps)-1] { |
| 96 | t.Errorf("Stream LastEntryId mismatch.\nActual: %#v \nExpected any of: %#v", info.LastEntryId, TestStreamTimestamps) |
| 97 | } |
| 98 | if info.MaxDeletedEntryId != "" { |
| 99 | t.Errorf("Stream MaxDeletedEntryId mismatch.\nActual: %#v \nExpected: %#v", info.MaxDeletedEntryId, "0-0") |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestStreamsGetStreamInfoUsingValKey7(t *testing.T) { |
| 106 | if os.Getenv("TEST_VALKEY7_URI") == "" { |
| 107 | t.Skipf("TEST_VALKEY7_URI not set - skipping") |
| 108 | } |
| 109 | |
| 110 | addr := strings.Replace(os.Getenv("TEST_VALKEY7_URI"), "valkey://", "redis://", 1) |
| 111 | c, err := redis.DialURL(addr) |
| 112 | if err != nil { |
| 113 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 114 | } |
| 115 | defer c.Close() |
| 116 | |
| 117 | setupTestKeys(t, addr) |
| 118 | defer deleteTestKeys(t, addr) |
| 119 | |
| 120 | if _, err = c.Do("SELECT", dbNumStr); err != nil { |
| 121 | t.Errorf("Couldn't select database %#v", dbNumStr) |
| 122 | } |
| 123 | |
| 124 | tsts := []scanStreamFixture{ |
| 125 | { |
| 126 | name: "Stream test", |
| 127 | stream: TestKeyNameStream, |
| 128 | streamInfo: streamInfo{ |
| 129 | Length: 2, |
| 130 | RadixTreeKeys: 1, |
| 131 | RadixTreeNodes: 2, |
| 132 | Groups: 2, |
| 133 | }, |
| 134 | }, |
| 135 | } |
| 136 | |
| 137 | for _, tst := range tsts { |
| 138 | t.Run(tst.name, func(t *testing.T) { |
| 139 | info, err := getStreamInfo(c, tst.stream) |
| 140 | if err != nil { |
| 141 | t.Fatalf("Error getting stream info for %#v: %s", tst.stream, err) |
| 142 | } |
| 143 | |
| 144 | if info.Length != tst.streamInfo.Length { |
| 145 | t.Errorf("Stream length mismatch.\nActual: %#v \nExpected: %#v", info.Length, tst.streamInfo.Length) |
| 146 | } |
| 147 | if info.RadixTreeKeys != tst.streamInfo.RadixTreeKeys { |
| 148 | t.Errorf("Stream RadixTreeKeys mismatch.\nActual: %#v \nExpected: %#v", info.RadixTreeKeys, tst.streamInfo.RadixTreeKeys) |
| 149 | } |
| 150 | if info.RadixTreeNodes != tst.streamInfo.RadixTreeNodes { |
| 151 | t.Errorf("Stream RadixTreeNodes mismatch.\nActual: %#v \nExpected: %#v", info.RadixTreeNodes, tst.streamInfo.RadixTreeNodes) |
| 152 | } |
| 153 | if info.Groups != tst.streamInfo.Groups { |
| 154 | t.Errorf("Stream Groups mismatch.\nActual: %#v \nExpected: %#v", info.Groups, tst.streamInfo.Groups) |
| 155 | } |
| 156 | if isNotTestTimestamp(info.LastGeneratedId) { |
| 157 | t.Errorf("Stream LastGeneratedId mismatch.\nActual: %#v \nExpected any of: %#v", info.LastGeneratedId, TestStreamTimestamps) |
| 158 | } |
| 159 | if info.FirstEntryId != TestStreamTimestamps[0] { |
| 160 | t.Errorf("Stream FirstEntryId mismatch.\nActual: %#v \nExpected any of: %#v", info.FirstEntryId, TestStreamTimestamps) |
| 161 | } |
| 162 | if info.LastEntryId != TestStreamTimestamps[len(TestStreamTimestamps)-1] { |
| 163 | t.Errorf("Stream LastEntryId mismatch.\nActual: %#v \nExpected any of: %#v", info.LastEntryId, TestStreamTimestamps) |
| 164 | } |
| 165 | if info.MaxDeletedEntryId != "0-0" { |
| 166 | t.Errorf("Stream MaxDeletedEntryId mismatch.\nActual: %#v \nExpected: %#v", info.MaxDeletedEntryId, "0-0") |
| 167 | } |
| 168 | }) |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | func TestStreamsScanStreamGroups123(t *testing.T) { |
| 173 | if os.Getenv("TEST_REDIS6_URI") == "" { |
| 174 | t.Skipf("TEST_REDIS6_URI not set - skipping") |
| 175 | } |
| 176 | addr := os.Getenv("TEST_REDIS6_URI") |
| 177 | |
| 178 | c, err := redis.DialURL(addr) |
| 179 | if err != nil { |
| 180 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 181 | } |
| 182 | |
| 183 | if _, err = c.Do("SELECT", dbNumStr); err != nil { |
| 184 | t.Errorf("Couldn't select database %#v", dbNumStr) |
| 185 | } |
| 186 | |
| 187 | fixtures := []keyFixture{ |
| 188 | {"XADD", "test_stream_1", []any{"1638006862521-0", "field_1", "str_1"}}, |
| 189 | {"XADD", "test_stream_2", []any{"1638006862522-0", "field_pattern_1", "str_pattern_1"}}, |
| 190 | } |
| 191 | // Create test streams |
| 192 | c.Do("XGROUP", "CREATE", "test_stream_1", "test_group_1", "$", "MKSTREAM") |
| 193 | c.Do("XGROUP", "CREATE", "test_stream_2", "test_group_1", "$", "MKSTREAM") |
| 194 | c.Do("XGROUP", "CREATE", "test_stream_2", "test_group_2", "$") |
| 195 | |
| 196 | // Add simple values |
| 197 | defer func() { |
| 198 | deleteKeyFixtures(t, c, fixtures) |
| 199 | c.Close() |
| 200 | }() |
| 201 | createKeyFixtures(t, c, fixtures) |
| 202 | |
| 203 | // Process messages to assign Consumers to their groups |
| 204 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", "test_stream_1", ">") |
| 205 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", "test_stream_2", ">") |
| 206 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", "test_stream_2", "0") |
| 207 | |
| 208 | tsts := []scanStreamFixture{ |
| 209 | { |
| 210 | name: "Single group test", |
| 211 | stream: "test_stream_1", |
| 212 | groups: []streamGroupsInfo{ |
| 213 | { |
| 214 | Name: "test_group_1", |
| 215 | Consumers: 1, |
| 216 | Pending: 1, |
| 217 | EntriesRead: 0, |
| 218 | Lag: 0, |
| 219 | LastDeliveredId: "1638006862521-0", |
| 220 | StreamGroupConsumersInfo: []streamGroupConsumersInfo{ |
| 221 | { |
| 222 | Name: "test_consumer_1", |
| 223 | Pending: 1, |
| 224 | }, |
| 225 | }, |
| 226 | }, |
| 227 | }}, |
| 228 | { |
| 229 | name: "Multiple groups test", |
| 230 | stream: "test_stream_2", |
| 231 | groups: []streamGroupsInfo{ |
| 232 | { |
| 233 | Name: "test_group_1", |
| 234 | Consumers: 2, |
| 235 | Pending: 1, |
| 236 | Lag: 0, |
| 237 | EntriesRead: 0, |
| 238 | LastDeliveredId: "1638006862522-0", |
| 239 | }, |
| 240 | { |
| 241 | Name: "test_group_2", |
| 242 | Consumers: 0, |
| 243 | Pending: 0, |
| 244 | Lag: 0, |
| 245 | }, |
| 246 | }}, |
| 247 | } |
| 248 | for _, tst := range tsts { |
| 249 | t.Run(tst.name, func(t *testing.T) { |
| 250 | scannedGroup, err := scanStreamGroups(c, tst.stream) |
| 251 | t.Logf("scanStreamGroups() err: %s", err) |
| 252 | if err != nil { |
| 253 | t.Fatalf("Err: %s", err) |
| 254 | return |
| 255 | } |
| 256 | |
| 257 | if len(scannedGroup) == len(tst.groups) { |
| 258 | for i := range scannedGroup { |
| 259 | if scannedGroup[i].Name != tst.groups[i].Name { |
| 260 | t.Errorf("%d) Group name mismatch.\nExpected: %#v \nActual: %#v", i, tst.groups[i].Name, scannedGroup[i].Name) |
| 261 | } |
| 262 | if scannedGroup[i].Consumers != tst.groups[i].Consumers { |
| 263 | t.Errorf("%d) Consumers count mismatch.\nExpected: %#v \nActual: %#v", i, tst.groups[i].Consumers, scannedGroup[i].Consumers) |
| 264 | } |
| 265 | if scannedGroup[i].Pending != tst.groups[i].Pending { |
| 266 | t.Errorf("%d) Pending items mismatch.\nExpected: %#v \nActual: %#v", i, tst.groups[i].Pending, scannedGroup[i].Pending) |
| 267 | } |
| 268 | if parseStreamItemId(scannedGroup[i].LastDeliveredId) != parseStreamItemId(tst.groups[i].LastDeliveredId) { |
| 269 | t.Errorf("%d) LastDeliveredId items mismatch.\nExpected: %#v \nActual: %#v", i, tst.groups[i].LastDeliveredId, scannedGroup[i].LastDeliveredId) |
| 270 | } |
| 271 | if scannedGroup[i].Lag != tst.groups[i].Lag { |
| 272 | t.Errorf("%d) Lag mismatch.\nExpected: %#v \nActual: %#v", i, tst.groups[i].Lag, scannedGroup[i].Lag) |
| 273 | } |
| 274 | if scannedGroup[i].EntriesRead != tst.groups[i].EntriesRead { |
| 275 | t.Errorf("%d) EntriesRead mismatch.\nExpected: %#v \nActual: %#v", i, tst.groups[i].EntriesRead, scannedGroup[i].EntriesRead) |
| 276 | } |
| 277 | } |
| 278 | } else { |
| 279 | t.Errorf("Consumers entries mismatch.\nExpected: %d\nActual: %d", len(tst.consumers), len(scannedGroup)) |
| 280 | } |
| 281 | }) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | func TestStreamsScanStreamGroupsUsingValKey7(t *testing.T) { |
| 286 | if os.Getenv("TEST_VALKEY7_URI") == "" { |
| 287 | t.Skipf("TEST_VALKEY7_URI not set - skipping") |
| 288 | } |
| 289 | addr := strings.Replace(os.Getenv("TEST_VALKEY7_URI"), "valkey://", "redis://", 1) |
| 290 | db := dbNumStr |
| 291 | |
| 292 | c, err := redis.DialURL(addr) |
| 293 | if err != nil { |
| 294 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 295 | } |
| 296 | |
| 297 | if _, err = c.Do("SELECT", db); err != nil { |
| 298 | t.Errorf("Couldn't select database %#v", db) |
| 299 | } |
| 300 | |
| 301 | fixtures := []keyFixture{ |
| 302 | {"XADD", "test_stream_1", []any{"1638006862521-0", "field_1", "str_1"}}, |
| 303 | {"XADD", "test_stream_2", []any{"1638006862522-0", "field_pattern_1", "str_pattern_1"}}, |
| 304 | } |
| 305 | |
| 306 | // Create test streams |
| 307 | c.Do("XGROUP", "CREATE", "test_stream_1", "test_group_1", "$", "MKSTREAM") |
| 308 | c.Do("XGROUP", "CREATE", "test_stream_2", "test_group_1", "$", "MKSTREAM") |
| 309 | c.Do("XGROUP", "CREATE", "test_stream_2", "test_group_2", "$") |
| 310 | |
| 311 | // Add simple values |
| 312 | defer func() { |
| 313 | deleteKeyFixtures(t, c, fixtures) |
| 314 | c.Close() |
| 315 | }() |
| 316 | createKeyFixtures(t, c, fixtures) |
| 317 | |
| 318 | // Process messages to assign Consumers to their groups |
| 319 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", "test_stream_1", ">") |
| 320 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", "test_stream_2", ">") |
| 321 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", "test_stream_2", "0") |
| 322 | |
| 323 | tsts := []scanStreamFixture{ |
| 324 | { |
| 325 | name: "Single group test", |
| 326 | stream: "test_stream_1", |
| 327 | groups: []streamGroupsInfo{ |
| 328 | { |
| 329 | Name: "test_group_1", |
| 330 | Consumers: 1, |
| 331 | Pending: 1, |
| 332 | EntriesRead: 1, |
| 333 | Lag: 0, |
| 334 | LastDeliveredId: "1638006862521-0", |
| 335 | StreamGroupConsumersInfo: []streamGroupConsumersInfo{ |
| 336 | { |
| 337 | Name: "test_consumer_1", |
| 338 | Pending: 1, |
| 339 | }, |
| 340 | }, |
| 341 | }, |
| 342 | }}, |
| 343 | { |
| 344 | name: "Multiple groups test", |
| 345 | stream: "test_stream_2", |
| 346 | groups: []streamGroupsInfo{ |
| 347 | { |
| 348 | Name: "test_group_1", |
| 349 | Consumers: 2, |
| 350 | Pending: 1, |
| 351 | Lag: 0, |
| 352 | EntriesRead: 1, |
| 353 | LastDeliveredId: "1638006862522-0", |
| 354 | }, |
| 355 | { |
| 356 | Name: "test_group_2", |
| 357 | Consumers: 0, |
| 358 | Pending: 0, |
| 359 | Lag: 1, |
| 360 | }, |
| 361 | }}, |
| 362 | } |
| 363 | for _, tst := range tsts { |
| 364 | t.Run(tst.name, func(t *testing.T) { |
| 365 | scannedGroup, err := scanStreamGroups(c, tst.stream) |
| 366 | t.Logf("scanStreamGroups() err: %s", err) |
| 367 | if err != nil { |
| 368 | t.Errorf("Err: %s", err) |
| 369 | } |
| 370 | |
| 371 | if len(scannedGroup) == len(tst.groups) { |
| 372 | for i := range scannedGroup { |
| 373 | if scannedGroup[i].Name != tst.groups[i].Name { |
| 374 | t.Errorf("Group name mismatch.\nExpected: %#v \nActual: %#v", tst.groups[i].Name, scannedGroup[i].Name) |
| 375 | } |
| 376 | if scannedGroup[i].Consumers != tst.groups[i].Consumers { |
| 377 | t.Errorf("Consumers count mismatch.\nExpected: %#v \nActual: %#v", tst.groups[i].Consumers, scannedGroup[i].Consumers) |
| 378 | } |
| 379 | if scannedGroup[i].Pending != tst.groups[i].Pending { |
| 380 | t.Errorf("Pending items mismatch.\nExpected: %#v \nActual: %#v", tst.groups[i].Pending, scannedGroup[i].Pending) |
| 381 | } |
| 382 | if parseStreamItemId(scannedGroup[i].LastDeliveredId) != parseStreamItemId(tst.groups[i].LastDeliveredId) { |
| 383 | t.Errorf("LastDeliveredId items mismatch.\nExpected: %#v \nActual: %#v", tst.groups[i].LastDeliveredId, scannedGroup[i].LastDeliveredId) |
| 384 | } |
| 385 | if scannedGroup[i].Lag != tst.groups[i].Lag { |
| 386 | t.Errorf("Lag mismatch.\nExpected: %#v \nActual: %#v", tst.groups[i].Lag, scannedGroup[i].Lag) |
| 387 | } |
| 388 | if scannedGroup[i].EntriesRead != tst.groups[i].EntriesRead { |
| 389 | t.Errorf("EntriesRead mismatch.\nExpected: %#v \nActual: %#v", tst.groups[i].EntriesRead, scannedGroup[i].EntriesRead) |
| 390 | } |
| 391 | } |
| 392 | } else { |
| 393 | t.Errorf("Consumers entries mismatch.\nExpected: %d\nActual: %d", len(tst.consumers), len(scannedGroup)) |
| 394 | } |
| 395 | }) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | func TestStreamsScanStreamGroupsConsumers(t *testing.T) { |
| 400 | if os.Getenv("TEST_REDIS_URI") == "" { |
| 401 | t.Skipf("TEST_REDIS_URI not set - skipping") |
| 402 | } |
| 403 | addr := os.Getenv("TEST_REDIS_URI") |
| 404 | db := dbNumStr |
| 405 | |
| 406 | c, err := redis.DialURL(addr) |
| 407 | if err != nil { |
| 408 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 409 | } |
| 410 | |
| 411 | if _, err = c.Do("SELECT", db); err != nil { |
| 412 | t.Errorf("Couldn't select database %#v", db) |
| 413 | } |
| 414 | |
| 415 | fixtures := []keyFixture{ |
| 416 | {"XADD", "single_consumer_stream", []any{"*", "field_1", "str_1"}}, |
| 417 | {"XADD", "multiple_consumer_stream", []any{"*", "field_pattern_1", "str_pattern_1"}}, |
| 418 | } |
| 419 | |
| 420 | // Create test streams |
| 421 | c.Do("XGROUP", "CREATE", "single_consumer_stream", "test_group_1", "$", "MKSTREAM") |
| 422 | c.Do("XGROUP", "CREATE", "multiple_consumer_stream", "test_group_1", "$", "MKSTREAM") |
| 423 | |
| 424 | // Add simple test items to streams |
| 425 | defer func() { |
| 426 | deleteKeyFixtures(t, c, fixtures) |
| 427 | c.Close() |
| 428 | }() |
| 429 | createKeyFixtures(t, c, fixtures) |
| 430 | |
| 431 | // Process messages to assign Consumers to their groups |
| 432 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", "single_consumer_stream", ">") |
| 433 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", "multiple_consumer_stream", ">") |
| 434 | c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", "multiple_consumer_stream", "0") |
| 435 | |
| 436 | tsts := []scanStreamFixture{ |
| 437 | { |
| 438 | name: "Single group test", |
| 439 | stream: "single_consumer_stream", |
| 440 | groups: []streamGroupsInfo{{Name: "test_group_1"}}, |
| 441 | consumers: []streamGroupConsumersInfo{ |
| 442 | { |
| 443 | Name: "test_consumer_1", |
| 444 | Pending: 1, |
| 445 | }, |
| 446 | }, |
| 447 | }, |
| 448 | { |
| 449 | name: "Multiple consumers test", |
| 450 | stream: "multiple_consumer_stream", |
| 451 | groups: []streamGroupsInfo{{Name: "test_group_1"}}, |
| 452 | consumers: []streamGroupConsumersInfo{ |
| 453 | { |
| 454 | Name: "test_consumer_1", |
| 455 | Pending: 1, |
| 456 | }, |
| 457 | { |
| 458 | Name: "test_consumer_2", |
| 459 | Pending: 0, |
| 460 | }, |
| 461 | }, |
| 462 | }, |
| 463 | } |
| 464 | |
| 465 | for _, tst := range tsts { |
| 466 | t.Run(tst.name, func(t *testing.T) { |
| 467 | |
| 468 | // For each group |
| 469 | for _, g := range tst.groups { |
| 470 | g.StreamGroupConsumersInfo, err = scanStreamGroupConsumers(c, tst.stream, g.Name) |
| 471 | if err != nil { |
| 472 | t.Errorf("Err: %s", err) |
| 473 | } |
| 474 | if len(g.StreamGroupConsumersInfo) == len(tst.consumers) { |
| 475 | for i := range g.StreamGroupConsumersInfo { |
| 476 | if g.StreamGroupConsumersInfo[i].Name != tst.consumers[i].Name { |
| 477 | t.Errorf("Consumer name mismatch.\nExpected: %#v \nActual: %#v", tst.consumers[i].Name, g.StreamGroupConsumersInfo[i].Name) |
| 478 | } |
| 479 | if g.StreamGroupConsumersInfo[i].Pending != tst.consumers[i].Pending { |
| 480 | t.Errorf("Pending items mismatch for %s.\nExpected: %#v \nActual: %#v", g.StreamGroupConsumersInfo[i].Name, tst.consumers[i].Pending, g.StreamGroupConsumersInfo[i].Pending) |
| 481 | } |
| 482 | |
| 483 | } |
| 484 | } else { |
| 485 | t.Errorf("Consumers entries mismatch.\nExpected: %d\nActual: %d", len(tst.consumers), len(g.StreamGroupConsumersInfo)) |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | }) |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | func TestStreamsExtractStreamMetrics(t *testing.T) { |
| 494 | if os.Getenv("TEST_REDIS_URI") == "" { |
| 495 | t.Skipf("TEST_REDIS_URI not set - skipping") |
| 496 | } |
| 497 | addr := os.Getenv("TEST_REDIS_URI") |
| 498 | e, _ := NewRedisExporter( |
| 499 | addr, |
| 500 | Options{Namespace: "test", CheckSingleStreams: dbNumStrFull + "=" + TestKeyNameStream}, |
| 501 | ) |
| 502 | c, err := redis.DialURL(addr) |
| 503 | if err != nil { |
| 504 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 505 | } |
| 506 | |
| 507 | setupTestKeys(t, addr) |
| 508 | defer deleteTestKeys(t, addr) |
| 509 | |
| 510 | chM := make(chan prometheus.Metric) |
| 511 | go func() { |
| 512 | e.extractStreamMetrics(chM, c) |
| 513 | close(chM) |
| 514 | }() |
| 515 | want := map[string]bool{ |
| 516 | "stream_length": false, |
| 517 | "stream_radix_tree_keys": false, |
| 518 | "stream_radix_tree_nodes": false, |
| 519 | "stream_last_generated_id": false, |
| 520 | "stream_groups": false, |
| 521 | "stream_max_deleted_entry_id": false, |
| 522 | "stream_first_entry_id": false, |
| 523 | "stream_last_entry_id": false, |
| 524 | "stream_group_consumers": false, |
| 525 | "stream_group_messages_pending": false, |
| 526 | "stream_group_last_delivered_id": false, |
| 527 | "stream_group_entries_read": false, |
| 528 | "stream_group_lag": false, |
| 529 | "stream_group_consumer_messages_pending": false, |
| 530 | "stream_group_consumer_idle_seconds": false, |
| 531 | } |
| 532 | |
| 533 | for m := range chM { |
| 534 | for k := range want { |
| 535 | log.Debugf("metric: %s", m.Desc().String()) |
| 536 | log.Debugf("want: %s", k) |
| 537 | |
| 538 | if strings.Contains(m.Desc().String(), k) { |
| 539 | want[k] = true |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | for k, found := range want { |
| 544 | if !found { |
| 545 | t.Errorf("didn't find %s", k) |
| 546 | } |
| 547 | |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | func TestStreamsExtractStreamMetricsExcludeConsumer(t *testing.T) { |
| 552 | if os.Getenv("TEST_REDIS_URI") == "" { |
| 553 | t.Skipf("TEST_REDIS_URI not set - skipping") |
| 554 | } |
| 555 | addr := os.Getenv("TEST_REDIS_URI") |
| 556 | e, _ := NewRedisExporter( |
| 557 | addr, |
| 558 | Options{Namespace: "test", CheckSingleStreams: dbNumStrFull + "=" + TestKeyNameStream, StreamsExcludeConsumerMetrics: true}, |
| 559 | ) |
| 560 | c, err := redis.DialURL(addr) |
| 561 | if err != nil { |
| 562 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 563 | } |
| 564 | |
| 565 | setupTestKeys(t, addr) |
| 566 | defer deleteTestKeys(t, addr) |
| 567 | |
| 568 | chM := make(chan prometheus.Metric) |
| 569 | go func() { |
| 570 | e.extractStreamMetrics(chM, c) |
| 571 | close(chM) |
| 572 | }() |
| 573 | want := map[string]bool{ |
| 574 | "stream_length": false, |
| 575 | "stream_radix_tree_keys": false, |
| 576 | "stream_radix_tree_nodes": false, |
| 577 | "stream_last_generated_id": false, |
| 578 | "stream_groups": false, |
| 579 | "stream_max_deleted_entry_id": false, |
| 580 | "stream_first_entry_id": false, |
| 581 | "stream_last_entry_id": false, |
| 582 | "stream_group_consumers": false, |
| 583 | "stream_group_messages_pending": false, |
| 584 | "stream_group_last_delivered_id": false, |
| 585 | "stream_group_entries_read": false, |
| 586 | "stream_group_lag": false, |
| 587 | } |
| 588 | |
| 589 | dontWant := map[string]bool{ |
| 590 | "stream_group_consumer_messages_pending": false, |
| 591 | "stream_group_consumer_idle_seconds": false, |
| 592 | } |
| 593 | |
| 594 | for m := range chM { |
| 595 | for k := range want { |
| 596 | log.Debugf("metric: %s", m.Desc().String()) |
| 597 | log.Debugf("want: %s", k) |
| 598 | |
| 599 | if strings.Contains(m.Desc().String(), k) { |
| 600 | want[k] = true |
| 601 | } |
| 602 | } |
| 603 | for k := range dontWant { |
| 604 | log.Debugf("metric: %s", m.Desc().String()) |
| 605 | log.Debugf("don't want: %s", k) |
| 606 | |
| 607 | if strings.Contains(m.Desc().String(), k) { |
| 608 | dontWant[k] = true |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | for k, found := range want { |
| 614 | if !found { |
| 615 | t.Errorf("didn't find %s metric, which should be collected", k) |
| 616 | } |
| 617 | } |
| 618 | for k, found := range dontWant { |
| 619 | if found { |
| 620 | t.Errorf("found %s metric, which shouldn't be collected", k) |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | func TestClusterStreamMetricsExtraction(t *testing.T) { |
| 626 | if os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") == "" { |
| 627 | t.Skipf("TEST_REDIS_CLUSTER_MASTER_URI not set - skipping cluster stream test") |
| 628 | } |
| 629 | |
| 630 | clusterURI := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") |
| 631 | |
| 632 | // Test streams to create |
| 633 | testStreams := []string{"audit_stream", "sa_audit_stream", "test_stream_cluster"} |
| 634 | |
| 635 | // Setup cluster connection to create test streams |
| 636 | // Use cluster-aware connection to avoid MOVED errors |
| 637 | tempExporter, err := NewRedisExporter(clusterURI, Options{IsCluster: true}) |
| 638 | if err != nil { |
| 639 | t.Fatalf("Couldn't create temp exporter for cluster setup: %v", err) |
| 640 | } |
| 641 | |
| 642 | c, err := tempExporter.connectToRedisCluster() |
| 643 | if err != nil { |
| 644 | t.Fatalf("Couldn't connect to cluster: %v", err) |
| 645 | } |
| 646 | defer c.Close() |
| 647 | |
| 648 | // Create test streams with some data using cluster connection |
| 649 | for _, streamName := range testStreams { |
| 650 | // Add entries to streams - cluster connection handles MOVED redirects automatically |
| 651 | _, err = c.Do("XADD", streamName, "*", "field1", "value1", "field2", "value2") |
| 652 | if err != nil { |
| 653 | t.Logf("Warning: couldn't create stream %s: %v", streamName, err) |
| 654 | continue |
| 655 | } |
| 656 | _, err = c.Do("XADD", streamName, "*", "field3", "value3") |
| 657 | if err != nil { |
| 658 | t.Logf("Warning: couldn't add to stream %s: %v", streamName, err) |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // Cleanup function - use the same cluster connection to avoid MOVED errors |
| 663 | defer func() { |
| 664 | for _, streamName := range testStreams { |
| 665 | _, err := c.Do("DEL", streamName) |
| 666 | if err != nil { |
| 667 | t.Logf("Warning: couldn't clean up stream %s: %v", streamName, err) |
| 668 | } |
| 669 | } |
| 670 | }() |
| 671 | |
| 672 | // Create exporter with cluster mode and single streams config |
| 673 | // This reproduces the exact command from the GitHub issue: |
| 674 | // redis_exporter --check-single-streams=audit,sa_audit --is-cluster=true |
| 675 | streamConfig := "db0=audit_stream,db0=sa_audit_stream,db0=test_stream_cluster" |
| 676 | |
| 677 | e, err := NewRedisExporter( |
| 678 | clusterURI, |
| 679 | Options{ |
| 680 | Namespace: "test", |
| 681 | CheckSingleStreams: streamConfig, |
| 682 | IsCluster: true, |
| 683 | }, |
| 684 | ) |
| 685 | if err != nil { |
| 686 | t.Fatalf("NewRedisExporter() err: %s", err) |
| 687 | } |
| 688 | |
| 689 | // Test the full HTTP endpoint (this tests the complete path including cluster connection fix) |
| 690 | ts := httptest.NewServer(e) |
| 691 | defer ts.Close() |
| 692 | |
| 693 | metricsOutput := downloadURL(t, ts.URL+"/metrics") |
| 694 | |
| 695 | // Check if we got HTML instead of metrics (indicates an error during metrics collection) |
| 696 | if strings.Contains(metricsOutput, "<html>") { |
| 697 | t.Logf("Got HTML response instead of metrics, this indicates an error during metrics collection") |
| 698 | t.Logf("This could be due to Redis connection issues or cluster MOVED errors") |
| 699 | t.Logf("First 500 chars of response: %.500s...", metricsOutput) |
| 700 | t.Fatal("Expected Prometheus metrics but got HTML error page - check Redis cluster connectivity") |
| 701 | } |
| 702 | |
| 703 | // Parse the metrics output to find stream metrics |
| 704 | foundMetrics := make(map[string]bool) |
| 705 | lines := strings.SplitSeq(metricsOutput, "\n") |
| 706 | |
| 707 | for line := range lines { |
| 708 | // Look for stream_length metrics with our test streams |
| 709 | if strings.Contains(line, "stream_length") { |
| 710 | for _, streamName := range testStreams { |
| 711 | if strings.Contains(line, `stream="`+streamName+`"`) { |
| 712 | foundMetrics[streamName] = true |
| 713 | t.Logf("Found stream metric for: %s", streamName) |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // Verify that we found metrics for our test streams |
| 720 | // This ensures that the cluster MOVED errors are properly handled |
| 721 | expectedStreams := 0 |
| 722 | for _, streamName := range testStreams { |
| 723 | if foundMetrics[streamName] { |
| 724 | expectedStreams++ |
| 725 | t.Logf("✓ Successfully found metrics for stream: %s", streamName) |
| 726 | } else { |
| 727 | t.Logf("⚠ Did not find metrics for stream: %s", streamName) |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if expectedStreams == 0 { |
| 732 | t.Error("Expected to find metrics for at least one test stream in cluster mode") |
| 733 | t.Error("This indicates that the cluster MOVED error fix may not be working properly") |
| 734 | |
| 735 | // Additional debugging info |
| 736 | t.Logf("Test streams created: %v", testStreams) |
| 737 | t.Logf("Found stream metrics for: %v", foundMetrics) |
| 738 | |
| 739 | // Show sample of metrics output for debugging |
| 740 | sampleLines := strings.Split(metricsOutput, "\n") |
| 741 | t.Log("Sample metrics output (first 10 lines):") |
| 742 | for i, line := range sampleLines { |
| 743 | if i >= 10 || line == "" { |
| 744 | break |
| 745 | } |
| 746 | t.Logf(" %s", line) |
| 747 | } |
| 748 | } else { |
| 749 | t.Logf("✓ SUCCESS: Found stream metrics for %d/%d streams in cluster mode", expectedStreams, len(testStreams)) |
| 750 | t.Logf("This confirms that Redis cluster MOVED errors are properly handled for streams") |
| 751 | t.Logf("HTTP endpoint successfully returned metrics without cluster MOVED errors") |
| 752 | } |
| 753 | } |