| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "math" |
| 6 | "os" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/gomodule/redigo/redis" |
| 12 | "github.com/prometheus/client_golang/prometheus" |
| 13 | dto "github.com/prometheus/client_model/go" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | latencyTestTimeToSleepInMillis = 200 |
| 18 | ) |
| 19 | |
| 20 | func TestLatencySpike(t *testing.T) { |
| 21 | e := getTestExporter() |
| 22 | |
| 23 | setupLatency(t, os.Getenv("TEST_REDIS_URI")) |
| 24 | defer resetLatency(t, os.Getenv("TEST_REDIS_URI")) |
| 25 | |
| 26 | chM := make(chan prometheus.Metric) |
| 27 | go func() { |
| 28 | e.Collect(chM) |
| 29 | close(chM) |
| 30 | }() |
| 31 | |
| 32 | for m := range chM { |
| 33 | if strings.Contains(m.Desc().String(), "latency_spike_duration_seconds") { |
| 34 | got := &dto.Metric{} |
| 35 | m.Write(got) |
| 36 | |
| 37 | // The metric value is in seconds, but our sleep interval is specified |
| 38 | // in milliseconds, so we need to convert |
| 39 | val := got.GetGauge().GetValue() * 1000 |
| 40 | // Because we're dealing with latency, there might be a slight delay |
| 41 | // even after sleeping for a specific amount of time so checking |
| 42 | // to see if we're between +-5 of our expected value |
| 43 | if math.Abs(float64(latencyTestTimeToSleepInMillis)-val) > 5 { |
| 44 | t.Errorf("values not matching, %f != %f", float64(latencyTestTimeToSleepInMillis), val) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | resetLatency(t, os.Getenv("TEST_REDIS_URI")) |
| 50 | |
| 51 | chM = make(chan prometheus.Metric) |
| 52 | go func() { |
| 53 | e.Collect(chM) |
| 54 | close(chM) |
| 55 | }() |
| 56 | |
| 57 | for m := range chM { |
| 58 | switch m := m.(type) { |
| 59 | case prometheus.Gauge: |
| 60 | if strings.Contains(m.Desc().String(), "latency_spike_duration_seconds") { |
| 61 | t.Errorf("latency threshold was not reset") |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func setupLatency(t *testing.T, addr string) error { |
| 68 | c, err := redis.DialURL(addr) |
| 69 | if err != nil { |
| 70 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 71 | return err |
| 72 | } |
| 73 | defer c.Close() |
| 74 | |
| 75 | _, err = c.Do("CONFIG", "SET", "LATENCY-MONITOR-THRESHOLD", 100) |
| 76 | if err != nil { |
| 77 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // Have to pass in the sleep time in seconds so we have to divide |
| 82 | // the number of milliseconds by 1000 to get number of seconds |
| 83 | _, err = c.Do("DEBUG", "SLEEP", latencyTestTimeToSleepInMillis/1000.0) |
| 84 | if err != nil { |
| 85 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | time.Sleep(time.Millisecond * 50) |
| 90 | |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | func resetLatency(t *testing.T, addr string) error { |
| 95 | c, err := redis.DialURL(addr) |
| 96 | if err != nil { |
| 97 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 98 | return err |
| 99 | } |
| 100 | defer c.Close() |
| 101 | |
| 102 | _, err = c.Do("LATENCY", "RESET") |
| 103 | if err != nil { |
| 104 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | time.Sleep(time.Millisecond * 50) |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | func TestLatencyHistogram(t *testing.T) { |
| 114 | addr := os.Getenv("TEST_REDIS_URI") |
| 115 | |
| 116 | // Since Redis 7.0.0 we should have latency histogram stats |
| 117 | e := getTestExporterWithAddr(addr) |
| 118 | setupTestKeys(t, addr) |
| 119 | |
| 120 | want := map[string]bool{"commands_latencies_usec": false} |
| 121 | commandStatsCheck(t, e, want) |
| 122 | deleteTestKeys(t, addr) |
| 123 | } |
| 124 | |
| 125 | func TestExtractTotalUsecForCommand(t *testing.T) { |
| 126 | statsOutString := `# Commandstats |
| 127 | cmdstat_testerr|1:calls=1,usec_per_call=5.00,rejected_calls=0,failed_calls=0 |
| 128 | cmdstat_testerr:calls=1,usec=2,usec_per_call=5.00,rejected_calls=0,failed_calls=0 |
| 129 | cmdstat_testerr2:calls=1,usec=-2,usec_per_call=5.00,rejected_calls=0,failed_calls=0 |
| 130 | cmdstat_testerr3:calls=1,usec=` + fmt.Sprintf("%d1", uint64(math.MaxUint64)) + `,usec_per_call=5.00,rejected_calls=0,failed_calls=0 |
| 131 | cmdstat_config|get:calls=69103,usec=15005068,usec_per_call=217.14,rejected_calls=0,failed_calls=0 |
| 132 | cmdstat_config|set:calls=3,usec=58,usec_per_call=19.33,rejected_calls=0,failed_calls=3 |
| 133 | |
| 134 | # Latencystats |
| 135 | latency_percentiles_usec_pubsub|channels:p50=5.023,p99=5.023,p99.9=5.023 |
| 136 | latency_percentiles_usec_config|get:p50=272.383,p99=346.111,p99.9=395.263 |
| 137 | latency_percentiles_usec_config|set:p50=23.039,p99=27.007,p99.9=27.007` |
| 138 | |
| 139 | testMap := map[string]uint64{ |
| 140 | "config|set": 58, |
| 141 | "config": 58 + 15005068, |
| 142 | "testerr|1": 0, |
| 143 | "testerr": 2 + 0, |
| 144 | "testerr2": 0, |
| 145 | "testerr3": 0, |
| 146 | } |
| 147 | |
| 148 | for cmd, expected := range testMap { |
| 149 | if res := extractTotalUsecForCommand(statsOutString, cmd); res != expected { |
| 150 | t.Errorf("Incorrect usec extracted. Expected %d but got %d!", expected, res) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func TestLatencyStats(t *testing.T) { |
| 156 | redisSevenAddr := os.Getenv("TEST_REDIS_URI") |
| 157 | |
| 158 | // Since Redis v7 we should have extended latency stats (summary of command latencies) |
| 159 | e := getTestExporterWithAddr(redisSevenAddr) |
| 160 | setupTestKeys(t, redisSevenAddr) |
| 161 | |
| 162 | want := map[string]bool{"latency_percentiles_usec": false} |
| 163 | commandStatsCheck(t, e, want) |
| 164 | deleteTestKeys(t, redisSevenAddr) |
| 165 | } |