Skip to content

Commit

Permalink
Do not try Uint parsing in redis plugin
Browse files Browse the repository at this point in the history
this is just a waste of cpu cycles, since telegraf converts all uints to
int64 anyways.
  • Loading branch information
sparrc committed Dec 20, 2016
1 parent 2f521a8 commit 1cc763e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 53 deletions.
16 changes: 5 additions & 11 deletions plugins/inputs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func gatherInfoOutput(
tags map[string]string,
) error {
var section string
var keyspace_hits, keyspace_misses uint64 = 0, 0
var keyspace_hits, keyspace_misses int64

scanner := bufio.NewScanner(rdr)
fields := make(map[string]interface{})
Expand Down Expand Up @@ -210,27 +210,21 @@ func gatherInfoOutput(

val := strings.TrimSpace(parts[1])

// Try parsing as a uint
if ival, err := strconv.ParseUint(val, 10, 64); err == nil {
// Try parsing as int
if ival, err := strconv.ParseInt(val, 10, 64); err == nil {
switch name {
case "keyspace_hits":
keyspace_hits = ival
case "keyspace_misses":
keyspace_misses = ival
case "rdb_last_save_time":
// influxdb can't calculate this, so we have to do it
fields["rdb_last_save_time_elapsed"] = uint64(time.Now().Unix()) - ival
fields["rdb_last_save_time_elapsed"] = time.Now().Unix() - ival
}
fields[metric] = ival
continue
}

// Try parsing as an int
if ival, err := strconv.ParseInt(val, 10, 64); err == nil {
fields[metric] = ival
continue
}

// Try parsing as a float
if fval, err := strconv.ParseFloat(val, 64); err == nil {
fields[metric] = fval
Expand Down Expand Up @@ -275,7 +269,7 @@ func gatherKeyspaceLine(
dbparts := strings.Split(line, ",")
for _, dbp := range dbparts {
kv := strings.Split(dbp, "=")
ival, err := strconv.ParseUint(kv[1], 10, 64)
ival, err := strconv.ParseInt(kv[1], 10, 64)
if err == nil {
fields[kv[0]] = ival
}
Expand Down
84 changes: 42 additions & 42 deletions plugins/inputs/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,53 @@ func TestRedis_ParseMetrics(t *testing.T) {

tags = map[string]string{"host": "redis.net", "replication_role": "master"}
fields := map[string]interface{}{
"uptime": uint64(238),
"lru_clock": uint64(2364819),
"clients": uint64(1),
"client_longest_output_list": uint64(0),
"client_biggest_input_buf": uint64(0),
"blocked_clients": uint64(0),
"used_memory": uint64(1003936),
"used_memory_rss": uint64(811008),
"used_memory_peak": uint64(1003936),
"used_memory_lua": uint64(33792),
"uptime": int64(238),
"lru_clock": int64(2364819),
"clients": int64(1),
"client_longest_output_list": int64(0),
"client_biggest_input_buf": int64(0),
"blocked_clients": int64(0),
"used_memory": int64(1003936),
"used_memory_rss": int64(811008),
"used_memory_peak": int64(1003936),
"used_memory_lua": int64(33792),
"mem_fragmentation_ratio": float64(0.81),
"loading": uint64(0),
"rdb_changes_since_last_save": uint64(0),
"rdb_bgsave_in_progress": uint64(0),
"rdb_last_save_time": uint64(1428427941),
"loading": int64(0),
"rdb_changes_since_last_save": int64(0),
"rdb_bgsave_in_progress": int64(0),
"rdb_last_save_time": int64(1428427941),
"rdb_last_bgsave_status": "ok",
"rdb_last_bgsave_time_sec": int64(-1),
"rdb_current_bgsave_time_sec": int64(-1),
"aof_enabled": uint64(0),
"aof_rewrite_in_progress": uint64(0),
"aof_rewrite_scheduled": uint64(0),
"aof_enabled": int64(0),
"aof_rewrite_in_progress": int64(0),
"aof_rewrite_scheduled": int64(0),
"aof_last_rewrite_time_sec": int64(-1),
"aof_current_rewrite_time_sec": int64(-1),
"aof_last_bgrewrite_status": "ok",
"aof_last_write_status": "ok",
"total_connections_received": uint64(2),
"total_commands_processed": uint64(1),
"instantaneous_ops_per_sec": uint64(0),
"total_connections_received": int64(2),
"total_commands_processed": int64(1),
"instantaneous_ops_per_sec": int64(0),
"instantaneous_input_kbps": float64(876.16),
"instantaneous_output_kbps": float64(3010.23),
"rejected_connections": uint64(0),
"sync_full": uint64(0),
"sync_partial_ok": uint64(0),
"sync_partial_err": uint64(0),
"expired_keys": uint64(0),
"evicted_keys": uint64(0),
"keyspace_hits": uint64(1),
"keyspace_misses": uint64(1),
"pubsub_channels": uint64(0),
"pubsub_patterns": uint64(0),
"latest_fork_usec": uint64(0),
"connected_slaves": uint64(0),
"master_repl_offset": uint64(0),
"repl_backlog_active": uint64(0),
"repl_backlog_size": uint64(1048576),
"repl_backlog_first_byte_offset": uint64(0),
"repl_backlog_histlen": uint64(0),
"rejected_connections": int64(0),
"sync_full": int64(0),
"sync_partial_ok": int64(0),
"sync_partial_err": int64(0),
"expired_keys": int64(0),
"evicted_keys": int64(0),
"keyspace_hits": int64(1),
"keyspace_misses": int64(1),
"pubsub_channels": int64(0),
"pubsub_patterns": int64(0),
"latest_fork_usec": int64(0),
"connected_slaves": int64(0),
"master_repl_offset": int64(0),
"repl_backlog_active": int64(0),
"repl_backlog_size": int64(1048576),
"repl_backlog_first_byte_offset": int64(0),
"repl_backlog_histlen": int64(0),
"used_cpu_sys": float64(0.14),
"used_cpu_user": float64(0.05),
"used_cpu_sys_children": float64(0.00),
Expand All @@ -102,15 +102,15 @@ func TestRedis_ParseMetrics(t *testing.T) {
}
}
assert.InDelta(t,
uint64(time.Now().Unix())-fields["rdb_last_save_time"].(uint64),
fields["rdb_last_save_time_elapsed"].(uint64),
time.Now().Unix()-fields["rdb_last_save_time"].(int64),
fields["rdb_last_save_time_elapsed"].(int64),
2) // allow for 2 seconds worth of offset

keyspaceTags := map[string]string{"host": "redis.net", "replication_role": "master", "database": "db0"}
keyspaceFields := map[string]interface{}{
"avg_ttl": uint64(0),
"expires": uint64(0),
"keys": uint64(2),
"avg_ttl": int64(0),
"expires": int64(0),
"keys": int64(2),
}
acc.AssertContainsTaggedFields(t, "redis", fields, tags)
acc.AssertContainsTaggedFields(t, "redis_keyspace", keyspaceFields, keyspaceTags)
Expand Down

0 comments on commit 1cc763e

Please sign in to comment.