| 1 | package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 5 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 6 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 9 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 10 | |
| 11 | "github.com/gomodule/redigo/redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/" |
| 12 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 13 | dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/ "github.com/prometheus/client_model/goscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/" |
| 14 | ) |
| 15 | |
| 16 | func getDBCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getDBCount().(c redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/.Connscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#) (dbCount int, err error) { |
| 17 | dbCount = 16 |
| 18 | var config []string |
| 19 | if config, err = redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/.Stringsscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Strings().(doRedisCmdscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/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 = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(strVal); err != nil { |
| 29 | dbCount = 16 |
| 30 | } |
| 31 | return |
| 32 | } |
| 33 | } |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | type keyGroupDatascip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData# struct { |
| 38 | namescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#name. string |
| 39 | checkKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#checkKeyGroups. string |
| 40 | maxDistinctKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#maxDistinctKeyGroups. int64 |
| 41 | wantedCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedCount. map[string]int |
| 42 | wantedMemoryscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedMemory. map[string]bool |
| 43 | wantedDistintKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedDistintKeyGroups. int |
| 44 | } |
| 45 | |
| 46 | func TestKeyGroupMetricsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyGroupMetrics().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 47 | if osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_REDIS_URI") == "" { |
| 48 | t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("TEST_REDIS_URI not set - skipping") |
| 49 | } |
| 50 | addr := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("TEST_REDIS_URI") |
| 51 | c, err := redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/.DialURLscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/DialURL().(addr) |
| 52 | if err != nil { |
| 53 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Couldn't connect to %#v: %#v", addr, err) |
| 54 | } |
| 55 | |
| 56 | var dbCount int |
| 57 | if dbCount, err = getDBCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getDBCount().(c); err != nil { |
| 58 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Couldn't get dbCount: %#v", err) |
| 59 | } |
| 60 | setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, addr) |
| 61 | defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, addr) |
| 62 | |
| 63 | tsts := []keyGroupDatascip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#{ |
| 64 | { |
| 65 | namescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#name.: "synchronous with unclassified keys", |
| 66 | checkKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#checkKeyGroups.: "^(key_ringo)_[0-9]+$,^(key_paul)_[0-9]+$,^(key_exp)_.+$", |
| 67 | maxDistinctKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#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 | wantedCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedCount.: map[string]int{ |
| 72 | "key_ringo": 1, |
| 73 | "key_paul": 1, |
| 74 | "unclassified": 9, |
| 75 | "key_exp": 5, |
| 76 | }, |
| 77 | wantedMemoryscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedMemory.: map[string]bool{ |
| 78 | "key_ringo": true, |
| 79 | "key_paul": true, |
| 80 | "unclassified": true, |
| 81 | "key_exp": true, |
| 82 | }, |
| 83 | wantedDistintKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedDistintKeyGroups.: 4, |
| 84 | }, |
| 85 | { |
| 86 | namescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#name.: "synchronous with overflow keys", |
| 87 | checkKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#checkKeyGroups.: "^(.*)$", // Each key is a distinct key group |
| 88 | maxDistinctKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#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 | wantedCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedCount.: map[string]int{ |
| 94 | "overflow": 15, "test-stream": 1, |
| 95 | }, |
| 96 | wantedMemoryscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedMemory.: map[string]bool{ |
| 97 | "overflow": true, "test-stream": true, |
| 98 | }, |
| 99 | wantedDistintKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedDistintKeyGroups.: 16, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | for _, tst := range tsts { |
| 104 | t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(tst.namescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#name., func(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 105 | e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().( |
| 106 | addr, |
| 107 | Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{ |
| 108 | Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", |
| 109 | CheckKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckKeyGroups.: tst.checkKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#checkKeyGroups., |
| 110 | CheckKeysBatchSizescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckKeysBatchSize.: 1000, |
| 111 | MaxDistinctKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#MaxDistinctKeyGroups.: tst.maxDistinctKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#maxDistinctKeyGroups., |
| 112 | }, |
| 113 | ) |
| 114 | for { |
| 115 | chM := make(chan prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#) |
| 116 | go func() { |
| 117 | e.extractKeyGroupMetricsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#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 := &dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.Metricscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#{} |
| 129 | m.Writescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Write().(got) |
| 130 | |
| 131 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(m.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Desc().().Stringscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#String().(), "test_key_group_count") { |
| 132 | for _, label := range got.GetLabelscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetLabel().() { |
| 133 | if *label.Namescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#Name. == "key_group" { |
| 134 | actualCount[*label.Valuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#Value.] = int(*got.Gaugescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#Gauge..Valuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Gauge#Value.) |
| 135 | } |
| 136 | } |
| 137 | } else if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(m.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Desc().().Stringscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#String().(), "test_key_group_memory_usage_bytes") { |
| 138 | for _, label := range got.GetLabelscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetLabel().() { |
| 139 | if *label.Namescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#Name. == "key_group" { |
| 140 | actualMemory[*label.Valuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#Value.] = true |
| 141 | } |
| 142 | } |
| 143 | } else if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(m.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Desc().().Stringscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc#String().(), "test_number_of_distinct_key_groups") { |
| 144 | for _, label := range got.GetLabelscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetLabel().() { |
| 145 | if *label.Namescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#Name. == "db" && *label.Valuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/LabelPair#Value. == "db"+dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr. { |
| 146 | actualDistinctKeyGroups = int(*got.Gaugescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#Gauge..Valuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Gauge#Value.) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if !receivedMetrics { |
| 153 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(100 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.) |
| 154 | continue |
| 155 | } |
| 156 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(tst.wantedCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedCount., actualCount) { |
| 157 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Key group count metrics are not expected:\n Expected: %#v\nActual: %#v\n", tst.wantedCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#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 !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(tst.wantedMemoryscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedMemory., actualMemory) { |
| 165 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Key group memory usage metrics are not expected:\n Expected: %#v\nActual: %#v\n", tst.wantedMemoryscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedMemory., actualMemory) |
| 166 | } |
| 167 | |
| 168 | if actualDistinctKeyGroups != tst.wantedDistintKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedDistintKeyGroups. { |
| 169 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Unexpected number of distinct key groups, expected: %d, actual: %d", tst.wantedDistintKeyGroupsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyGroupData#wantedDistintKeyGroups., actualDistinctKeyGroups) |
| 170 | } |
| 171 | break |
| 172 | } |
| 173 | }) |
| 174 | } |
| 175 | } |