Skip to content

Commit

Permalink
Merge pull request oliver006#147 from oliver006/oh_databases
Browse files Browse the repository at this point in the history
only pad db key counts when running extractInfoMetrics() the first time
  • Loading branch information
oliver006 authored Mar 22, 2018
2 parents c452304 + 9bb06cf commit 09e179c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 36 deletions.
65 changes: 35 additions & 30 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func extractConfigMetrics(config []string, addr string, alias string, scrapes ch
return
}

func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes chan<- scrapeResult, dbCount int) error {
func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes chan<- scrapeResult, dbCount int, padDBKeyCounts bool) error {
cmdstats := false
lines := strings.Split(info, "\r\n")

Expand All @@ -421,19 +421,24 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
}

split := strings.Split(line, ":")
if _, ok := instanceInfoFields[split[0]]; ok {
instanceInfo[split[0]] = split[1]
if len(split) != 2 {
continue
}
fieldKey := split[0]
fieldValue := split[1]
if _, ok := instanceInfoFields[fieldKey]; ok {
instanceInfo[fieldKey] = fieldValue
continue
}

if _, ok := slaveInfoFields[split[0]]; ok {
slaveInfo[split[0]] = split[1]
if _, ok := slaveInfoFields[fieldKey]; ok {
slaveInfo[fieldKey] = fieldValue
continue
}

if split[0] == "master_link_status" {
if fieldKey == "master_link_status" {
e.metricsMtx.RLock()
if split[1] == "up" {
if fieldValue == "up" {
e.metrics["master_link_up"].WithLabelValues(addr, alias).Set(1)
} else {
e.metrics["master_link_up"].WithLabelValues(addr, alias).Set(0)
Expand All @@ -442,7 +447,7 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
continue
}

if slaveOffset, slaveIp, slaveState, ok := parseConnectedSlaveString(split[0], split[1]); ok {
if slaveOffset, slaveIp, slaveState, ok := parseConnectedSlaveString(fieldKey, fieldValue); ok {
e.metricsMtx.RLock()
e.metrics["connected_slave_offset"].WithLabelValues(
addr,
Expand All @@ -453,7 +458,7 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
e.metricsMtx.RUnlock()
}

if len(split) != 2 || !includeMetric(split[0]) {
if !includeMetric(fieldKey) {
continue
}

Expand All @@ -465,14 +470,14 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
cmdstat_set:calls=61,usec=3139,usec_per_call=51.46
cmdstat_setex:calls=75,usec=1260,usec_per_call=16.80
*/
frags := strings.Split(split[0], "_")
frags := strings.Split(fieldKey, "_")
if len(frags) != 2 {
continue
}

cmd := frags[1]

frags = strings.Split(split[1], ",")
frags = strings.Split(fieldValue, ",")
if len(frags) != 3 {
continue
}
Expand All @@ -494,25 +499,26 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
continue
}

if keysTotal, keysEx, avgTTL, ok := parseDBKeyspaceString(split[0], split[1]); ok {
scrapes <- scrapeResult{Name: "db_keys", Addr: addr, Alias: alias, DB: split[0], Value: keysTotal}
scrapes <- scrapeResult{Name: "db_keys_expiring", Addr: addr, Alias: alias, DB: split[0], Value: keysEx}
if keysTotal, keysEx, avgTTL, ok := parseDBKeyspaceString(fieldKey, fieldValue); ok {
dbName := fieldKey
scrapes <- scrapeResult{Name: "db_keys", Addr: addr, Alias: alias, DB: dbName, Value: keysTotal}
scrapes <- scrapeResult{Name: "db_keys_expiring", Addr: addr, Alias: alias, DB: dbName, Value: keysEx}
if avgTTL > -1 {
scrapes <- scrapeResult{Name: "db_avg_ttl_seconds", Addr: addr, Alias: alias, DB: split[0], Value: avgTTL}
scrapes <- scrapeResult{Name: "db_avg_ttl_seconds", Addr: addr, Alias: alias, DB: dbName, Value: avgTTL}
}
handledDBs[split[0]] = true
handledDBs[dbName] = true
continue
}

metricName := sanitizeMetricName(split[0])
metricName := sanitizeMetricName(fieldKey)
if newName, ok := metricMap[metricName]; ok {
metricName = newName
}

var err error
var val float64

switch split[1] {
switch fieldValue {

case "ok":
val = 1
Expand All @@ -521,25 +527,24 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
val = 0

default:
val, err = strconv.ParseFloat(split[1], 64)
val, err = strconv.ParseFloat(fieldValue, 64)

}
if err != nil {
log.Debugf("couldn't parse %s, err: %s", split[1], err)
log.Debugf("couldn't parse %s, err: %s", fieldValue, err)
continue
}

scrapes <- scrapeResult{Name: metricName, Addr: addr, Alias: alias, Value: val}
}

for dbIndex := 0; dbIndex < dbCount; dbIndex++ {

dbName := "db" + strconv.Itoa(dbIndex)

if _, exists := handledDBs[dbName]; !exists {

scrapes <- scrapeResult{Name: "db_keys", Addr: addr, Alias: alias, DB: dbName, Value: 0}
scrapes <- scrapeResult{Name: "db_keys_expiring", Addr: addr, Alias: alias, DB: dbName, Value: 0}
if padDBKeyCounts {
for dbIndex := 0; dbIndex < dbCount; dbIndex++ {
dbName := "db" + strconv.Itoa(dbIndex)
if _, exists := handledDBs[dbName]; !exists {
scrapes <- scrapeResult{Name: "db_keys", Addr: addr, Alias: alias, DB: dbName, Value: 0}
scrapes <- scrapeResult{Name: "db_keys_expiring", Addr: addr, Alias: alias, DB: dbName, Value: 0}
}
}
}

Expand Down Expand Up @@ -612,7 +617,7 @@ func (e *Exporter) scrapeRedisHost(scrapes chan<- scrapeResult, addr string, idx

info, err := redis.String(c.Do("INFO", "ALL"))
if err == nil {
e.extractInfoMetrics(info, addr, e.redis.Aliases[idx], scrapes, dbCount)
e.extractInfoMetrics(info, addr, e.redis.Aliases[idx], scrapes, dbCount, true)
} else {
log.Errorf("Redis INFO err: %s", err)
return err
Expand All @@ -623,7 +628,7 @@ func (e *Exporter) scrapeRedisHost(scrapes chan<- scrapeResult, addr string, idx
if err != nil {
log.Errorf("redis err: %s", err)
} else {
e.extractInfoMetrics(info, addr, e.redis.Aliases[idx], scrapes, dbCount)
e.extractInfoMetrics(info, addr, e.redis.Aliases[idx], scrapes, dbCount, false)
}
}

Expand Down
16 changes: 10 additions & 6 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ package exporter
*/

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"

"bytes"
"flag"
"github.com/gomodule/redigo/redis"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
dto "github.com/prometheus/client_model/go"
)

Expand All @@ -42,6 +44,8 @@ var (

dbNumStr = "11"
dbNumStrFull = fmt.Sprintf("db%s", dbNumStr)

TestServerURL = ""
)

const (
Expand Down Expand Up @@ -97,7 +101,9 @@ func resetLatency(t *testing.T, addr string) error {
}

func getMetrics(t *testing.T) []byte {
resp, err := http.Get("http://127.0.0.1:9121/metrics")
url := TestServerURL + "/metrics"
log.Printf("Getting metrics from: %s", url)
resp, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -568,7 +574,6 @@ func TestCommandStats(t *testing.T) {
}

func TestHTTPEndpoint(t *testing.T) {

e, _ := NewRedisExporter(defaultRedisHost, "test", dbNumStrFull+"="+url.QueryEscape(keys[0]))

setupDBKeys(t, defaultRedisHost.Addrs[0])
Expand Down Expand Up @@ -808,6 +813,5 @@ func init() {
log.Printf("Using redis addrs: %#v", addrs)
defaultRedisHost = RedisHost{Addrs: []string{"redis://" + *redisAddr}, Aliases: aliases}

http.Handle("/metrics", prometheus.Handler())
go http.ListenAndServe("127.0.0.1:9121", nil)
TestServerURL = httptest.NewServer(promhttp.Handler()).URL
}

0 comments on commit 09e179c

Please sign in to comment.