Skip to content

Commit

Permalink
Merge pull request #27 from oliver006/oh_urlencode_keys
Browse files Browse the repository at this point in the history
urlencode check keys
  • Loading branch information
oliver006 authored Sep 24, 2016
2 parents 5a9f035 + 5bb81e4 commit f0f60e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and adjust the host name accordingly.

Name | Description
-------------------|------------
check-keys | Comma separated list of keys to export value and length/size, eg: `db3=user_count` will export key `user_count` from db `3`. db defaults to `0` if omitted.
redis.addr | Address of one or more redis nodes, comma separated, defaults to `localhost:6379`.
redis.password | Password to use when authenticating to Redis
namespace | Namespace for the metrics, defaults to `redis`.
Expand All @@ -60,6 +61,7 @@ REDIS_PASSWORD | Password to use when authenticating to Redis
Most items from the INFO command are exported,
see http://redis.io/commands/info for details.<br>
In addition, for every database there are metrics for total keys, expiring keys and the average TTL for keys in the database.<br>
You can also export values of keys if they're in numeric format by using the `-check-keys` flag. The exporter will also export the size (or, depending on the data type, the length) of the key. This can be used to export the number of elements in (sorted) sets, hashes, lists, etc. <br>


### What does it look like?
Expand Down
13 changes: 9 additions & 4 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package exporter
import (
"fmt"
"log"
"net/url"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -161,21 +162,25 @@ func NewRedisExporter(redis RedisHost, namespace, checkKeys string) (*Exporter,
Help: "The last scrape error status.",
}),
}
var err error
for _, k := range strings.Split(checkKeys, ",") {
db := "0"
key := ""
frags := strings.Split(k, ":")
frags := strings.Split(k, "=")
switch len(frags) {
case 1:
db = "0"
key = strings.TrimSpace(frags[0])
key, err = url.QueryUnescape(strings.TrimSpace(frags[0]))
case 2:
db = strings.Replace(strings.TrimSpace(frags[0]), "db", "", -1)
key = strings.TrimSpace(frags[1])
key, err = url.QueryUnescape(strings.TrimSpace(frags[1]))
default:
err = fmt.Errorf("")
}
if err != nil {
log.Printf("Couldn't parse db/key string: %s", k)
continue
}

e.keys = append(e.keys, dbKeyPair{db, key})
}
e.initGauges()
Expand Down
19 changes: 9 additions & 10 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -197,7 +198,7 @@ func TestExporterMetrics(t *testing.T) {

want := 25
if len(e.metrics) < want {
t.Errorf("need moar metrics, found %d, want > %d", len(e.metrics), want)
t.Errorf("need moar metrics, found: %d, want: %d", len(e.metrics), want)
}

wantKeys := []string{
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestExporterValues(t *testing.T) {
go e.scrape(scrapes)

wantValues := map[string]float64{
"db_keys_total": float64(len(keys)+len(keysExpiring)) + 1, // + 1 for the SET key
"db_keys_total": float64(len(keys)+len(keysExpiring)) + 1, // + 1 for the SET key
"db_expiring_keys_total": float64(len(keysExpiring)),
}

Expand Down Expand Up @@ -278,7 +279,7 @@ func TestKeyspaceStringParser(t *testing.T) {

func TestKeyValuesAndSizes(t *testing.T) {

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

setupDBKeys(t)
defer deleteKeysFromDB(t)
Expand All @@ -292,19 +293,16 @@ func TestKeyValuesAndSizes(t *testing.T) {
want := map[string]bool{"test_key_size": false, "test_key_value": false}

for m := range chM {

switch m.(type) {
case prometheus.Gauge:
for k := range want {
if strings.Contains(m.Desc().String(), k) {
want[k] = true
}
}

default:
log.Printf("default: m: %#v", m)
}

}
for k, v := range want {
if !v {
Expand All @@ -316,7 +314,7 @@ func TestKeyValuesAndSizes(t *testing.T) {

func TestHTTPEndpoint(t *testing.T) {

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

setupDBKeys(t)
defer deleteKeysFromDB(t)
Expand All @@ -339,6 +337,7 @@ func TestHTTPEndpoint(t *testing.T) {
tests := []string{
`test_connected_clients`,
`test_total_commands_processed`,
`test_key_size`,
}
for _, test := range tests {
if !bytes.Contains(body, []byte(test)) {
Expand All @@ -350,7 +349,7 @@ func TestHTTPEndpoint(t *testing.T) {
func TestNonExistingHost(t *testing.T) {

rr := RedisHost{Addrs: []string{"localhost:12345"}}
e, _ := NewRedisExporter(rr, "test", dbNumStrFull+":"+keys[0])
e, _ := NewRedisExporter(rr, "test", "")

chM := make(chan prometheus.Metric)

Expand Down Expand Up @@ -403,12 +402,12 @@ func TestNonExistingHost(t *testing.T) {

func init() {
for _, n := range []string{"john", "paul", "ringo", "george"} {
key := fmt.Sprintf("key-%s-%d", n, ts)
key := fmt.Sprintf("key:%s-%d", n, ts)
keys = append(keys, key)
}

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

Expand Down

0 comments on commit f0f60e2

Please sign in to comment.