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

fix replication metrics handling, fix tests (#246)

parent 994d0d2b
No related branches found
No related tags found
No related merge requests found
Loading
@@ -34,6 +34,7 @@ steps:
Loading
@@ -34,6 +34,7 @@ steps:
- name: tests - name: tests
image: "golang:1.12" image: "golang:1.12"
environment: environment:
LOG_LEVEL: "info"
TEST_TILE38_URI: "tile38:9851" TEST_TILE38_URI: "tile38:9851"
TEST_SECOND_REDIS_URI: "redis://moar-redis:6380" TEST_SECOND_REDIS_URI: "redis://moar-redis:6380"
TEST_REDIS_CLUSTER_MASTER_URI: "redis-cluster:7000" TEST_REDIS_CLUSTER_MASTER_URI: "redis-cluster:7000"
Loading
@@ -41,17 +42,19 @@ steps:
Loading
@@ -41,17 +42,19 @@ steps:
COVERALLS_TOKEN: COVERALLS_TOKEN:
from_secret: coveralls-token from_secret: coveralls-token
commands: commands:
- 'go build'
- "sleep 10" # let the redis test instances all come up first
- 'go test -v -covermode=atomic -cover -race -coverprofile=coverage.txt ./exporter/... --redis.addr=redis'
- 'echo "checking gofmt"'
- 'echo " ! gofmt -d main.go 2>&1 | read " | bash' - 'echo " ! gofmt -d main.go 2>&1 | read " | bash'
- 'echo " ! gofmt -d exporter/*.go 2>&1 | read " | bash' - 'echo " ! gofmt -d exporter/*.go 2>&1 | read " | bash'
- 'echo "checking gofmt - DONE"'
- 'go vet ./exporter/...' - 'go vet ./exporter/...'
- 'go build'
- "sleep 10"
- 'go test -v -covermode=atomic -cover -race -coverprofile=coverage.txt ./exporter/... --redis.addr=redis'
- 'go get github.com/mattn/goveralls' - 'go get github.com/mattn/goveralls'
- '/go/bin/goveralls -coverprofile=coverage.txt -service=drone.io' - '/go/bin/goveralls -coverprofile=coverage.txt -service=drone.io'
when: when:
event: event:
- push - pull_request
   
   
- name: coverage-codecov - name: coverage-codecov
Loading
@@ -63,7 +66,7 @@ steps:
Loading
@@ -63,7 +66,7 @@ steps:
- coverage.txt - coverage.txt
when: when:
event: event:
- push - pull_request
   
   
- name: release-docker-image-scratch - name: release-docker-image-scratch
Loading
Loading
Loading
@@ -560,7 +560,7 @@ func (e *Exporter) handleMetricsCommandStats(addr string, alias string, fieldKey
Loading
@@ -560,7 +560,7 @@ func (e *Exporter) handleMetricsCommandStats(addr string, alias string, fieldKey
e.metrics["commands_duration_seconds_total"].WithLabelValues(addr, alias, cmd).Set(usecTotal / 1e6) e.metrics["commands_duration_seconds_total"].WithLabelValues(addr, alias, cmd).Set(usecTotal / 1e6)
} }
   
func (e *Exporter) handleMetricsReplication(addr string, alias string, fieldKey string, fieldValue string) { func (e *Exporter) handleMetricsReplication(addr string, alias string, fieldKey string, fieldValue string) bool {
e.metricsMtx.RLock() e.metricsMtx.RLock()
defer e.metricsMtx.RUnlock() defer e.metricsMtx.RUnlock()
   
Loading
@@ -571,7 +571,7 @@ func (e *Exporter) handleMetricsReplication(addr string, alias string, fieldKey
Loading
@@ -571,7 +571,7 @@ func (e *Exporter) handleMetricsReplication(addr string, alias string, fieldKey
} else { } else {
e.metrics["master_link_up"].WithLabelValues(addr, alias).Set(0) e.metrics["master_link_up"].WithLabelValues(addr, alias).Set(0)
} }
return return true
} }
   
// not a slave, try extracting master metrics // not a slave, try extracting master metrics
Loading
@@ -593,7 +593,10 @@ func (e *Exporter) handleMetricsReplication(addr string, alias string, fieldKey
Loading
@@ -593,7 +593,10 @@ func (e *Exporter) handleMetricsReplication(addr string, alias string, fieldKey
slaveState, slaveState,
).Set(lag) ).Set(lag)
} }
return true
} }
return false
} }
   
