Skip to content

Commit

Permalink
Merge pull request #83 from albestia/master
Browse files Browse the repository at this point in the history
--check-keys could use wildcards to search existing keys
  • Loading branch information
oliver006 authored Jul 6, 2017
2 parents de9ec3b + f0a43a8 commit cd1eb0b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
39 changes: 25 additions & 14 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,23 +471,34 @@ func (e *Exporter) scrapeRedisHost(scrapes chan<- scrapeResult, addr string, idx
if _, err := c.Do("SELECT", k.db); err != nil {
continue
}
if tempVal, err := c.Do("GET", k.key); err == nil && tempVal != nil {
if val, err := strconv.ParseFloat(fmt.Sprintf("%s", tempVal), 64); err == nil {
e.keyValues.WithLabelValues(addr, e.redis.Aliases[idx], "db"+k.db, k.key).Set(val)

obtainedKeys := []string{}
if tempVal, err := redis.Strings(c.Do("KEYS", k.key)); err == nil && tempVal != nil {
for _, tempKey := range tempVal {
log.Debugf("Append result: %s", tempKey)
obtainedKeys = append(obtainedKeys, tempKey)
}
}

for _, op := range []string{
"HLEN",
"LLEN",
"SCARD",
"ZCARD",
"PFCOUNT",
"STRLEN",
} {
if tempVal, err := c.Do(op, k.key); err == nil && tempVal != nil {
e.keySizes.WithLabelValues(addr, e.redis.Aliases[idx], "db"+k.db, k.key).Set(float64(tempVal.(int64)))
break
for _, key := range obtainedKeys {
if tempVal, err := c.Do("GET", key); err == nil && tempVal != nil {
if val, err := strconv.ParseFloat(fmt.Sprintf("%s", tempVal), 64); err == nil {
e.keyValues.WithLabelValues(addr, e.redis.Aliases[idx], "db"+k.db, key).Set(val)
}
}

for _, op := range []string{
"HLEN",
"LLEN",
"SCARD",
"ZCARD",
"PFCOUNT",
"STRLEN",
} {
if tempVal, err := c.Do(op, key); err == nil && tempVal != nil {
e.keySizes.WithLabelValues(addr, e.redis.Aliases[idx], "db"+k.db, key).Set(float64(tempVal.(int64)))
break
}
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,47 @@ func TestKeyValuesAndSizes(t *testing.T) {
}
}

func TestKeyValuesAndSizesWildcard(t *testing.T) {
s := dbNumStrFull + "=wild*"
fmt.Println(s)
e, _ := NewRedisExporter(defaultRedisHost, "test", s)

setupDBKeys(t, defaultRedisHost.Addrs[0])
defer deleteKeysFromDB(t, defaultRedisHost.Addrs[0])

chM := make(chan prometheus.Metric)
go func() {
e.Collect(chM)
close(chM)
}()

metricWant := "test_key_size"
totalExpected := 3

for m := range chM {
switch m.(type) {
case prometheus.Gauge:
if !strings.Contains(m.Desc().String(), metricWant) {
continue
}

g := &dto.Metric{}
m.Write(g)
for _, l := range g.Label {
if *l.Name == "key" && strings.HasPrefix(*l.Value, "wild") {
totalExpected--
}
}
default:
log.Printf("default: m: %#v", m)
}
}

if totalExpected != 0 {
t.Errorf("didn't find enough wild*, outstanding: %d", totalExpected)
}
}

func TestKeyValueInvalidDB(t *testing.T) {

e, _ := NewRedisExporter(defaultRedisHost, "test", "999="+url.QueryEscape(keys[0]))
Expand Down Expand Up @@ -568,6 +609,9 @@ func init() {
keys = append(keys, key)
}

// add some with the same prefix so we can test the check-keys wildcard
keys = append(keys, "wildcard", "wildbeast", "wildwoods")

for _, n := range []string{"A.J.", "Howie", "Nick", "Kevin", "Brian"} {
key := fmt.Sprintf("key:exp-%s-%d", n, ts)
keysExpiring = append(keysExpiring, key)
Expand Down

0 comments on commit cd1eb0b

Please sign in to comment.