Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed May 7, 2019
1 parent 09b8811 commit 6288963
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 101 deletions.
16 changes: 10 additions & 6 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,34 +844,38 @@ func getKeysFromPatterns(c redis.Conn, keys []dbKeyPair) (expandedKeys []dbKeyPa
return expandedKeys, err
}

func (e *Exporter) scrapeRedisHost(ch chan<- prometheus.Metric) error {
func connectToRedis(addr string, skipTLSVerification bool) (redis.Conn, error) {
options := []redis.DialOption{
redis.DialConnectTimeout(5 * time.Second),
redis.DialReadTimeout(5 * time.Second),
redis.DialWriteTimeout(5 * time.Second),

redis.DialTLSConfig(&tls.Config{
InsecureSkipVerify: e.options.SkipTLSVerification,
InsecureSkipVerify: skipTLSVerification,
}),
}

uri := e.redisAddr
uri := addr
if !strings.Contains(uri, "://") {
uri = "redis://" + uri
}
log.Debugf("Trying DialURL(): %s", uri)
c, err := redis.DialURL(uri, options...)
if err != nil {
log.Debugf("DialURL() failed, err: %s", err)
if frags := strings.Split(e.redisAddr, "://"); len(frags) == 2 {
if frags := strings.Split(addr, "://"); len(frags) == 2 {
log.Debugf("Trying: Dial(): %s %s", frags[0], frags[1])
c, err = redis.Dial(frags[0], frags[1], options...)
} else {
log.Debugf("Trying: Dial(): tcp %s", e.redisAddr)
c, err = redis.Dial("tcp", e.redisAddr, options...)
log.Debugf("Trying: Dial(): tcp %s", addr)
c, err = redis.Dial("tcp", addr, options...)
}
}
return c, err
}

func (e *Exporter) scrapeRedisHost(ch chan<- prometheus.Metric) error {
c, err := connectToRedis(e.redisAddr, e.options.SkipTLSVerification)
if err != nil {
log.Debugf("aborting for addr: %s - redis err: %s", e.redisAddr, err)
return err
Expand Down
111 changes: 16 additions & 95 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,111 +392,32 @@ func deleteKeysFromDB(t *testing.T, addr string) error {
return nil
}

/*
func TestHostVariations(t *testing.T) {
for _, prefix := range []string{"", "redis://", "tcp://"} {
addr := prefix + os.Getenv("TEST_REDIS_URI")
e, _ := NewRedisExporter([]RedisHost{{Addr: addr}}, Options{Namespace: "test"})
scrapes := make(chan prometheus.Metric, 10000)
e.scrapeAllHosts(scrapes)
found := 0
for range scrapes {
found++
}
if found == 0 {
t.Errorf("didn't find any scrapes for host: %s", addr)
}
}
}
host := strings.ReplaceAll(os.Getenv("TEST_REDIS_URI"), "redis://", "")

func TestCountingKeys(t *testing.T) {
e := getTestExporter()
scrapes := make(chan scrapeResult, 10000)
e.scrapeAllHosts(scrapes)
var keysTestDB float64
for s := range scrapes {
if s.Name == "db_keys" && s.DB == dbNumStrFull {
keysTestDB = s.Value
break
for _, prefix := range []string{"", "redis://", "tcp://", ""} {
addr := prefix + host
c, err := connectToRedis(addr, true)
if err != nil {
t.Errorf("connectToRedis() err: %s", err)
continue
}
}

setupDBKeys(t, os.Getenv("TEST_REDIS_URI"))
defer deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
scrapes = make(chan scrapeResult, 1000)
e.scrapeAllHosts(scrapes)
// +1 for the one SET key
want := keysTestDB + float64(len(keys)) + float64(len(keysExpiring)) + 1 + float64(len(listKeys))
for s := range scrapes {
if s.Name == "db_keys" && s.DB == dbNumStrFull {
if want != s.Value {
t.Errorf("values not matching, %f != %f", keysTestDB, s.Value)
}
break
if _, err := c.Do("PING", ""); err != nil {
t.Errorf("PING err: %s", err)
}
}
deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
scrapes = make(chan scrapeResult, 10000)
e.scrapeAllHosts(scrapes)

for s := range scrapes {
if s.Name == "db_keys" && s.DB == dbNumStrFull {
if keysTestDB != s.Value {
t.Errorf("values not matching, %f != %f", keysTestDB, s.Value)
}
break
}
if s.Name == "db_avg_ttl_seconds" && s.DB == dbNumStrFull {
if keysTestDB != s.Value {
t.Errorf("values not matching, %f != %f", keysTestDB, s.Value)
}
break
}
}
}
func TestExporterValues(t *testing.T) {
e := getTestExporter()
setupDBKeys(t, os.Getenv("TEST_REDIS_URI"))
defer deleteKeysFromDB(t, os.Getenv("TEST_REDIS_URI"))
scrapes := make(chan scrapeResult, 10000)
e.scrapeAllHosts(scrapes)
wantValues := map[string]float64{
"db_keys_total": float64(len(keys)+len(keysExpiring)) + 1, // + 1 for the SET key
"db_expiring_keys_total": float64(len(keysExpiring)),
}
for s := range scrapes {
if wantVal, ok := wantValues[s.Name]; ok {
if dbNumStrFull == s.DB && wantVal != s.Value {
t.Errorf("values not matching, %f != %f", wantVal, s.Value)
}
}
c.Close()
}
}
*/

type tstData struct {
db string
stats string
keysTotal, keysEx, avgTTL float64
ok bool
}

func TestKeyspaceStringParser(t *testing.T) {
tsts := []tstData{
tsts := []struct {
db string
stats string
keysTotal, keysEx, avgTTL float64
ok bool
}{
{db: "xxx", stats: "", ok: false},
{db: "xxx", stats: "keys=1,expires=0,avg_ttl=0", ok: false},
{db: "db0", stats: "xxx", ok: false},
Expand Down

0 comments on commit 6288963

Please sign in to comment.