| 1 | package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 6 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 7 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 8 | |
| 9 | "github.com/gomodule/redigo/redisscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/" |
| 10 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 11 | dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/ "github.com/prometheus/client_model/goscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/" |
| 12 | ) |
| 13 | |
| 14 | func TestSlowLogscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/TestSlowLog().(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#) { |
| 15 | e := getTestExporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/getTestExporter().() |
| 16 | |
| 17 | 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#) |
| 18 | go func() { |
| 19 | e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM) |
| 20 | close(chM) |
| 21 | }() |
| 22 | |
| 23 | oldSlowLogID := float64(0) |
| 24 | |
| 25 | for m := range chM { |
| 26 | switch m := m.(type) { |
| 27 | 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#: |
| 28 | 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().(), "slowlog_last_id") { |
| 29 | got := &dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.Metricscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#{} |
| 30 | m.Writescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Write().(got) |
| 31 | |
| 32 | oldSlowLogID = got.GetGaugescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetGauge().().GetValuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Gauge#GetValue().() |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | setupSlowLogscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupSlowLog().(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")) |
| 38 | defer resetSlowLogscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/resetSlowLog().(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")) |
| 39 | |
| 40 | 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#) |
| 41 | go func() { |
| 42 | e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM) |
| 43 | close(chM) |
| 44 | }() |
| 45 | |
| 46 | for m := range chM { |
| 47 | switch m := m.(type) { |
| 48 | 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#: |
| 49 | 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().(), "slowlog_last_id") { |
| 50 | got := &dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.Metricscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#{} |
| 51 | m.Writescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Write().(got) |
| 52 | |
| 53 | val := got.GetGaugescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetGauge().().GetValuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Gauge#GetValue().() |
| 54 | |
| 55 | if oldSlowLogID > val { |
| 56 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no new slowlogs found") |
| 57 | } |
| 58 | } |
| 59 | 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().(), "slowlog_length") { |
| 60 | got := &dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.Metricscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#{} |
| 61 | m.Writescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Write().(got) |
| 62 | |
| 63 | val := got.GetGaugescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetGauge().().GetValuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Gauge#GetValue().() |
| 64 | if val == 0 { |
| 65 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("slowlog length is zero") |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | resetSlowLogscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/resetSlowLog().(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")) |
| 72 | |
| 73 | 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#) |
| 74 | go func() { |
| 75 | e.Collectscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#Collect().(chM) |
| 76 | close(chM) |
| 77 | }() |
| 78 | |
| 79 | for m := range chM { |
| 80 | switch m := m.(type) { |
| 81 | 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#: |
| 82 | 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().(), "slowlog_length") { |
| 83 | got := &dtoscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/.Metricscip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#{} |
| 84 | m.Writescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#Write().(got) |
| 85 | |
| 86 | val := got.GetGaugescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Metric#GetGauge().().GetValuescip-go gomod github.com/prometheus/client_model v0.6.2 `github.com/prometheus/client_model/go`/Gauge#GetValue().() |
| 87 | if val != 0 { |
| 88 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("Slowlog was not reset") |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func setupSlowLogscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/setupSlowLog().(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#, addr string) error { |
| 96 | 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) |
| 97 | if err != nil { |
| 98 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("couldn't setup redis, err: %s ", err) |
| 99 | return err |
| 100 | } |
| 101 | defer c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().() |
| 102 | |
| 103 | _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("CONFIG", "SET", "SLOWLOG-LOG-SLOWER-THAN", 10000) |
| 104 | if err != nil { |
| 105 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("DEBUG", "SLEEP", latencyTestTimeToSleepInMillisscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/latencyTestTimeToSleepInMillis./1000.0) |
| 112 | if err != nil { |
| 113 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("couldn't setup redis, err: %s ", err) |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond. * 50) |
| 118 | |
| 119 | return nil |
| 120 | } |
| 121 | |
| 122 | func resetSlowLogscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/resetSlowLog().(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#, addr string) error { |
| 123 | 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) |
| 124 | if err != nil { |
| 125 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("couldn't setup redis, err: %s ", err) |
| 126 | return err |
| 127 | } |
| 128 | defer c.Closescip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Close().() |
| 129 | |
| 130 | _, err = c.Doscip-go gomod github.com/gomodule/redigo v1.9.3 `github.com/gomodule/redigo/redis`/Conn#Do().("SLOWLOG", "RESET") |
| 131 | if err != nil { |
| 132 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("couldn't setup redis, err: %s ", err) |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond. * 50) |
| 137 | |
| 138 | return nil |
| 139 | } |