Skip to content

Commit

Permalink
feat: add latency percentiles metrics to Redis input
Browse files Browse the repository at this point in the history
  • Loading branch information
fretb committed May 3, 2024
1 parent 59a7eab commit b2a165b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
11 changes: 10 additions & 1 deletion plugins/inputs/redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ and the elapsed time since the last rdb save (rdb\_last\_save\_time\_elapsed).
- usec(int, mircoseconds)
- usec_per_call(float, microseconds)

- redis_latency_percentiles_usec
- fields:
- p50(float, microseconds)
- p99(float, microseconds)
- p99.9(float, microseconds)

- redis_replication
- tags:
- replication_role
Expand Down Expand Up @@ -187,7 +193,10 @@ and the elapsed time since the last rdb save (rdb\_last\_save\_time\_elapsed).
- The redis_cmdstat measurement has an additional tag:
- command

## Example Output
- The redis_latency_percentiles_usec measurement has an additional tag:
- command

### Example Output:

Using this configuration:

Expand Down
45 changes: 44 additions & 1 deletion plugins/inputs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ func gatherInfoOutput(
gatherCommandstateLine(name, kline, acc, tags)
continue
}
if section == "Latencystats" {
kline := strings.TrimSpace(parts[1])
gatherLatencystatsLine(name, kline, acc, tags)
continue
}
if section == "Replication" && replicationSlaveMetricPrefix.MatchString(name) {
kline := strings.TrimSpace(parts[1])
gatherReplicationLine(name, kline, acc, tags)
Expand Down Expand Up @@ -515,7 +520,7 @@ func gatherKeyspaceLine(
//
// cmdstat_publish:calls=33791,usec=208789,usec_per_call=6.18
//
// Tag: cmdstat=publish; Fields: calls=33791i,usec=208789i,usec_per_call=6.18
// Tag: command=publish; Fields: calls=33791i,usec=208789i,usec_per_call=6.18
func gatherCommandstateLine(
name string,
line string,
Expand Down Expand Up @@ -557,6 +562,44 @@ func gatherCommandstateLine(
acc.AddFields("redis_cmdstat", fields, tags)
}

// Parse the special latency_percentiles_usec lines.
// Example:
// latency_percentiles_usec_zadd:p50=9.023,p99=28.031,p99.9=43.007
// Tag: command=zadd; Fields: p50=9.023,p99=28.031,p99.9=43.007
func gatherLatencystatsLine(
name string,
line string,
acc telegraf.Accumulator,
globalTags map[string]string,
) {
if !strings.HasPrefix(name, "latency_percentiles_usec") {
return
}

fields := make(map[string]interface{})
tags := make(map[string]string)
for k, v := range globalTags {
tags[k] = v
}
tags["command"] = strings.TrimPrefix(name, "latency_percentiles_usec_")
parts := strings.Split(line, ",")
for _, part := range parts {
kv := strings.Split(part, "=")
if len(kv) != 2 {
continue
}

switch kv[0] {
case "p50", "p99", "p99.9":
fval, err := strconv.ParseFloat(kv[1], 64)
if err == nil {
fields[kv[0]] = fval
}
}
}
acc.AddFields("redis_latency_percentiles_usec", fields, tags)
}

// Parse the special Replication line
// Example:
//
Expand Down
20 changes: 20 additions & 0 deletions plugins/inputs/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,22 @@ func TestRedis_ParseMetrics(t *testing.T) {
}
acc.AssertContainsTaggedFields(t, "redis_cmdstat", cmdstatPublishFields, cmdstatPublishTags)

latencyZaddTags := map[string]string{"host": "redis.net", "replication_role": "master", "command": "zadd"}
latencyZaddFields := map[string]interface{}{
"p50": float64(9.023),
"p99": float64(28.031),
"p99.9": float64(43.007),
}
acc.AssertContainsTaggedFields(t, "redis_latency_percentiles_usec", latencyZaddFields, latencyZaddTags)

latencyHgetallTags := map[string]string{"host": "redis.net", "replication_role": "master", "command": "hgetall"}
latencyHgetallFields := map[string]interface{}{
"p50": float64(11.007),
"p99": float64(34.047),
"p99.9": float64(66.047),
}
acc.AssertContainsTaggedFields(t, "redis_latency_percentiles_usec", latencyHgetallFields, latencyHgetallTags)

replicationTags := map[string]string{
"host": "redis.net",
"replication_role": "slave",
Expand Down Expand Up @@ -541,6 +557,10 @@ errorstat_NOSCRIPT:count=4
errorstat_WRONGPASS:count=2
errorstat_WRONGTYPE:count=30
# Latencystats
latency_percentiles_usec_zadd:p50=9.023,p99=28.031,p99.9=43.007
latency_percentiles_usec_hgetall:p50=11.007,p99=34.047,p99.9=66.047
# Keyspace
db0:keys=2,expires=0,avg_ttl=0
Expand Down

0 comments on commit b2a165b

Please sign in to comment.