| 1 | package exporter |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/gomodule/redigo/redis" |
| 10 | "github.com/prometheus/client_golang/prometheus" |
| 11 | dto "github.com/prometheus/client_model/go" |
| 12 | ) |
| 13 | |
| 14 | func TestSlowLog(t *testing.T) { |
| 15 | e := getTestExporter() |
| 16 | |
| 17 | chM := make(chan prometheus.Metric) |
| 18 | go func() { |
| 19 | e.Collect(chM) |
| 20 | close(chM) |
| 21 | }() |
| 22 | |
| 23 | oldSlowLogID := float64(0) |
| 24 | |
| 25 | for m := range chM { |
| 26 | switch m := m.(type) { |
| 27 | case prometheus.Gauge: |
| 28 | if strings.Contains(m.Desc().String(), "slowlog_last_id") { |
| 29 | got := &dto.Metric{} |
| 30 | m.Write(got) |
| 31 | |
| 32 | oldSlowLogID = got.GetGauge().GetValue() |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | setupSlowLog(t, os.Getenv("TEST_REDIS_URI")) |
| 38 | defer resetSlowLog(t, os.Getenv("TEST_REDIS_URI")) |
| 39 | |
| 40 | chM = make(chan prometheus.Metric) |
| 41 | go func() { |
| 42 | e.Collect(chM) |
| 43 | close(chM) |
| 44 | }() |
| 45 | |
| 46 | for m := range chM { |
| 47 | switch m := m.(type) { |
| 48 | case prometheus.Gauge: |
| 49 | if strings.Contains(m.Desc().String(), "slowlog_last_id") { |
| 50 | got := &dto.Metric{} |
| 51 | m.Write(got) |
| 52 | |
| 53 | val := got.GetGauge().GetValue() |
| 54 | |
| 55 | if oldSlowLogID > val { |
| 56 | t.Errorf("no new slowlogs found") |
| 57 | } |
| 58 | } |
| 59 | if strings.Contains(m.Desc().String(), "slowlog_length") { |
| 60 | got := &dto.Metric{} |
| 61 | m.Write(got) |
| 62 | |
| 63 | val := got.GetGauge().GetValue() |
| 64 | if val == 0 { |
| 65 | t.Errorf("slowlog length is zero") |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | resetSlowLog(t, os.Getenv("TEST_REDIS_URI")) |
| 72 | |
| 73 | chM = make(chan prometheus.Metric) |
| 74 | go func() { |
| 75 | e.Collect(chM) |
| 76 | close(chM) |
| 77 | }() |
| 78 | |
| 79 | for m := range chM { |
| 80 | switch m := m.(type) { |
| 81 | case prometheus.Gauge: |
| 82 | if strings.Contains(m.Desc().String(), "slowlog_length") { |
| 83 | got := &dto.Metric{} |
| 84 | m.Write(got) |
| 85 | |
| 86 | val := got.GetGauge().GetValue() |
| 87 | if val != 0 { |
| 88 | t.Errorf("Slowlog was not reset") |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func setupSlowLog(t *testing.T, addr string) error { |
| 96 | c, err := redis.DialURL(addr) |
| 97 | if err != nil { |
| 98 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 99 | return err |
| 100 | } |
| 101 | defer c.Close() |
| 102 | |
| 103 | _, err = c.Do("CONFIG", "SET", "SLOWLOG-LOG-SLOWER-THAN", 10000) |
| 104 | if err != nil { |
| 105 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | // Have to pass in the sleep time in seconds so we have to divide |
| 110 | // the number of milliseconds by 1000 to get number of seconds |
| 111 | _, err = c.Do("DEBUG", "SLEEP", latencyTestTimeToSleepInMillis/1000.0) |
| 112 | if err != nil { |
| 113 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | time.Sleep(time.Millisecond * 50) |
| 118 | |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | func resetSlowLog(t *testing.T, addr string) error { |
| 123 | c, err := redis.DialURL(addr) |
| 124 | if err != nil { |
| 125 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 126 | return err |
| 127 | } |
| 128 | defer c.Close() |
| 129 | |
| 130 | _, err = c.Do("SLOWLOG", "RESET") |
| 131 | if err != nil { |
| 132 | t.Errorf("couldn't setup redis, err: %s ", err) |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | time.Sleep(time.Millisecond * 50) |
| 137 | |
| 138 | return nil |
| 139 | } |