Skip to content

Commit aab0119

Browse files
committed
run checks
1 parent 92de354 commit aab0119

12 files changed

+109
-109
lines changed

.github/workflows/tests.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ jobs:
4949
LOG_LEVEL: "info"
5050
run: |
5151
sleep 15
52-
make checks
5352
make test
5453
5554
@@ -102,6 +101,9 @@ jobs:
102101
version: v1.64
103102
args: "--tests=false"
104103

104+
- name: Run checks
105+
run: make checks
106+
105107

106108
build-stuff:
107109
runs-on: ubuntu-latest

exporter/exporter.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"net/url"
77
"runtime"
8+
"runtime/debug"
89
"strconv"
910
"strings"
1011
"sync"
@@ -726,19 +727,23 @@ func (e *Exporter) scrapeRedisHost(ch chan<- prometheus.Metric) error {
726727
e.extractLatencyMetrics(ch, infoAll, c)
727728
}
728729

729-
// skip for master?
730+
// skip these metrics for master if SkipCheckKeysForRoleMaster is set
730731
// (can help with reducing work load on the master node)
731-
if role != InstanceRoleSlave || e.options.SkipCheckKeysForRoleMaster {
732+
log.Infof("checkKeys metric collection for role: %s flag: %#v", role, e.options.SkipCheckKeysForRoleMaster)
733+
debug.PrintStack()
734+
if role == InstanceRoleSlave || !e.options.SkipCheckKeysForRoleMaster {
732735
if err := e.extractCheckKeyMetrics(ch, c); err != nil {
733736
log.Errorf("extractCheckKeyMetrics() err: %s", err)
734737
}
735-
}
736738

737-
e.extractSlowLogMetrics(ch, c)
739+
e.extractCountKeysMetrics(ch, c)
738740

739-
e.extractStreamMetrics(ch, c)
741+
e.extractStreamMetrics(ch, c)
742+
} else {
743+
log.Infof("skipping checkKeys metrics, role: %s flag: %#v", role, e.options.SkipCheckKeysForRoleMaster)
744+
}
740745

741-
e.extractCountKeysMetrics(ch, c)
746+
e.extractSlowLogMetrics(ch, c)
742747

743748
e.extractKeyGroupMetrics(ch, c, dbCount)
744749

exporter/exporter_test.go

+33-36
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@ import (
2323
log "github.com/sirupsen/logrus"
2424
)
2525

