| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "reflect" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/gomodule/redigo/redis" |
| 12 | "github.com/prometheus/client_golang/prometheus" |
| 13 | dto "github.com/prometheus/client_model/go" |
| 14 | ) |
| 15 | |
| 16 | func getDBCount(c redis.Conn) (dbCount int, err error) { |
| 17 | dbCount = 16 |
| 18 | var config []string |
| 19 | if config, err = redis.Strings(doRedisCmd(c, "CONFIG", "GET", "*")); err != nil { |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | for pos := 0; pos < len(config)/2; pos++ { |
| 24 | strKey := config[pos*2] |
| 25 | strVal := config[pos*2+1] |
| 26 | |
| 27 | if strKey == "databases" { |
| 28 | if dbCount, err = strconv.Atoi(strVal); err != nil { |
| 29 | dbCount = 16 |
| 30 | } |
| 31 | return |
| 32 | } |
| 33 | } |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | type keyGroupData struct { |
| 38 | name string |
| 39 | checkKeyGroups string |
| 40 | maxDistinctKeyGroups int64 |
| 41 | wantedCount map[string]int |
| 42 | wantedMemory map[string]bool |
| 43 | wantedDistintKeyGroups int |
| 44 | } |
| 45 | |
| 46 | func TestKeyGroupMetrics(t *testing.T) { |
| 47 | if os.Getenv("TEST_REDIS_URI") == "" { |
| 48 | t.Skipf("TEST_REDIS_URI not set - skipping") |
| 49 | } |
| 50 | addr := os.Getenv("TEST_REDIS_URI") |
| 51 | c, err := redis.DialURL(addr) |
| 52 | if err != nil { |
| 53 | t.Fatalf("Couldn't connect to %#v: %#v", addr, err) |
| 54 | } |
| 55 | |
| 56 | var dbCount int |
| 57 | if dbCount, err = getDBCount(c); err != nil { |
| 58 | t.Fatalf("Couldn't get dbCount: %#v", err) |
| 59 | } |
| 60 | setupTestKeys(t, addr) |
| 61 | defer deleteTestKeys(t, addr) |
| 62 | |
| 63 | tsts := []keyGroupData{ |
| 64 | { |
| 65 | name: "synchronous with unclassified keys", |
| 66 | checkKeyGroups: "^(key_ringo)_[0-9]+$,^(key_paul)_[0-9]+$,^(key_exp)_.+$", |
| 67 | maxDistinctKeyGroups: 100, |
| 68 | // The actual counts are a function of keys (all types) being set up in the init() function |
| 69 | // and the CheckKeyGroups regexes for initializing the Redis exporter above. The count below |
| 70 | // will need to be updated if either of the aforementioned things have changed. |
| 71 | wantedCount: map[string]int{ |
| 72 | "key_ringo": 1, |
| 73 | "key_paul": 1, |
| 74 | "unclassified": 9, |
| 75 | "key_exp": 5, |
| 76 | }, |
| 77 | wantedMemory: map[string]bool{ |
| 78 | "key_ringo": true, |
| 79 | "key_paul": true, |
| 80 | "unclassified": true, |
| 81 | "key_exp": true, |
| 82 | }, |
| 83 | wantedDistintKeyGroups: 4, |
| 84 | }, |
| 85 | { |
| 86 | name: "synchronous with overflow keys", |
| 87 | checkKeyGroups: "^(.*)$", // Each key is a distinct key group |
| 88 | maxDistinctKeyGroups: 1, |
| 89 | // The actual counts depend on the largest key being set up in the init() |
| 90 | // function (test-stream at the time this code was written) and the total |
| 91 | // of keys (all types). This will need to be updated to match future |
| 92 | // updates of the init() function |
| 93 | wantedCount: map[string]int{ |
| 94 | "overflow": 15, "test-stream": 1, |
| 95 | }, |
| 96 | wantedMemory: map[string]bool{ |
| 97 | "overflow": true, "test-stream": true, |
| 98 | }, |
| 99 | wantedDistintKeyGroups: 16, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | for _, tst := range tsts { |
| 104 | t.Run(tst.name, func(t *testing.T) { |
| 105 | e, _ := NewRedisExporter( |
| 106 | addr, |
| 107 | Options{ |
| 108 | Namespace: "test", |
| 109 | CheckKeyGroups: tst.checkKeyGroups, |
| 110 | CheckKeysBatchSize: 1000, |
| 111 | MaxDistinctKeyGroups: tst.maxDistinctKeyGroups, |
| 112 | }, |
| 113 | ) |
| 114 | for { |
| 115 | chM := make(chan prometheus.Metric) |
| 116 | go func() { |
| 117 | e.extractKeyGroupMetrics(chM, c, dbCount) |
| 118 | close(chM) |
| 119 | }() |
| 120 | |
| 121 | actualCount := make(map[string]int) |
| 122 | actualMemory := make(map[string]bool) |
| 123 | actualDistinctKeyGroups := 0 |
| 124 | |
| 125 | receivedMetrics := false |
| 126 | for m := range chM { |
| 127 | receivedMetrics = true |
| 128 | got := &dto.Metric{} |
| 129 | m.Write(got) |
| 130 | |
| 131 | if strings.Contains(m.Desc().String(), "test_key_group_count") { |
| 132 | for _, label := range got.GetLabel() { |
| 133 | if *label.Name == "key_group" { |
| 134 | actualCount[*label.Value] = int(*got.Gauge.Value) |
| 135 | } |
| 136 | } |
| 137 | } else if strings.Contains(m.Desc().String(), "test_key_group_memory_usage_bytes") { |
| 138 | for _, label := range got.GetLabel() { |
| 139 | if *label.Name == "key_group" { |
| 140 | actualMemory[*label.Value] = true |
| 141 | } |
| 142 | } |
| 143 | } else if strings.Contains(m.Desc().String(), "test_number_of_distinct_key_groups") { |
| 144 | for _, label := range got.GetLabel() { |
| 145 | if *label.Name == "db" && *label.Value == "db"+dbNumStr { |
| 146 | actualDistinctKeyGroups = int(*got.Gauge.Value) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if !receivedMetrics { |
| 153 | time.Sleep(100 * time.Millisecond) |
| 154 | continue |
| 155 | } |
| 156 | if !reflect.DeepEqual(tst.wantedCount, actualCount) { |
| 157 | t.Errorf("Key group count metrics are not expected:\n Expected: %#v\nActual: %#v\n", tst.wantedCount, actualCount) |
| 158 | } |
| 159 | |
| 160 | // It's a little fragile to anticipate how much memory |
| 161 | // will be allocated for specific key groups, so we |
| 162 | // are only going to check for presence of memory usage |
| 163 | // metrics for expected key groups here. |
| 164 | if !reflect.DeepEqual(tst.wantedMemory, actualMemory) { |
| 165 | t.Errorf("Key group memory usage metrics are not expected:\n Expected: %#v\nActual: %#v\n", tst.wantedMemory, actualMemory) |
| 166 | } |
| 167 | |
| 168 | if actualDistinctKeyGroups != tst.wantedDistintKeyGroups { |
| 169 | t.Errorf("Unexpected number of distinct key groups, expected: %d, actual: %d", tst.wantedDistintKeyGroups, actualDistinctKeyGroups) |
| 170 | } |
| 171 | break |
| 172 | } |
| 173 | }) |
| 174 | } |
| 175 | } |