Skip to content

Commit

Permalink
[receiver/redis] Command latency metrics. (open-telemetry#26569)
Browse files Browse the repository at this point in the history
**Description:** Added command latency metric to the redis receiver.

**Link to tracking Issue:**
open-telemetry#6942

**Testing:** Added tests for parsing `latencystats` section of redis
info.

**Documentation:** Metric documentation in metadata.yaml
  • Loading branch information
bjandras authored and jorgeancal committed Sep 18, 2023
1 parent 724aa3f commit 40edc63
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 23 deletions.
27 changes: 27 additions & 0 deletions .chloggen/redisreceiver_cmd_latency.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: redisreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added `redis.cmd.latency` metric.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [6942]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
15 changes: 15 additions & 0 deletions receiver/redisreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,21 @@ Total number of calls for a command
| ---- | ----------- | ------ |
| cmd | Redis command name | Any Str |

### redis.cmd.latency

Command execution latency

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| us | Gauge | Double |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| cmd | Redis command name | Any Str |
| percentile | Percentile | Str: ``p50``, ``p99``, ``p99.9`` |

### redis.cmd.usec

Total time for all executions of this command
Expand Down
4 changes: 4 additions & 0 deletions receiver/redisreceiver/internal/metadata/generated_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions receiver/redisreceiver/internal/metadata/generated_metrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions receiver/redisreceiver/internal/metadata/generated_metrics_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions receiver/redisreceiver/internal/metadata/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ all_set:
enabled: true
redis.cmd.calls:
enabled: true
redis.cmd.latency:
enabled: true
redis.cmd.usec:
enabled: true
redis.commands:
Expand Down Expand Up @@ -82,6 +84,8 @@ none_set:
enabled: false
redis.cmd.calls:
enabled: false
redis.cmd.latency:
enabled: false
redis.cmd.usec:
enabled: false
redis.commands:
Expand Down
40 changes: 40 additions & 0 deletions receiver/redisreceiver/latencystats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package redisreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"

import (
"fmt"
"strconv"
"strings"
)

// Holds percentile latencies, e.g. "p99" -> 1.5.
type latencies map[string]float64

// parseLatencyStats parses the values part of one entry in Redis latencystats section,
// e.g. "p50=181.247,p99=309.247,p99.9=1023.999".
func parseLatencyStats(str string) (latencies, error) {
res := make(latencies)

pairs := strings.Split(strings.TrimSpace(str), ",")

for _, pairStr := range pairs {
pair := strings.Split(pairStr, "=")
if len(pair) != 2 {
return nil, fmt.Errorf("unexpected latency percentiles pair '%s'", pairStr)
}

key := pair[0]
valueStr := pair[1]

value, err := strconv.ParseFloat(valueStr, 64)
if err != nil {
return nil, err
}

res[key] = value
}

return res, nil
}
34 changes: 34 additions & 0 deletions receiver/redisreceiver/latencystats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package redisreceiver

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestParseLatencyStats(t *testing.T) {
ls, err := parseLatencyStats("p50=181.247,p55=182.271,p99=309.247,p99.9=1023.999")
require.Nil(t, err)
require.Equal(t, ls["p50"], 181.247)
require.Equal(t, ls["p55"], 182.271)
require.Equal(t, ls["p99"], 309.247)
require.Equal(t, ls["p99.9"], 1023.999)
}

func TestParseMalformedLatencyStats(t *testing.T) {
tests := []struct{ name, stats string }{
{"missing value", "p50=42.0,p90=50.0,p99.9="},
{"missing equals", "p50=42.0,p90=50.0,p99.9"},
{"extra comma", "p50=42.0,,p90=50.0"},
{"wrong value type", "p50=asdf"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := parseLatencyStats(test.stats)
require.NotNil(t, err)
})
}
}
15 changes: 15 additions & 0 deletions receiver/redisreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ attributes:
cmd:
description: Redis command name
type: string
percentile:
description: Percentile
type: string
enum:
- p50
- p99
- p99.9

metrics:
redis.maxmemory:
Expand Down Expand Up @@ -84,6 +91,14 @@ metrics:
aggregation_temporality: cumulative
attributes: [cmd]

redis.cmd.latency:
enabled: false
description: Command execution latency
unit: us
gauge:
value_type: double
attributes: [cmd, percentile]

redis.uptime:
enabled: true
description: Number of seconds since Redis server start
Expand Down
Loading

0 comments on commit 40edc63

Please sign in to comment.