From f5d93eda5983c7c155ea81d0c58f165aaba03e47 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 21 Mar 2018 19:50:41 -0400 Subject: [PATCH 1/2] better variable names, only pad db key counts when running extractInfoMetrics() the first time --- exporter/redis.go | 65 +++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/exporter/redis.go b/exporter/redis.go index 126e1139..16697b54 100644 --- a/exporter/redis.go +++ b/exporter/redis.go @@ -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") @@ -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) @@ -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, @@ -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 } @@ -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 } @@ -494,17 +499,18 @@ 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 } @@ -512,7 +518,7 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c var err error var val float64 - switch split[1] { + switch fieldValue { case "ok": val = 1 @@ -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} + } } } @@ -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 @@ -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) } } From 9bb06cf23a52d2a8f52fa7fa6ed91dfcd7fde165 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 21 Mar 2018 20:05:45 -0400 Subject: [PATCH 2/2] fix http test setup --- exporter/redis_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/exporter/redis_test.go b/exporter/redis_test.go index 1cfe4032..53d7d6d3 100644 --- a/exporter/redis_test.go +++ b/exporter/redis_test.go @@ -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" ) @@ -42,6 +44,8 @@ var ( dbNumStr = "11" dbNumStrFull = fmt.Sprintf("db%s", dbNumStr) + + TestServerURL = "" ) const ( @@ -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) } @@ -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]) @@ -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 }