func (e *Exporter) handleMetricsServer(addr string, alias string, fieldKey string, fieldValue string) { func (e *Exporter) handleMetricsServer(addr string, alias string, fieldKey string, fieldValue string) {
Loading
@@ -641,8 +644,9 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
Loading
@@ -641,8 +644,9 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c
switch fieldClass { switch fieldClass {
   
case "Replication": case "Replication":
e.handleMetricsReplication(addr, alias, fieldKey, fieldValue) if ok := e.handleMetricsReplication(addr, alias, fieldKey, fieldValue); ok {
continue continue
}
   
case "Server": case "Server":
e.handleMetricsServer(addr, alias, fieldKey, fieldValue) e.handleMetricsServer(addr, alias, fieldKey, fieldValue)
Loading
Loading
Loading
@@ -9,7 +9,6 @@ package exporter
Loading
@@ -9,7 +9,6 @@ package exporter
*/ */
   
import ( import (
"bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
Loading
@@ -152,7 +151,7 @@ func resetSlowLog(t *testing.T, addr string) error {
Loading
@@ -152,7 +151,7 @@ func resetSlowLog(t *testing.T, addr string) error {
return nil return nil
} }
   
func downloadUrl(t *testing.T, url string) []byte { func downloadUrl(t *testing.T, url string) string {
log.Debugf("downloadURL() %s", url) log.Debugf("downloadURL() %s", url)
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
Loading
@@ -163,7 +162,7 @@ func downloadUrl(t *testing.T, url string) []byte {
Loading
@@ -163,7 +162,7 @@ func downloadUrl(t *testing.T, url string) []byte {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
return body return string(body)
} }
   
func TestLatencySpike(t *testing.T) { func TestLatencySpike(t *testing.T) {
Loading
@@ -334,10 +333,9 @@ func setupDBKeys(t *testing.T, addr string) error {
Loading
@@ -334,10 +333,9 @@ func setupDBKeys(t *testing.T, addr string) error {
} }
defer c.Close() defer c.Close()
   
_, err = c.Do("SELECT", dbNumStr) if _, err := c.Do("SELECT", dbNumStr); err != nil {
if err != nil { log.Printf("setupDBKeys() - couldn't setup redis, err: %s ", err)
t.Errorf("couldn't setup redis, err: %s ", err) // not failing on this one - cluster doesn't allow for SELECT so we log and ignore the error
return err
} }
   
for _, key := range keys { for _, key := range keys {
Loading
@@ -384,10 +382,9 @@ func deleteKeysFromDB(t *testing.T, addr string) error {
Loading
@@ -384,10 +382,9 @@ func deleteKeysFromDB(t *testing.T, addr string) error {
} }
defer c.Close() defer c.Close()
   
_, err = c.Do("SELECT", dbNumStr) if _, err := c.Do("SELECT", dbNumStr); err != nil {
if err != nil { log.Printf("deleteKeysFromDB() - couldn't setup redis, err: %s ", err)
t.Errorf("couldn't setup redis, err: %s ", err) // not failing on this one - cluster doesn't allow for SELECT so we log and ignore the error
return err
} }
   
for _, key := range keys { for _, key := range keys {
Loading
@@ -1019,6 +1016,10 @@ func TestCommandStats(t *testing.T) {
Loading
@@ -1019,6 +1016,10 @@ func TestCommandStats(t *testing.T) {
} }
   
func TestHTTPEndpoint(t *testing.T) { func TestHTTPEndpoint(t *testing.T) {
r := prometheus.NewRegistry()
prometheus.DefaultGatherer = r
prometheus.DefaultRegisterer = r
ts := httptest.NewServer(promhttp.Handler()) ts := httptest.NewServer(promhttp.Handler())
defer ts.Close() defer ts.Close()
   
Loading
@@ -1044,7 +1045,7 @@ func TestHTTPEndpoint(t *testing.T) {
Loading
@@ -1044,7 +1045,7 @@ func TestHTTPEndpoint(t *testing.T) {
`cmd="get"`, `cmd="get"`,
} }
for _, test := range tests { for _, test := range tests {
if !bytes.Contains(body, []byte(test)) { if !strings.Contains(body, test) {
t.Errorf("want metrics to include %q, have:\n%s", test, body) t.Errorf("want metrics to include %q, have:\n%s", test, body)
} }
} }
Loading
@@ -1206,6 +1207,10 @@ func TestSanitizeMetricName(t *testing.T) {
Loading
@@ -1206,6 +1207,10 @@ func TestSanitizeMetricName(t *testing.T) {
} }
   
func TestKeysReset(t *testing.T) { func TestKeysReset(t *testing.T) {
r := prometheus.NewRegistry()
prometheus.DefaultGatherer = r
prometheus.DefaultRegisterer = r
ts := httptest.NewServer(promhttp.Handler()) ts := httptest.NewServer(promhttp.Handler())
defer ts.Close() defer ts.Close()
   
Loading
@@ -1223,16 +1228,14 @@ func TestKeysReset(t *testing.T) {
Loading
@@ -1223,16 +1228,14 @@ func TestKeysReset(t *testing.T) {
}() }()
   
body := downloadUrl(t, ts.URL+"/metrics") body := downloadUrl(t, ts.URL+"/metrics")
if !strings.Contains(body, keys[0]) {
if !bytes.Contains(body, []byte(keys[0])) {
t.Errorf("Did not found key %q\n%s", keys[0], body) t.Errorf("Did not found key %q\n%s", keys[0], body)
} }
   
deleteKeysFromDB(t, defaultRedisHost.Addrs[0]) deleteKeysFromDB(t, defaultRedisHost.Addrs[0])
   
body = downloadUrl(t, ts.URL+"/metrics") body = downloadUrl(t, ts.URL+"/metrics")
if strings.Contains(body, keys[0]) {
if bytes.Contains(body, []byte(keys[0])) {
t.Errorf("Metric is present in metrics list %q\n%s", keys[0], body) t.Errorf("Metric is present in metrics list %q\n%s", keys[0], body)
} }
} }
Loading
@@ -1244,15 +1247,18 @@ func TestClusterMaster(t *testing.T) {
Loading
@@ -1244,15 +1247,18 @@ func TestClusterMaster(t *testing.T) {
return return
} }
   
r := prometheus.NewRegistry()
prometheus.DefaultGatherer = r
prometheus.DefaultRegisterer = r
ts := httptest.NewServer(promhttp.Handler()) ts := httptest.NewServer(promhttp.Handler())
defer ts.Close() defer ts.Close()
   
addr := "redis://" + os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI") addr := "redis://" + os.Getenv("TEST_REDIS_CLUSTER_MASTER_URI")
host := RedisHost{Addrs: []string{addr}, Aliases: []string{"master"}} host := RedisHost{Addrs: []string{addr}, Aliases: []string{"master"}}
log.Printf("master - using host cfg: %#v", host)
e, _ := NewRedisExporter(host, "test", "", "") e, _ := NewRedisExporter(host, "test", "", "")
   
setupDBKeys(t, defaultRedisHost.Addrs[0])
defer deleteKeysFromDB(t, defaultRedisHost.Addrs[0])
prometheus.Register(e) prometheus.Register(e)
   
chM := make(chan prometheus.Metric, 10000) chM := make(chan prometheus.Metric, 10000)
Loading
@@ -1262,8 +1268,14 @@ func TestClusterMaster(t *testing.T) {
Loading
@@ -1262,8 +1268,14 @@ func TestClusterMaster(t *testing.T) {
}() }()
   
body := downloadUrl(t, ts.URL+"/metrics") body := downloadUrl(t, ts.URL+"/metrics")
if !bytes.Contains(body, []byte("test_instance_info")) { // log.Printf("master - body: %s", body)
t.Errorf("Did not found key %q\n%s", keys[0], body) for _, want := range []string{
"test_instance_info{addr=\"redis://redis-cluster:7000\",alias=\"master\"",
"test_master_repl_offset",
} {
if !strings.Contains(body, want) {
t.Errorf("Did not find key [%s] \nbody: %s", want, body)
}
} }
} }
   
Loading
@@ -1311,12 +1323,16 @@ func TestPasswordProtectedInstance(t *testing.T) {
Loading
@@ -1311,12 +1323,16 @@ func TestPasswordProtectedInstance(t *testing.T) {
   
body := downloadUrl(t, ts.URL+"/metrics") body := downloadUrl(t, ts.URL+"/metrics")
   
if !bytes.Contains(body, []byte("test_up")) { if !strings.Contains(body, "test_up") {
t.Errorf("error") t.Errorf("error, missing test_up")
} }
} }
   
func TestPasswordInvalid(t *testing.T) { func TestPasswordInvalid(t *testing.T) {
r := prometheus.NewRegistry()
prometheus.DefaultGatherer = r
prometheus.DefaultRegisterer = r
ts := httptest.NewServer(promhttp.Handler()) ts := httptest.NewServer(promhttp.Handler())
defer ts.Close() defer ts.Close()
   
Loading
@@ -1359,7 +1375,7 @@ func TestPasswordInvalid(t *testing.T) {
Loading
@@ -1359,7 +1375,7 @@ func TestPasswordInvalid(t *testing.T) {
}() }()
   
body := downloadUrl(t, ts.URL+"/metrics") body := downloadUrl(t, ts.URL+"/metrics")
if !bytes.Contains(body, []byte("test_exporter_last_scrape_error 1")) { if !strings.Contains(body, "test_exporter_last_scrape_error 1") {
t.Errorf(`error, expected string "test_exporter_last_scrape_error 1" in body`) t.Errorf(`error, expected string "test_exporter_last_scrape_error 1" in body`)
} }
} }
Loading
@@ -1371,16 +1387,18 @@ func TestClusterSlave(t *testing.T) {
Loading
@@ -1371,16 +1387,18 @@ func TestClusterSlave(t *testing.T) {
return return
} }
   
r := prometheus.NewRegistry()
prometheus.DefaultGatherer = r
prometheus.DefaultRegisterer = r
ts := httptest.NewServer(promhttp.Handler()) ts := httptest.NewServer(promhttp.Handler())
defer ts.Close() defer ts.Close()
   
addr := "redis://" + os.Getenv("TEST_REDIS_CLUSTER_SLAVE_URI") addr := "redis://" + os.Getenv("TEST_REDIS_CLUSTER_SLAVE_URI")
host := RedisHost{Addrs: []string{addr}, Aliases: []string{"slave"}} host := RedisHost{Addrs: []string{addr}, Aliases: []string{"slave"}}
log.Printf("slave - using host cfg: %#v", host)
e, _ := NewRedisExporter(host, "test", "", "") e, _ := NewRedisExporter(host, "test", "", "")
   
setupDBKeys(t, defaultRedisHost.Addrs[0])
defer deleteKeysFromDB(t, defaultRedisHost.Addrs[0])
prometheus.Register(e) prometheus.Register(e)
   
chM := make(chan prometheus.Metric, 10000) chM := make(chan prometheus.Metric, 10000)
Loading
@@ -1390,15 +1408,19 @@ func TestClusterSlave(t *testing.T) {
Loading
@@ -1390,15 +1408,19 @@ func TestClusterSlave(t *testing.T) {
}() }()
   
body := downloadUrl(t, ts.URL+"/metrics") body := downloadUrl(t, ts.URL+"/metrics")
if !bytes.Contains(body, []byte("test_instance_info")) { // log.Printf("slave - body: %s", body)
t.Errorf("Did not found key %q\n%s", keys[0], body) for _, want := range []string{
"test_instance_info",
"test_master_last_io_seconds",
"test_slave_info{addr=\"redis://redis-cluster:7005\",alias=\"slave\",",
} {
if !strings.Contains(body, want) {
t.Errorf("Did not find key [%s] \nbody: %s", want, body)
}
} }
} }
   
func TestCheckKeys(t *testing.T) { func TestCheckKeys(t *testing.T) {
ts := httptest.NewServer(promhttp.Handler())
defer ts.Close()
for _, tst := range []struct { for _, tst := range []struct {
SingleCheckKey string SingleCheckKey string
CheckKeys string CheckKeys string
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