26-
var (
27-
keys []string
28-
keysExpiring []string
29-
listKeys []string
30-
singleStringKey string
31-
26+
const (
3227
dbNumStr = "11"
3328
altDBNumStr = "12"
3429
invalidDBNumStr = "16"
35-
dbNumStrFull = fmt.Sprintf("db%s", dbNumStr)
3630
)
3731

38-
const (
39-
TestKeysSetName = "test-set"
40-
TestKeysStreamName = "test-stream"
41-
TestKeysHllName = "test-hll"
32+
var (
33+
keys []string
34+
keysExpiring []string
35+
listKeys []string
36+
37+
dbNumStrFull = fmt.Sprintf("db%s", dbNumStr)
38+
)
39+
40+
var (
41+
TestKeyNameSingleString = "" // initialized with a timestamp at runtime
42+
TestKeyNameSet = "test-set"
43+
TestKeyNameStream = "test-stream"
44+
TestKeyNameHll = "test-hll"
4245
)
4346

4447
func getTestExporter() *Exporter {
@@ -95,43 +98,43 @@ func setupKeys(t *testing.T, c redis.Conn, dbNumStr string) error {
9598
}
9699
}
97100

98-
if _, err := c.Do("PFADD", TestKeysHllName, "val1"); err != nil {
101+
if _, err := c.Do("PFADD", TestKeyNameHll, "val1"); err != nil {
99102
t.Errorf("PFADD err: %s", err)
100103
return err
101104
}
102-
if _, err := c.Do("PFADD", TestKeysHllName, "val22"); err != nil {
105+
if _, err := c.Do("PFADD", TestKeyNameHll, "val22"); err != nil {
103106
t.Errorf("PFADD err: %s", err)
104107
return err
105108
}
106-
if _, err := c.Do("PFADD", TestKeysHllName, "val333"); err != nil {
109+
if _, err := c.Do("PFADD", TestKeyNameHll, "val333"); err != nil {
107110
t.Errorf("PFADD err: %s", err)
108111
return err
109112
}
110113

111-
if _, err := c.Do("SADD", TestKeysSetName, "test-val-1"); err != nil {
114+
if _, err := c.Do("SADD", TestKeyNameSet, "test-val-1"); err != nil {
112115
t.Errorf("SADD err: %s", err)
113116
return err
114117
}
115-
if _, err := c.Do("SADD", TestKeysSetName, "test-val-2"); err != nil {
118+
if _, err := c.Do("SADD", TestKeyNameSet, "test-val-2"); err != nil {
116119
t.Errorf("SADD err: %s", err)
117120
return err
118121
}
119122

120-
if _, err := c.Do("SET", singleStringKey, "this-is-a-string"); err != nil {
123+
if _, err := c.Do("SET", TestKeyNameSingleString, "this-is-a-string"); err != nil {
121124
t.Errorf("PFADD err: %s", err)
122125
return err
123126
}
124127

125128
// Create test streams
126-
c.Do("XGROUP", "CREATE", TestKeysStreamName, "test_group_1", "$", "MKSTREAM")
127-
c.Do("XGROUP", "CREATE", TestKeysStreamName, "test_group_2", "$", "MKSTREAM")
128-
c.Do("XADD", TestKeysStreamName, TestStreamTimestamps[0], "field_1", "str_1")
129-
c.Do("XADD", TestKeysStreamName, TestStreamTimestamps[1], "field_2", "str_2")
129+
c.Do("XGROUP", "CREATE", TestKeyNameStream, "test_group_1", "$", "MKSTREAM")
130+
c.Do("XGROUP", "CREATE", TestKeyNameStream, "test_group_2", "$", "MKSTREAM")
131+
c.Do("XADD", TestKeyNameStream, TestStreamTimestamps[0], "field_1", "str_1")
132+
c.Do("XADD", TestKeyNameStream, TestStreamTimestamps[1], "field_2", "str_2")
130133

131134
// Process messages to assign Consumers to their groups
132-
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", TestKeysStreamName, ">")
133-
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", TestKeysStreamName, ">")
134-
c.Do("XREADGROUP", "GROUP", "test_group_2", "test_consumer_1", "COUNT", "1", "STREAMS", TestKeysStreamName, "0")
135+
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", TestKeyNameStream, ">")
136+
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", TestKeyNameStream, ">")
137+
c.Do("XREADGROUP", "GROUP", "test_group_2", "test_consumer_1", "COUNT", "1", "STREAMS", TestKeyNameStream, "0")
135138

136139
time.Sleep(time.Millisecond * 100)
137140
return nil
@@ -155,10 +158,10 @@ func deleteKeys(c redis.Conn, dbNumStr string) {
155158
c.Do("DEL", key)
156159
}
157160

158-
c.Do("DEL", TestKeysHllName)
159-
c.Do("DEL", TestKeysSetName)
160-
c.Do("DEL", TestKeysStreamName)
161-
c.Do("DEL", singleStringKey)
161+
c.Do("DEL", TestKeyNameHll)
162+
c.Do("DEL", TestKeyNameSet)
163+
c.Do("DEL", TestKeyNameStream)
164+
c.Do("DEL", TestKeyNameSingleString)
162165
}
163166

164167
func setupDBKeys(t *testing.T, uri string) {
@@ -363,15 +366,9 @@ func TestKeysReset(t *testing.T) {
363366
setupDBKeys(t, os.Getenv("TEST_REDIS_URI"))
364367
defer deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
365368

366-
chM := make(chan prometheus.Metric, 10000)
367-
go func() {
368-
e.Collect(chM)
369-
close(chM)
370-
}()
371-
372369
body := downloadURL(t, ts.URL+"/metrics")
373370
if !strings.Contains(body, keys[0]) {
374-
t.Errorf("Did not found key %q\n%s", keys[0], body)
371+
t.Errorf("Did not find key %q\n%s", keys[0], body)
375372
}
376373

377374
deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
@@ -468,7 +465,7 @@ func init() {
468465
keys = append(keys, fmt.Sprintf("key_%s_%d", n, testTimestamp))
469466
}
470467

471-
singleStringKey = fmt.Sprintf("key_string_%d", testTimestamp)
468+
TestKeyNameSingleString = fmt.Sprintf("key_string_%d", testTimestamp)
472469

473470
listKeys = append(listKeys, "beatles_list")
474471

exporter/http_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestHTTPScrapeMetricsEndpoints(t *testing.T) {
2828
defer deleteKeysFromDB(t, os.Getenv("TEST_PWD_REDIS_URI"))
2929

3030
csk := dbNumStrFull + "=" + url.QueryEscape(keys[0]) // check-single-keys
31-
css := dbNumStrFull + "=" + TestKeysStreamName // check-single-streams
31+
css := dbNumStrFull + "=" + TestKeyNameStream // check-single-streams
3232
cntk := dbNumStrFull + "=" + keys[0] + "*" // count-keys
3333

3434
u, err := url.Parse(os.Getenv("TEST_REDIS_URI"))

exporter/info.go

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func extractPercentileVal(s string) (percentile float64, val float64, err error)
6565
return
6666
}
6767

68+
// returns the role of the instance we're scraping (master or slave)
6869
func (e *Exporter) extractInfoMetrics(ch chan<- prometheus.Metric, info string, dbCount int) string {
6970
keyValues := map[string]string{}
7071
handledDBs := map[string]bool{}

exporter/info_test.go

+48-12
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ func TestClusterMaster(t *testing.T) {
141141
ts := httptest.NewServer(e)
142142
defer ts.Close()
143143

144-
chM := make(chan prometheus.Metric, 10000)
145-
go func() {
146-
e.Collect(chM)
147-
close(chM)
148-
}()
149-
150144
body := downloadURL(t, ts.URL+"/metrics")
151145
log.Debugf("master - body: %s", body)
152146
for _, want := range []string{
@@ -159,6 +153,54 @@ func TestClusterMaster(t *testing.T) {
159153
}
160154
}
161155

156+
func TestClusterSkipCheckKeysIfMaster(t *testing.T) {
157+
uriMaster := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
158+
uriSlave := os.Getenv("TEST_REDIS_CLUSTER_SLAVE_URI")
159+
if uriMaster == "" || uriSlave == "" {
160+
t.Skipf("TEST_REDIS_CLUSTER_MASTER_URI or slave not set - skipping")
161+
}
162+
163+
setupDBKeysCluster(t, uriMaster)
164+
defer deleteKeysFromDBCluster(uriMaster)
165+
166+
for _, uri := range []string{uriMaster, uriSlave} {
167+
for _, skip := range []bool{true, false} {
168+
e, _ := NewRedisExporter(
169+
uri,
170+
Options{Namespace: "test",
171+
Registry: prometheus.NewRegistry(),
172+
CheckKeys: TestKeyNameHll,
173+
SkipCheckKeysForRoleMaster: skip,
174+
IsCluster: true,
175+
})
176+
ts := httptest.NewServer(e)
177+
178+
body := downloadURL(t, ts.URL+"/metrics")
179+
180+
expectedMetricPresent := true
181+
if skip && uri == uriMaster {
182+
expectedMetricPresent = false
183+
}
184+
t.Logf("skip: %#v uri: %s uri == uriMaster: %#v", skip, uri, uri == uriMaster)
185+
t.Logf("expectedMetricPresent: %#v", expectedMetricPresent)
186+
187+
want := `test_key_size{db="db0",key="test-hll"} 3`
188+
189+
if expectedMetricPresent {
190+
if !strings.Contains(body, want) {
191+
t.Fatalf("expectedMetricPresent but missing. metric: %s body: %s\n", want, body)
192+
}
193+
} else {
194+
if strings.Contains(body, want) {
195+
t.Fatalf("should have skipped it but found it, body:\n%s", body)
196+
}
197+
}
198+
199+
ts.Close()
200+
}
201+
}
202+
}
203+
162204
func TestClusterSlave(t *testing.T) {
163205
if os.Getenv("TEST_REDIS_CLUSTER_SLAVE_URI") == "" {
164206
t.Skipf("TEST_REDIS_CLUSTER_SLAVE_URI not set - skipping")
@@ -169,12 +211,6 @@ func TestClusterSlave(t *testing.T) {
169211
ts := httptest.NewServer(e)
170212
defer ts.Close()
171213

172-
chM := make(chan prometheus.Metric, 10000)
173-
go func() {
174-
e.Collect(chM)
175-
close(chM)
176-
}()
177-
178214
body := downloadURL(t, ts.URL+"/metrics")
179215
log.Debugf("slave - body: %s", body)
180216
for _, want := range []string{

exporter/keys.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,14 @@ func (e *Exporter) extractCheckKeyMetrics(ch chan<- prometheus.Metric, redisClie
151151
if err != nil {
152152
return fmt.Errorf("Couldn't connect to redis cluster, err: %s", err)
153153
}
154+
defer cc.Close()
155+
154156
c = cc
155-
defer c.Close()
156157
}
157158

158159
keys, err := parseKeyArg(e.options.CheckKeys)
159160
if err != nil {
160-
return fmt.Errorf("Couldn't parse check-keys: w", err)
161+
return fmt.Errorf("Couldn't parse check-keys: %w", err)
161162
}
162163
log.Debugf("keys: %#v", keys)
163164

exporter/keys_test.go

+3-27
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ func TestKeyValuesAndSizes(t *testing.T) {
3636
setupDBKeys(t, os.Getenv("TEST_REDIS_URI"))
3737
defer deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
3838

39-
chM := make(chan prometheus.Metric, 10000)
40-
go func() {
41-
e.Collect(chM)
42-
close(chM)
43-
}()
44-
4539
body := downloadURL(t, ts.URL+"/metrics")
4640
for _, want := range []string{
4741
"test_key_size",
@@ -63,18 +57,12 @@ func TestKeyValuesAsLabel(t *testing.T) {
6357
os.Getenv("TEST_REDIS_URI"),
6458
Options{
6559
Namespace: "test",
66-
CheckSingleKeys: dbNumStrFull + "=" + url.QueryEscape(singleStringKey),
60+
CheckSingleKeys: dbNumStrFull + "=" + url.QueryEscape(TestKeyNameSingleString),
6761
DisableExportingKeyValues: exc,
6862
Registry: prometheus.NewRegistry()},
6963
)
7064
ts := httptest.NewServer(e)
7165

72-
chM := make(chan prometheus.Metric, 10000)
73-
go func() {
74-
e.Collect(chM)
75-
close(chM)
76-
}()
77-
7866
body := downloadURL(t, ts.URL+"/metrics")
7967
for _, match := range []string{
8068
"key_value_as_string",
@@ -106,7 +94,7 @@ func TestClusterKeyValuesAndSizes(t *testing.T) {
10694
CheckSingleKeys: fmt.Sprintf(
10795
"%s=%s,%s=%s",
10896
dbNumStrFull, url.QueryEscape(keys[0]),
109-
dbNumStrFull, url.QueryEscape(TestKeysSetName),
97+
dbNumStrFull, url.QueryEscape(TestKeyNameSet),
11098
),
11199
IsCluster: true,
112100
},
@@ -639,12 +627,6 @@ func TestCheckSingleKeyDefaultsTo0(t *testing.T) {
639627
ts := httptest.NewServer(e)
640628
defer ts.Close()
641629

642-
chM := make(chan prometheus.Metric, 10000)
643-
go func() {
644-
e.Collect(chM)
645-
close(chM)
646-
}()
647-
648630
body := downloadURL(t, ts.URL+"/metrics")
649631
if !strings.Contains(body, `test_key_size{db="db0",key="single"} 0`) {
650632
t.Errorf("Expected metric `test_key_size` with key=`single` and value 0 but got:\n%s", body)
@@ -661,7 +643,7 @@ func TestClusterGetKeyInfo(t *testing.T) {
661643
clusterUri,
662644
Options{
663645
Namespace: "test",
664-
CheckSingleKeys: TestKeysHllName, Registry: prometheus.NewRegistry(),
646+
CheckSingleKeys: TestKeyNameHll, Registry: prometheus.NewRegistry(),
665647
IsCluster: true,
666648
},
667649
)
@@ -671,12 +653,6 @@ func TestClusterGetKeyInfo(t *testing.T) {
671653
setupDBKeysCluster(t, clusterUri)
672654
defer deleteKeysFromDBCluster(clusterUri)
673655

674-
chM := make(chan prometheus.Metric, 10000)
675-
go func() {
676-
e.Collect(chM)
677-
close(chM)
678-
}()
679-
680656
body := downloadURL(t, ts.URL+"/metrics")
681657
if !strings.Contains(body, `test_key_size{db="db0",key="test-hll"} 3`) {
682658
t.Errorf("Expected metric `test_key_size` with key=`test-hll` and value 3 but got:\n%s", body)

exporter/lua_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ func TestLuaScript(t *testing.T) {
6060
ts := httptest.NewServer(e)
6161
defer ts.Close()
6262

63-
chM := make(chan prometheus.Metric, 10000)
64-
go func() {
65-
e.Collect(chM)
66-
close(chM)
67-
}()
68-
6963
body := downloadURL(t, ts.URL+"/metrics")
7064

7165
for _, want := range tst.Wants {

0 commit comments

Comments
 (0)