Skip to content

Commit 3729e3e

Browse files
committed
cluster test for getKeyInfo for hll
1 parent 8f7917e commit 3729e3e

File tree

6 files changed

+79
-42
lines changed

6 files changed

+79
-42
lines changed

exporter/exporter_test.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ import (
2323
log "github.com/sirupsen/logrus"
2424
)
2525

26-
const (
27-
TestValue = 1234.56
28-
TimeToSleep = 200
29-
)
30-
3126
var (
3227
keys []string
3328
keysExpiring []string
@@ -41,9 +36,9 @@ var (
4136
)
4237

4338
const (
44-
TestSetName = "test-set"
45-
TestStreamName = "test-stream"
46-
TestHllName = "test-hll"
39+
TestKeysSetName = "test-set"
40+
TestKeysStreamName = "test-stream"
41+
TestKeysHllName = "test-hll"
4742
)
4843

4944
func getTestExporter() *Exporter {
@@ -75,16 +70,17 @@ func setupKeys(t *testing.T, c redis.Conn, dbNumStr string) error {
7570
// not failing on this one - cluster doesn't allow for SELECT so we log and ignore the error
7671
}
7772

73+
testValue := 1234.56
7874
for _, key := range keys {
79-
if _, err := c.Do("SET", key, TestValue); err != nil {
75+
if _, err := c.Do("SET", key, testValue); err != nil {
8076
t.Errorf("couldn't setup redis, err: %s ", err)
8177
return err
8278
}
8379
}
8480

8581
// set to expire in 300 seconds, should be plenty for a test run
8682
for _, key := range keysExpiring {
87-
if _, err := c.Do("SETEX", key, "300", TestValue); err != nil {
83+
if _, err := c.Do("SETEX", key, "300", testValue); err != nil {
8884
t.Errorf("couldn't setup redis, err: %s ", err)
8985
return err
9086
}
@@ -99,25 +95,25 @@ func setupKeys(t *testing.T, c redis.Conn, dbNumStr string) error {
9995
}
10096
}
10197

102-
c.Do("PFADD", TestHllName, "val1")
103-
c.Do("PFADD", TestHllName, "val22")
104-
c.Do("PFADD", TestHllName, "val333")
98+
c.Do("PFADD", TestKeysHllName, "val1")
99+
c.Do("PFADD", TestKeysHllName, "val22")
100+
c.Do("PFADD", TestKeysHllName, "val333")
105101

106-
c.Do("SADD", TestSetName, "test-val-1")
107-
c.Do("SADD", TestSetName, "test-val-2")
102+
c.Do("SADD", TestKeysSetName, "test-val-1")
103+
c.Do("SADD", TestKeysSetName, "test-val-2")
108104

109105
c.Do("SET", singleStringKey, "this-is-a-string")
110106

111107
// Create test streams
112-
c.Do("XGROUP", "CREATE", TestStreamName, "test_group_1", "$", "MKSTREAM")
113-
c.Do("XGROUP", "CREATE", TestStreamName, "test_group_2", "$", "MKSTREAM")
114-
c.Do("XADD", TestStreamName, TestStreamTimestamps[0], "field_1", "str_1")
115-
c.Do("XADD", TestStreamName, TestStreamTimestamps[1], "field_2", "str_2")
108+
c.Do("XGROUP", "CREATE", TestKeysStreamName, "test_group_1", "$", "MKSTREAM")
109+
c.Do("XGROUP", "CREATE", TestKeysStreamName, "test_group_2", "$", "MKSTREAM")
110+
c.Do("XADD", TestKeysStreamName, TestStreamTimestamps[0], "field_1", "str_1")
111+
c.Do("XADD", TestKeysStreamName, TestStreamTimestamps[1], "field_2", "str_2")
116112

117113
// Process messages to assign Consumers to their groups
118-
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", TestStreamName, ">")
119-
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", TestStreamName, ">")
120-
c.Do("XREADGROUP", "GROUP", "test_group_2", "test_consumer_1", "COUNT", "1", "STREAMS", TestStreamName, "0")
114+
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_1", "COUNT", "1", "STREAMS", TestKeysStreamName, ">")
115+
c.Do("XREADGROUP", "GROUP", "test_group_1", "test_consumer_2", "COUNT", "1", "STREAMS", TestKeysStreamName, ">")
116+
c.Do("XREADGROUP", "GROUP", "test_group_2", "test_consumer_1", "COUNT", "1", "STREAMS", TestKeysStreamName, "0")
121117

122118
return nil
123119
}
@@ -140,9 +136,9 @@ func deleteKeys(c redis.Conn, dbNumStr string) {
140136
c.Do("DEL", key)
141137
}
142138

143-
c.Do("DEL", TestHllName)
144-
c.Do("DEL", TestSetName)
145-
c.Do("DEL", TestStreamName)
139+
c.Do("DEL", TestKeysHllName)
140+
c.Do("DEL", TestKeysSetName)
141+
c.Do("DEL", TestKeysStreamName)
146142
c.Do("DEL", singleStringKey)
147143
}
148144

exporter/http_test.go

Lines changed: 1 addition & 1 deletion
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 + "=" + TestStreamName // check-single-streams
31+
css := dbNumStrFull + "=" + TestKeysStreamName // check-single-streams
3232
cntk := dbNumStrFull + "=" + keys[0] + "*" // count-keys
3333

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

exporter/keys_test.go

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,28 @@ func TestKeyValuesAsLabel(t *testing.T) {
9191
}
9292

9393
func TestClusterKeyValuesAndSizes(t *testing.T) {
94-
if os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") == "" {
94+
clusterUri := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
95+
if clusterUri == "" {
9596
t.Skipf("Skipping TestClusterKeyValuesAndSizes, don't have env var TEST_REDIS_CLUSTER_MASTER_URI")
9697
}
98+
9799
for _, exc := range []bool{true, false} {
100+
98101
e, _ := NewRedisExporter(
99-
os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI"),
102+
clusterUri,
100103
Options{
101104
Namespace: "test", DisableExportingKeyValues: exc,
102-
CheckSingleKeys: dbNumStrFull + "=" + url.QueryEscape(keys[0]),
103-
IsCluster: true},
105+
CheckSingleKeys: fmt.Sprintf(
106+
"%s=%s,%s=%s",
107+
dbNumStrFull, url.QueryEscape(keys[0]),
108+
dbNumStrFull, url.QueryEscape(TestKeysSetName),
109+
),
110+
IsCluster: true,
111+
},
104112
)
105113

106-
uri := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
107-
108-
setupDBKeysCluster(t, uri)
109-
defer deleteKeysFromDBCluster(uri)
114+
setupDBKeysCluster(t, clusterUri)
115+
defer deleteKeysFromDBCluster(clusterUri)
110116

111117
chM := make(chan prometheus.Metric)
112118
go func() {
@@ -647,6 +653,37 @@ func TestCheckSingleKeyDefaultsTo0(t *testing.T) {
647653
}
648654
}
649655

656+
func TestClusterGetKeyInfo(t *testing.T) {
657+
clusterUri := os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
658+
if clusterUri == "" {
659+
t.Skipf("Skipping TestClusterKeyValuesAndSizes, don't have env var TEST_REDIS_CLUSTER_MASTER_URI")
660+
}
661+
662+
e, _ := NewRedisExporter(
663+
clusterUri,
664+
Options{
665+
Namespace: "test",
666+
CheckSingleKeys: TestKeysHllName, Registry: prometheus.NewRegistry(),
667+
},
668+
)
669+
ts := httptest.NewServer(e)
670+
defer ts.Close()
671+
672+
setupDBKeysCluster(t, clusterUri)
673+
defer deleteKeysFromDBCluster(clusterUri)
674+
675+
chM := make(chan prometheus.Metric, 10000)
676+
go func() {
677+
e.Collect(chM)
678+
close(chM)
679+
}()
680+
681+
body := downloadURL(t, ts.URL+"/metrics")
682+
if !strings.Contains(body, `test_key_size{db="db0",key="single"} 0`) {
683+
t.Errorf("Expected metric `test_key_size` with key=`single` and value 0 but got:\n%s", body)
684+
}
685+
}
686+
650687
func TestGetKeysCount(t *testing.T) {
651688
addr := os.Getenv("TEST_REDIS_URI")
652689
db := dbNumStr

exporter/latency_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313
dto "github.com/prometheus/client_model/go"
1414
)
1515

16+
const (
17+
latencyTestTimeToSleepInMillis = 200
18+
)
19+
1620
func TestLatencySpike(t *testing.T) {
1721
e := getTestExporter()
1822

@@ -36,8 +40,8 @@ func TestLatencySpike(t *testing.T) {
3640
// Because we're dealing with latency, there might be a slight delay
3741
// even after sleeping for a specific amount of time so checking
3842
// to see if we're between +-5 of our expected value
39-
if math.Abs(float64(TimeToSleep)-val) > 5 {
40-
t.Errorf("values not matching, %f != %f", float64(TimeToSleep), val)
43+
if math.Abs(float64(latencyTestTimeToSleepInMillis)-val) > 5 {
44+
t.Errorf("values not matching, %f != %f", float64(latencyTestTimeToSleepInMillis), val)
4145
}
4246
}
4347
}
@@ -76,7 +80,7 @@ func setupLatency(t *testing.T, addr string) error {
7680

7781
// Have to pass in the sleep time in seconds so we have to divide
7882
// the number of milliseconds by 1000 to get number of seconds
79-
_, err = c.Do("DEBUG", "SLEEP", TimeToSleep/1000.0)
83+
_, err = c.Do("DEBUG", "SLEEP", latencyTestTimeToSleepInMillis/1000.0)
8084
if err != nil {
8185
t.Errorf("couldn't setup redis, err: %s ", err)
8286
return err

exporter/slowlog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func setupSlowLog(t *testing.T, addr string) error {
108108

109109
// Have to pass in the sleep time in seconds so we have to divide
110110
// the number of milliseconds by 1000 to get number of seconds
111-
_, err = c.Do("DEBUG", "SLEEP", TimeToSleep/1000.0)
111+
_, err = c.Do("DEBUG", "SLEEP", latencyTestTimeToSleepInMillis/1000.0)
112112
if err != nil {
113113
t.Errorf("couldn't setup redis, err: %s ", err)
114114
return err

exporter/streams_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestStreamsGetStreamInfo(t *testing.T) {
5656
tsts := []scanStreamFixture{
5757
{
5858
name: "Stream test",
59-
stream: TestStreamName,
59+
stream: TestKeysStreamName,
6060
streamInfo: streamInfo{
6161
Length: 2,
6262
RadixTreeKeys: 1,
@@ -123,7 +123,7 @@ func TestStreamsGetStreamInfoUsingValKey7(t *testing.T) {
123123
tsts := []scanStreamFixture{
124124
{
125125
name: "Stream test",
126-
stream: TestStreamName,
126+
stream: TestKeysStreamName,
127127
streamInfo: streamInfo{
128128
Length: 2,
129129
RadixTreeKeys: 1,
@@ -496,7 +496,7 @@ func TestStreamsExtractStreamMetrics(t *testing.T) {
496496
addr := os.Getenv("TEST_REDIS_URI")
497497
e, _ := NewRedisExporter(
498498
addr,
499-
Options{Namespace: "test", CheckSingleStreams: dbNumStrFull + "=" + TestStreamName},
499+
Options{Namespace: "test", CheckSingleStreams: dbNumStrFull + "=" + TestKeysStreamName},
500500
)
501501
c, err := redis.DialURL(addr)
502502
if err != nil {
@@ -554,7 +554,7 @@ func TestStreamsExtractStreamMetricsExcludeConsumer(t *testing.T) {
554554
addr := os.Getenv("TEST_REDIS_URI")
555555
e, _ := NewRedisExporter(
556556
addr,
557-
Options{Namespace: "test", CheckSingleStreams: dbNumStrFull + "=" + TestStreamName, StreamsExcludeConsumerMetrics: true},
557+
Options{Namespace: "test", CheckSingleStreams: dbNumStrFull + "=" + TestKeysStreamName, StreamsExcludeConsumerMetrics: true},
558558
)
559559
c, err := redis.DialURL(addr)
560560
if err != nil {

0 commit comments

Comments
 (0)