Skip to content

Commit

Permalink
a few more metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed Jun 23, 2017
1 parent d3af2b4 commit a347d08
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
41 changes: 36 additions & 5 deletions exporter/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ var (
metricMap = map[string]string{
// # Server
"uptime_in_seconds": "uptime_in_seconds",
"process_id": "process_id",

// # Clients
"connected_clients": "connected_clients",
"blocked_clients": "blocked_clients",
"connected_clients": "connected_clients",
"blocked_clients": "blocked_clients",
"client_longest_output_list": "client_longest_output_list",

// # Memory
"used_memory": "memory_used_bytes",
Expand Down Expand Up @@ -86,11 +88,15 @@ var (
"keyspace_misses": "keyspace_misses_total",
"pubsub_channels": "pubsub_channels",
"pubsub_patterns": "pubsub_patterns",
"instantaneous_ops_per_sec": "instantaneous_ops_per_sec",
"latest_fork_usec": "latest_fork_usec",

// # Replication
"loading": "loading_dump_file",
"connected_slaves": "connected_slaves",
"repl_backlog_size": "replication_backlog_bytes",
"loading": "loading_dump_file",
"connected_slaves": "connected_slaves",
"repl_backlog_size": "replication_backlog_bytes",
"master_last_io_seconds_ago": "master_last_io_seconds",
"master_repl_offset": "master_repl_offset",

// # CPU
"used_cpu_sys": "used_cpu_sys",
Expand All @@ -102,11 +108,18 @@ var (
"cluster_stats_messages_sent": "cluster_messages_sent_total",
"cluster_stats_messages_received": "cluster_messages_received_total",
}

instanceInfoFields = map[string]bool{"redis_version": true, "redis_build_id": true, "redis_mode": true, "os": true}
)

func (e *Exporter) initGauges() {

e.metrics = map[string]*prometheus.GaugeVec{}
e.metrics["instance_info"] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: e.namespace,
Name: "instance_info",
Help: "Information about the Redis instance",
}, []string{"addr", "alias", "redis_version", "redis_build_id", "redis_mode", "os"})
e.metrics["db_keys"] = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: e.namespace,
Name: "db_keys",
Expand Down Expand Up @@ -293,6 +306,8 @@ func parseDBKeyspaceString(db string, stats string) (keysTotal float64, keysExpi
func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes chan<- scrapeResult) error {
cmdstats := false
lines := strings.Split(info, "\r\n")

instanceInfo := map[string]string{}
for _, line := range lines {
log.Debugf("info: %s", line)
if len(line) > 0 && line[0] == '#' {
Expand All @@ -308,6 +323,11 @@ 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]
continue
}

if len(split) != 2 || !includeMetric(split[0]) {
continue
}
Expand Down Expand Up @@ -383,6 +403,17 @@ func (e *Exporter) extractInfoMetrics(info, addr string, alias string, scrapes c

scrapes <- scrapeResult{Name: metricName, Addr: addr, Alias: alias, Value: val}
}

e.metricsMtx.RLock()
e.metrics["instance_info"].WithLabelValues(
addr, alias,
instanceInfo["redis_version"],
instanceInfo["redis_build_id"],
instanceInfo["redis_mode"],
instanceInfo["os"],
).Set(1)
e.metricsMtx.RUnlock()

return nil
}

Expand Down
56 changes: 48 additions & 8 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,46 @@ func TestKeyValuesAndSizes(t *testing.T) {
log.Printf("default: m: %#v", m)
}
}
for k, v := range want {
if !v {
for k, found := range want {
if !found {
t.Errorf("didn't find %s", k)
}

}
}

func TestKeyValueInvalidDB(t *testing.T) {

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

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

dontWant := map[string]bool{"test_key_size": false}
for m := range chM {
switch m.(type) {
case prometheus.Gauge:
for k := range dontWant {
if strings.Contains(m.Desc().String(), k) {
log.Println(m.Desc().String())
dontWant[k] = true
}
}
default:
log.Printf("default: m: %#v", m)
}
}
for k, found := range dontWant {
if found {
t.Errorf("we found %s but it shouldn't be there", k)
}

}
}

func TestCommandStats(t *testing.T) {

e, _ := NewRedisExporter(defaultRedisHost, "test", dbNumStrFull+"="+url.QueryEscape(keys[0]))
Expand Down Expand Up @@ -339,8 +371,8 @@ func TestCommandStats(t *testing.T) {
log.Printf("default: m: %#v", m)
}
}
for k, v := range want {
if !v {
for k, found := range want {
if !found {
t.Errorf("didn't find %s", k)
}

Expand Down Expand Up @@ -370,9 +402,17 @@ func TestHTTPEndpoint(t *testing.T) {
}

tests := []string{
// metrics
`test_connected_clients`,
`test_commands_processed_total`,
`test_key_size`,
`test_instance_info`,

// labels and label values
`addr="redis://localhost:6379"`,
`redis_mode`,
`standalone`,
`cmd="get"`,
}
for _, test := range tests {
if !bytes.Contains(body, []byte(test)) {
Expand Down Expand Up @@ -442,15 +482,15 @@ func TestMoreThanOneHost(t *testing.T) {

c, err := redis.DialURL(secondHost)
if err != nil {
fmt.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
return
}
defer c.Close()

_, err = c.Do("PING")
if err != nil {
fmt.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
return
}
Expand All @@ -464,15 +504,15 @@ func TestMoreThanOneHost(t *testing.T) {

_, err = c.Do("SELECT", dbNumStr)
if err != nil {
fmt.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
return
}

secondHostValue := float64(5678.9)
_, err = c.Do("SET", keys[0], secondHostValue)
if err != nil {
fmt.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
return
}
Expand Down

0 comments on commit a347d08

Please sign in to comment.