Skip to content
Snippets Groups Projects
Unverified Commit 0b08781c authored by Oliver's avatar Oliver Committed by GitHub
Browse files

Merge pull request #194 from cgroschupp/add_slowlog_last_id

Add slowlog_last_id metric.
parents 824dc556 2516dfed
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -200,6 +200,11 @@ func (e *Exporter) initGauges() {
Name: "slowlog_length",
Help: "Total slowlog",
}, []string{"addr", "alias"})
e.metrics["slowlog_last_id"] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: e.namespace,
Name: "slowlog_last_id",
Help: "Last id of slowlog",
}, []string{"addr", "alias"})
}
 
// splitKeyArgs splits a command-line supplied argument into a slice of dbKeyPairs.
Loading
Loading
@@ -874,6 +879,20 @@ func (e *Exporter) scrapeRedisHost(scrapes chan<- scrapeResult, addr string, idx
e.metricsMtx.RUnlock()
}
 
if values, err := redis.Values(c.Do("SLOWLOG", "GET", "1")); err == nil {
var slowlogLastId int64 = 0
if len(values) > 0 {
if values, err = redis.Values(values[0], err); err == nil && len(values) > 0 {
slowlogLastId = values[0].(int64)
}
}
e.metricsMtx.RLock()
e.metrics["slowlog_last_id"].WithLabelValues(addr, e.redis.Aliases[idx]).Set(float64(slowlogLastId))
e.metricsMtx.RUnlock()
}
log.Debugf("scrapeRedisHost() done")
return nil
}
Loading
Loading
Loading
Loading
@@ -105,6 +105,53 @@ func resetLatency(t *testing.T, addr string) error {
return nil
}
 
func setupSlowLog(t *testing.T, addr string) error {
c, err := redis.DialURL(addr)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
defer c.Close()
_, err = c.Do("CONFIG", "SET", "SLOWLOG-LOG-SLOWER-THAN", 10000)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
// Have to pass in the sleep time in seconds so we have to divide
// the number of milliseconds by 1000 to get number of seconds
_, err = c.Do("DEBUG", "SLEEP", TimeToSleep/1000.0)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
time.Sleep(time.Millisecond * 50)
return nil
}
func resetSlowLog(t *testing.T, addr string) error {
c, err := redis.DialURL(addr)
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
defer c.Close()
_, err = c.Do("SLOWLOG", "RESET")
if err != nil {
t.Errorf("couldn't setup redis, err: %s ", err)
return err
}
time.Sleep(time.Millisecond * 50)
return nil
}
func downloadUrl(t *testing.T, url string) []byte {
log.Debugf("downloadURL() %s", url)
resp, err := http.Get(url)
Loading
Loading
@@ -167,6 +214,87 @@ func TestLatencySpike(t *testing.T) {
}
}
 
func TestSlowLog(t *testing.T) {
e, _ := NewRedisExporter(defaultRedisHost, "test", "", "")
chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
oldSlowLogId := float64(0)
for m := range chM {
switch m := m.(type) {
case prometheus.Gauge:
if strings.Contains(m.Desc().String(), "slowlog_last_id") {
got := &dto.Metric{}
m.Write(got)
oldSlowLogId = got.GetGauge().GetValue()
}
}
}
setupSlowLog(t, defaultRedisHost.Addrs[0])
defer resetSlowLog(t, defaultRedisHost.Addrs[0])
chM = make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
for m := range chM {
switch m := m.(type) {
case prometheus.Gauge:
if strings.Contains(m.Desc().String(), "slowlog_last_id") {
got := &dto.Metric{}
m.Write(got)
val := got.GetGauge().GetValue()
if oldSlowLogId > val {
t.Errorf("no new slowlogs found")
}
}
if strings.Contains(m.Desc().String(), "slowlog_length") {
got := &dto.Metric{}
m.Write(got)
val := got.GetGauge().GetValue()
if val == 0 {
t.Errorf("slowlog length is zero")
}
}
}
}
resetSlowLog(t, defaultRedisHost.Addrs[0])
chM = make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()
for m := range chM {
switch m := m.(type) {
case prometheus.Gauge:
if strings.Contains(m.Desc().String(), "slowlog_length") {
got := &dto.Metric{}
m.Write(got)
val := got.GetGauge().GetValue()
if val != 0 {
t.Errorf("Slowlog was not reset")
}
}
}
}
}
func setupDBKeys(t *testing.T, addr string) error {
 
c, err := redis.DialURL(addr)
Loading
Loading
@@ -345,6 +473,7 @@ func TestExporterMetrics(t *testing.T) {
"config_maxmemory", // testing config extraction
"config_maxclients", // testing config extraction
"slowlog_length",
"slowlog_last_id",
"start_time_seconds",
"uptime_in_seconds",
}
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment