Debian Codegraph @

exporter/keys_test.go

raw symbols SCIP
1 package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
5 "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/"
6 "net/urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/"
7 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
8 "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/"
9 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
10 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
11 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
12
13 "github.com/gomodule/redigo/redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/"
14 "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/"
15 logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/ "github.com/sirupsen/logrusscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/"
16 )
17
18 // defaultCount is used for `SCAN whatever COUNT defaultCount` command
19 const (
20 defaultCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/defaultCount. int64 = 10
21 invalidCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/invalidCount. int64 = 0
22 )
23
24 func TestKeyValuesAndSizesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyValuesAndSizes().(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#) {
25 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
26 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"),
27 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
28 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
29 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull. + "=" + urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/QueryEscape().(testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
30 },
31 )
32 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
33 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
34
35 setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, 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"))
36 defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, 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"))
37
38 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
39 for _, want := range []string{
40 "test_key_size",
41 "test_key_value",
42 } {
43 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().(body, want) {
44 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("didn't find %s, body: %s", want, body)
45 return
46 }
47 }
48 }
49
50 func TestKeyValuesAsLabelscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyValuesAsLabel().(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#) {
51 setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, 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"))
52 defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, 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"))
53
54 for _, exc := range []bool{true, false} {
55 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
56 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"),
57 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
58 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
59 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull. + "=" + urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/QueryEscape().(TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString.),
60 DisableExportingKeyValuesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#DisableExportingKeyValues.: exc,
61 },
62 )
63 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
64
65 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
66 for _, match := range []string{
67 "key_value_as_string",
68 "test_key_value",
69 } {
70 if exc && 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().(body, match) {
71 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("didn't expect %s with DisableExportingKeyValues enabled, body: %s", match, body)
72 } else if !exc && !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().(body, match) {
73 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("didn't find %s with DisableExportingKeyValues disabled, body: %s", match, body)
74 }
75 }
76 ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
77 }
78 }
79
80 func TestClusterKeyValuesAndSizesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestClusterKeyValuesAndSizes().(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#) {
81 clusterUri := 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_CLUSTER_MASTER_URI")
82 if clusterUri == "" {
83 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestClusterKeyValuesAndSizes, don't have env var TEST_REDIS_CLUSTER_MASTER_URI")
84 }
85 setupTestKeysClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeysCluster().(t, clusterUri)
86 defer deleteTestKeysClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeysCluster().(t, clusterUri)
87
88 for _, disableExportingValues := range []bool{true, false} {
89 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
90 clusterUri,
91 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
92 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", DisableExportingKeyValuesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#DisableExportingKeyValues.: disableExportingValues,
93 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(
94 "%s=%s,%s=%s",
95 dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull., urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/QueryEscape().(testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
96 dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull., urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/QueryEscape().(TestKeyNameSetscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSet.),
97 ),
98 IsClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#IsCluster.: true,
99 },
100 )
101
102 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#)
103 go func() {
104 e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM)
105 close(chM)
106 }()
107
108 foundExpectedKey := map[string]bool{
109 "test_key_size": false,
110 "test_key_value": false,
111 "test_key_memory_usage_bytes": false,
112 }
113
114 for m := range chM {
115 for k := range foundExpectedKey {
116 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().(), k) {
117 foundExpectedKey[k] = true
118 }
119 }
120 }
121 for k, found := range foundExpectedKey {
122 if k == "test_key_value" {
123 if found && disableExportingValues {
124 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("didn't expect %s with DisableExportingKeyValues enabled", k)
125 } else if !found && !disableExportingValues {
126 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("didn't find %s with DisableExportingKeyValues disabled", k)
127 }
128 } else if !found {
129 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("didn't find %s", k)
130 }
131 }
132 }
133 }
134
135 func TestParseKeyArgscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestParseKeyArg().(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#) {
136 for _, test := range []struct {
137 name string
138 keyArgs string
139 expected []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#
140 expectSuccess bool
141 }{
142 // positive tests
143 {"empty_args", "", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, true},
144 {"default_database", "my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{{"0", "my-key"}}, true},
145 {"prefixed_database", "db0=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{{"0", "my-key"}}, true},
146 {"indexed_database", "0=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{{"0", "my-key"}}, true},
147 {"triple_key", "check-key-01", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{{"0", "check-key-01"}}, true},
148 {
149 name: "default_database_multiple_keys",
150 keyArgs: "my-key1,my-key2",
151 expected: []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
152 {"0", "my-key1"},
153 {"0", "my-key2"},
154 },
155 expectSuccess: true,
156 },
157 {
158 name: "key_with_leading_space",
159 keyArgs: "my-key-noSpace, my-key-withSpace",
160 expected: []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
161 {"0", "my-key-noSpace"},
162 {"0", "my-key-withSpace"},
163 },
164 expectSuccess: true,
165 },
166 {
167 name: "key_with_spaces",
168 keyArgs: "my-key-noSpace1, my-key-withSpaces ,my-key-noSpace2",
169 expected: []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
170 {"0", "my-key-noSpace1"},
171 {"0", "my-key-withSpaces"},
172 {"0", "my-key-noSpace2"},
173 },
174 expectSuccess: true,
175 },
176 {
177 name: "different_databases",
178 keyArgs: "db0=key1,db1=key1",
179 expected: []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
180 {"0", "key1"},
181 {"1", "key1"},
182 },
183 expectSuccess: true,
184 },
185 {
186 name: "dbdb_replace",
187 keyArgs: "dbdbdb0=key1,db1=key1",
188 expected: []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
189 {"0", "key1"},
190 {"1", "key1"},
191 },
192 expectSuccess: true,
193 },
194 {
195 name: "default_database_with_another",
196 keyArgs: "key1,db1=key1",
197 expected: []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
198 {"0", "key1"},
199 {"1", "key1"},
200 },
201 expectSuccess: true,
202 },
203 {
204 "invalid_args_with_args_separator_skipped",
205 "=", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, true,
206 },
207 {
208 "empty_args_with_comma_separators_skipped",
209 ",,,my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{{"0", "my-key"}}, true,
210 },
211 {
212 "multiple_invalid_args_skipped",
213 "=,=,,0=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{{"0", "my-key"}}, true,
214 },
215 {
216 "empty_key_with_args_separator_skipped",
217 "0=", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, true,
218 },
219 {
220 "empty_database_with_args_separator_skipped",
221 "=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, true,
222 },
223 // negative tests
224 {
225 "string_database_index",
226 "wrong=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, false,
227 },
228 {
229 "prefixed_string_database_index",
230 "dbwrong=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, false,
231 },
232 {
233 "wrong_args_count",
234 "wrong=wrong=wrong", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, false,
235 },
236 {
237 "wrong_args",
238 "wrong=wrong=1", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, false,
239 },
240 {
241 "negative_database_index",
242 "db-1=my-key", []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{}, false,
243 },
244 } {
245 t.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Run().(test.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#) {
246 parsed, err := parseKeyArgscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/parseKeyArg().(test.keyArgs)
247 if test.expectSuccess && err != nil {
248 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected success for test: %s, got err: %s", test.name, err)
249 return
250 }
251 if len(parsed) != len(test.expected) {
252 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Parsed elements count don't match expected: parsed %d; expected %d", len(parsed), len(test.expected))
253 return
254 }
255 for i, pair := range test.expected {
256 if pair != parsed[i] {
257 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Parsed elements don't match expected dbKeyPair:\n parsed %#v;\nexpected %#v", parsed[i], pair)
258 return
259 }
260 }
261
262 if !test.expectSuccess && err == nil {
263 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected failure for test: %s, got no err", test.name)
264 return
265 }
266 if !test.expectSuccess && err != nil {
267 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Expected failure for test: %s, got err: %s", test.name, err)
268 return
269 }
270 })
271 }
272 }
273
274 type keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture# struct {
275 commandscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#command. string
276 keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#key. string
277 argsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#args. []any
278 }
279
280 func newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().(command string, key string, args ...any) keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture# {
281 return keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#{command, key, args}
282 }
283
284 func createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(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#, 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#, fixtures []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#) {
285 for _, f := range fixtures {
286 args := append([]any{f.keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#key.}, f.argsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#args....)
287 if _, err := c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().(f.commandscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#command., args...); err != nil {
288 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Error creating fixture: %#v, %#v", f, err)
289 }
290 }
291 }
292
293 func deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(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#, 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#, fixtures []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#) {
294 for _, f := range fixtures {
295 if _, err := c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("DEL", f.keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#key.); err != nil {
296 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error deleting fixture: %#v, %#v", f, err)
297 }
298 }
299 }
300
301 func TestScanKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestScanKeys().(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#) {
302 numKeys := 1000
303 var fixtures []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#
304
305 // Make 1000 keys that match
306 for i := range numKeys {
307 key := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("get_keys_test_shouldmatch_%v", i)
308 fixtures = append(fixtures, newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", key, "Woohoo!"))
309 }
310
311 // And 1000 that don't
312 for i := range numKeys {
313 key := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("get_keys_test_shouldnotmatch_%v", i)
314 fixtures = append(fixtures, newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", key, "Rats!"))
315 }
316
317 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")
318 db := dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr.
319
320 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)
321 if err != nil {
322 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Couldn't connect to %#v: %#v", addr, err)
323 }
324 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", db)
325 if err != nil {
326 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", db)
327 }
328
329 defer func() {
330 deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(t, c, fixtures)
331 c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().()
332 }()
333
334 createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(t, c, fixtures)
335
336 matches, 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().(scanKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/scanKeys().(c, "get_keys_test_*shouldmatch*", defaultCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/defaultCount.))
337 if err != nil {
338 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error getting keys matching a pattern: %#v", err)
339 }
340
341 numMatches := len(matches)
342 if numMatches != numKeys {
343 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected %#v matches, got %#v.", numKeys, numMatches)
344 }
345
346 for _, match := range matches {
347 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(match, "get_keys_test_shouldmatch") {
348 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected match to have prefix: get_keys_test_shouldmatch")
349 }
350 }
351
352 // Test expected errors separately
353 invalidFixtures := map[string]int64{
354 // empty string is a string after all
355 "": 100,
356 "pattern": invalidCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/invalidCount.,
357 }
358 for pattern, count := range invalidFixtures {
359 got, 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().(scanKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/scanKeys().(c, pattern, count))
360 if err != nil {
361 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("\"Passed\" expected, got error: %#v", err)
362 if pattern == "" && err.Error() != "pattern shouldn't be empty" {
363 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("\"Empty pattern\" error message expected, but got: %s", err.Error())
364 }
365 } else {
366 if len(got) >= 0 {
367 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error expected, got valid response: %#v", got)
368 }
369 }
370 }
371 }
372
373 func TestGetKeysFromPatternsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestGetKeysFromPatterns().(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#) {
374 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")
375 dbMain := dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr.
376 dbAlt := altDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/altDBNumStr.
377 dbInvalid := invalidDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/invalidDBNumStr.
378
379 dbMainFixtures := []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#{
380 newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", "dbMainNoPattern1", "woohoo!"),
381 newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", "dbMainSomePattern1", "woohoo!"),
382 newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", "dbMainSomePattern2", "woohoo!"),
383 }
384
385 dbAltFixtures := []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#{
386 newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", "dbAltNoPattern1", "woohoo!"),
387 newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", "dbAltSomePattern1", "woohoo!"),
388 newKeyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newKeyFixture().("SET", "dbAltSomePattern2", "woohoo!"),
389 }
390
391 keys := []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
392 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbMain, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbMainNoPattern1"},
393 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbMain, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "*SomePattern*"},
394 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbAlt, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbAltNoPattern1"},
395 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbAlt, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "*SomePattern*"},
396 }
397 invalidKeys := []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
398 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbInvalid, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "someUnusedPattern*"},
399 }
400
401 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)
402 if err != nil {
403 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Couldn't connect to %#v: %#v", addr, err)
404 }
405
406 defer func() {
407 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", dbMain)
408 if err != nil {
409 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", dbMain)
410 }
411 deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(t, c, dbMainFixtures)
412
413 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", dbAlt)
414 if err != nil {
415 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", dbAlt)
416 }
417 deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(t, c, dbAltFixtures)
418 c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().()
419 }()
420
421 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", dbMain)
422 if err != nil {
423 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", dbMain)
424 }
425 createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(t, c, dbMainFixtures)
426
427 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", dbAlt)
428 if err != nil {
429 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", dbAlt)
430 }
431 createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(t, c, dbAltFixtures)
432
433 expandedKeys, err := getKeysFromPatternsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getKeysFromPatterns().(c, keys, defaultCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/defaultCount.)
434 if err != nil {
435 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error getting keys from patterns: %#v", err)
436 }
437
438 expectedKeys := []dbKeyPairscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#{
439 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbMain, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbMainNoPattern1"},
440 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbMain, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbMainSomePattern1"},
441 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbMain, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbMainSomePattern2"},
442 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbAlt, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbAltNoPattern1"},
443 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbAlt, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbAltSomePattern1"},
444 {dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db.: dbAlt, keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.: "dbAltSomePattern2"},
445 }
446
447 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(expectedKeys, func(i, j int) bool {
448 return (expectedKeys[i].dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db. + expectedKeys[i].keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.) < (expectedKeys[j].dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db. + expectedKeys[j].keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.)
449 })
450
451 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(expandedKeys, func(i, j int) bool {
452 return (expandedKeys[i].dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db. + expandedKeys[i].keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.) < (expandedKeys[j].dbscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#db. + expandedKeys[j].keyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbKeyPair#key.)
453 })
454
455 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().(expectedKeys, expandedKeys) {
456 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("When expanding keys:\nexpected: %#v\nactual: %#v", expectedKeys, expandedKeys)
457 }
458
459 got, err := getKeysFromPatternsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getKeysFromPatterns().(c, invalidKeys, defaultCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/defaultCount.)
460 if err != nil {
461 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Expected error - \"invalid DB\": %#v", err)
462 } else {
463 if len(got) != 0 {
464 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error expected with invalid database %#v, got valid response: %#v", invalidKeys, got)
465 }
466 }
467 }
468
469 func TestKeySizeListscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeySizeList().(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#) {
470 s := dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull. + "=" + testKeysListscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeysList.[0]
471 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
472 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"),
473 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: s},
474 )
475
476 setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, 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"))
477 defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, 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"))
478
479 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#)
480 go func() {
481 e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM)
482 close(chM)
483 }()
484
485 found := false
486 for m := range chM {
487 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_size") {
488 found = true
489 }
490 }
491
492 if !found {
493 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("didn't find the key")
494 }
495 }
496
497 func TestKeyValueInvalidDBscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyValueInvalidDB().(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#) {
498 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
499 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"),
500 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
501 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
502 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: "999=" + urlscip-go gomod github.com/golang/go/src go1.25.0 `net/url`/.QueryEscapescip-go gomod github.com/golang/go/src go1.25.0 `net/url`/QueryEscape().(testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
503 },
504 )
505
506 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#)
507 go func() {
508 e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM)
509 close(chM)
510 }()
511
512 dontWant := map[string]bool{"test_key_size": false}
513 for m := range chM {
514 switch m.(type) {
515 case prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Gaugescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Gauge#:
516 for k := range dontWant {
517 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().(), k) {
518 logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/.Printlnscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/Println().(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().())
519 dontWant[k] = true
520 }
521 }
522 default:
523 logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/.Debugfscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/Debugf().("default: m: %#v", m)
524 }
525 }
526 for k, found := range dontWant {
527 if found {
528 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("we found %s but it shouldn't be there", k)
529 }
530 }
531 }
532
533 func TestCheckKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestCheckKeys().(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#) {
534 for _, tst := range []struct {
535 SingleCheckKey string
536 CheckKeys string
537 ExpectSuccess bool
538 }{
539 {"", "", true},
540 {"db1=key3", "", true},
541 {"check-key-01", "", true},
542 {"", "check-key-02", true},
543 {"wrong=wrong=1", "", false},
544 {"", "wrong=wrong=2", false},
545 } {
546 _, err := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(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"), Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: tst.SingleCheckKey, CheckKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckKeys.: tst.CheckKeys})
547 if tst.ExpectSuccess && err != nil {
548 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected success for test: %#v, got err: %s", tst, err)
549 return
550 }
551
552 if !tst.ExpectSuccess && err == nil {
553 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected failure for test: %#v, got no err", tst)
554 return
555 }
556 }
557 }
558
559 func TestCheckSingleKeyDefaultsTo0scip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestCheckSingleKeyDefaultsTo0().(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#) {
560 uri := 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")
561 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(uri, Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test", CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: "single"})
562 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
563 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
564
565 setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, uri)
566 defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, uri)
567
568 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
569 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().(body, `test_key_size{db="db0",key="single"} 0`) {
570 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected metric `test_key_size` with key=`single` and value 0 but got:\n%s", body)
571 }
572 }
573
574 func TestCheckKeysMultipleDBsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestCheckKeysMultipleDBs().(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#) {
575 uri := 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")
576 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(uri,
577 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
578 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: "single," +
579 dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr. + "=" + testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0] + "," +
580 dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr. + "=" + TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString. + "," +
581 altDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/altDBNumStr. + "=" + TestKeysHllNamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeysHllName. + "," +
582 altDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/altDBNumStr. + "=" + TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString. + "," +
583 anotherAltDbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/anotherAltDbNumStr. + "=" + testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0],
584 CheckKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckKeys.: dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr. + "=" + "test*",
585 CheckKeysBatchSizescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckKeysBatchSize.: 1000,
586 })
587 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
588 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
589
590 setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, uri)
591 defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, uri)
592
593 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
594
595 for _, k := range []string{
596 `test_key_size{db="db0",key="single"} 0`, // non-existent key
597
598 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_size{db="db%s",key="%s"} 16`, dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr., TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString.),
599 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_size{db="db%s",key="%s"} 2`, dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr., TestKeysZSetNamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeysZSetName.),
600 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_size{db="db%s",key="%s"} 4`, dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr., TestKeysHashNamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeysHashName.),
601 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_size{db="db%s",key="%s"} 3`, altDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/altDBNumStr., TestKeysHllNamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeysHllName.),
602 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_size{db="db%s",key="%s"} 16`, altDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/altDBNumStr., TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString.),
603 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_size{db="db%s",key="%s"} 7`, anotherAltDbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/anotherAltDbNumStr., testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
604
605 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_value{db="db%s",key="%s"} 1234.56`, dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr., testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
606 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`test_key_value{db="db%s",key="%s"} 1234.56`, anotherAltDbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/anotherAltDbNumStr., testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
607
608 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`key_memory_usage_bytes{db="db%s",key="%s"}`, dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr., TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString.),
609 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`key_memory_usage_bytes{db="db%s",key="%s"}`, altDBNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/altDBNumStr., TestKeyNameSingleStringscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestKeyNameSingleString.),
610 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(`key_memory_usage_bytes{db="db%s",key="%s"}`, anotherAltDbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/anotherAltDbNumStr., testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[0]),
611 } {
612 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().(body, k) {
613 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected metric: %s but got:\n%s", k, body)
614 }
615 }
616 }
617
618 func TestClusterGetKeyInfoscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestClusterGetKeyInfo().(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#) {
619 clusterUri := 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_CLUSTER_MASTER_URI")
620 if clusterUri == "" {
621 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestClusterKeyValuesAndSizes, don't have env var TEST_REDIS_CLUSTER_MASTER_URI")
622 }
623
624 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
625 clusterUri,
626 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
627 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
628 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(AllTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/AllTestKeys., ","),
629 IsClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#IsCluster.: true,
630 },
631 )
632 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
633 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
634
635 setupTestKeysClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeysCluster().(t, clusterUri)
636 defer deleteTestKeysClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeysCluster().(t, clusterUri)
637
638 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
639 for _, want := range []string{
640 "key_value_as_string",
641 `test_key_size{db="db0",key="test-hll"} 3`,
642 } {
643 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().(body, want) {
644 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected metric: %s but got:\n%s", want, body)
645 }
646 }
647 }
648
649 func TestRedis88ClusterArrayGetKeyInfoscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestRedis88ClusterArrayGetKeyInfo().(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#) {
650 clusterUri := 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_REDIS88_CLUSTER_MASTER_URI")
651 if clusterUri == "" {
652 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestRedis88ClusterGetKeyInfo, don't have env var TEST_REDIS88_CLUSTER_MASTER_URI")
653 }
654
655 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
656 clusterUri,
657 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
658 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
659 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: "array_test,events:1",
660 IsClusterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#IsCluster.: true,
661 },
662 )
663 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
664 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
665
666 fixtures := []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#{
667 {"ARMSET", "array_test", []any{"0", "10", "5", "20", "100", "30"}},
668 {"ARSET", "events:1", []any{"0", "login", "click", "purchase", "logout"}},
669 }
670
671 c := getClusterConnscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getClusterConn().(t, clusterUri)
672
673 createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(t, c, fixtures)
674
675 defer func() {
676 deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(t, c, fixtures)
677 c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().()
678 }()
679
680 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
681 for _, want := range []string{
682 `test_key_size{db="db0",key="array_test"} 3`,
683 `test_key_size{db="db0",key="events:1"} 4`,
684 } {
685 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().(body, want) {
686 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected metric: %s but got:\n%s", want, body)
687 }
688 }
689 }
690
691 func TestGetKeyInfoWithMissingKeyscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestGetKeyInfoWithMissingKey().(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#) {
692 /*
693 https://github.com/oliver006/redis_exporter/issues/1008
694
695 CheckSingleKeys contains a bunch of keys but the first one is missing
696 and the rest is still expected to be present in the metrics
697 */
698
699 uri := 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")
700 if uri == "" {
701 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestGetKeyInfoWithMissingKey, don't have env var TEST_REDIS_URI")
702 }
703
704 keys := []string{dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull. + "=" + "i-dont-exist"}
705 for idx := range testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys. {
706 keys = append(keys, dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull.+"="+testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys.[idx])
707 if idx == 3 {
708 keys = append(keys, dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull.+"="+"another-missing-key")
709 }
710 }
711
712 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
713 uri,
714 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
715 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
716 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(keys, ","),
717 },
718 )
719 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
720 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
721
722 setupTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupTestKeys().(t, uri)
723 defer deleteTestKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteTestKeys().(t, uri)
724
725 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
726 for _, k := range testKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/testKeys. {
727 for _, w := range []string{
728 `test_key_memory_usage_bytes{db="%s",key="%s"}`,
729 `test_key_size{db="%s",key="%s"}`,
730 `test_key_value{db="%s",key="%s"}`,
731 } {
732 want := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(w, dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull., k)
733 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().(body, want) {
734 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected metric: %s but got:\n%s", want, body)
735 }
736 }
737 }
738 }
739
740 func TestGetKeysCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestGetKeysCount().(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#) {
741 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")
742 db := dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr.
743
744 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)
745 if err != nil {
746 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Couldn't connect to %#v: %#v", addr, err)
747 }
748 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", db)
749 if err != nil {
750 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", db)
751 }
752
753 fixtures := []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#{
754 {"SET", "count_test:keys_count_test_string1", []any{"Woohoo!"}},
755 {"SET", "count_test:keys_count_test_string2", []any{"!oohooW"}},
756 {"LPUSH", "count_test:keys_count_test_list1", []any{"listval1", "listval2", "listval3"}},
757 {"LPUSH", "count_test:keys_count_test_list2", []any{"listval1", "listval2", "listval3"}},
758 {"LPUSH", "count_test:keys_count_test_list3", []any{"listval1", "listval2", "listval3"}},
759 }
760
761 createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(t, c, fixtures)
762 defer func() {
763 deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(t, c, fixtures)
764 c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().()
765 }()
766
767 expectedCount := map[string]int{
768 "count_test:keys_count_test_string*": 2,
769 "count_test:keys_count_test_list*": 3,
770 "count_test:*": 5,
771 }
772
773 for pattern, count := range expectedCount {
774 actualCount, err := getKeysCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getKeysCount().(c, pattern, defaultCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/defaultCount.)
775 if err != nil {
776 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error getting count for pattern \"%#v\"", pattern)
777 }
778
779 if actualCount != count {
780 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Wrong count for pattern \"%#v\". Expected: %#v; Actual: %#v", pattern, count, actualCount)
781 }
782 }
783
784 got, err := getKeysCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getKeysCount().(c, "pattern", invalidCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/invalidCount.)
785 if err != nil {
786 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("Expected error - \"error retrieving keys\": %#v", err)
787 } else {
788 if got >= 0 {
789 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Error expected with invalidCount option \"%#v\", got valid response: %#v", invalidCountscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/invalidCount., got)
790 }
791 }
792 }
793
794 func TestRedis88ArrayGetKeyInfoscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestRedis88ArrayGetKeyInfo().(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#) {
795 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_REDIS88_URI")
796 if addr == "" {
797 t.Skipfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Skipf().("Skipping TestRedis88ArrayGetKeyInfo, don't have env var TEST_REDIS88_URI")
798 }
799 db := dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr.
800
801 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)
802 if err != nil {
803 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("Couldn't connect to %#v: %#v", addr, err)
804 }
805 _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SELECT", db)
806 if err != nil {
807 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Couldn't select database %#v", db)
808 }
809
810 fixtures := []keyFixturescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/keyFixture#{
811 {"ARMSET", "array_test", []any{"0", "10", "5", "20", "100", "30"}},
812 }
813
814 createKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/createKeyFixtures().(t, c, fixtures)
815
816 defer func() {
817 deleteKeyFixturesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/deleteKeyFixtures().(t, c, fixtures)
818 c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().()
819 }()
820
821 e, _ := NewRedisExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/NewRedisExporter().(
822 addr,
823 Optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#{
824 Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace.: "test",
825 CheckSingleKeysscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#CheckSingleKeys.: dbNumStrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStr. + "=" + "array_test",
826 },
827 )
828 ts := httptestscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/.NewServerscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/NewServer().(e)
829 defer ts.Closescip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#Close().()
830
831 body := downloadURLscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/downloadURL().(t, ts.URLscip-go gomod github.com/golang/go/src go1.25.0 `net/http/httptest`/Server#URL.+"/metrics")
832 for _, want := range []string{
833 `test_key_size{db="` + dbNumStrFullscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/dbNumStrFull. + `",key="array_test"} 3`,
834 } {
835 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().(body, want) {
836 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Expected metric: %s but got:\n%s", want, body)
837 }
838 }
839 }