Skip to content

Commit

Permalink
[receiver/redis] include server.address and server.port resource attr…
Browse files Browse the repository at this point in the history
…ibutes (#26707)

**Description:** <Describe what has changed.>

Two new resource attributes have been added: `server.address` and
`server.port`. This is change aligned with [semantic
conventions](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/semantic-conventions.md?plain=1#L30)
and allows users identifying a particular redis server.


**Link to tracking Issue:** #22044

**Testing:** <Describe what testing was performed and which tests were
added.>

```
❯ go test -race -timeout 300s -parallel 4 --tags="",integration --count 1 ./...
ok      github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver        6.179s
ok      github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver/internal/metadata      1.464s
```

* Added: new test to check error when the configuration endpoint is
missing or invalid
* Updated: integration test to check new resource attributes

**Documentation:** <Describe the documentation added.>
New attribute documentation was generated using `mdatagen`

---------

Co-authored-by: Antoine Toulme <[email protected]>
  • Loading branch information
sigilioso and atoulme authored Nov 14, 2023
1 parent d2b5291 commit dbdb683
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 15 deletions.
27 changes: 27 additions & 0 deletions .chloggen/redis-receiver-server-address-and-port.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: include server.address and server.port resource attributes

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

# (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]
6 changes: 6 additions & 0 deletions cmd/otelcontribcol/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mongodbatlasreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/otlpjsonfilereceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snmpreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/syslogreceiver"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/tcplogreceiver"
Expand Down Expand Up @@ -340,6 +341,11 @@ func TestDefaultReceivers(t *testing.T) {
},
{
receiver: "redis",
getConfigFn: func() component.Config {
cfg := rcvrFactories["redis"].CreateDefaultConfig().(*redisreceiver.Config)
cfg.Endpoint = "localhost:6379"
return cfg
},
},
{
receiver: "riak",
Expand Down
17 changes: 17 additions & 0 deletions receiver/redisreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package redisreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/redisreceiver"

import (
"fmt"
"net"

"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configtls"
Expand Down Expand Up @@ -34,3 +37,17 @@ type Config struct {

MetricsBuilderConfig metadata.MetricsBuilderConfig `mapstructure:",squash"`
}

// configInfo holds configuration information to be used as resource/metrics attributes.
type configInfo struct {
Address string
Port string
}

func newConfigInfo(cfg *Config) (configInfo, error) {
address, port, err := net.SplitHostPort(cfg.Endpoint)
if err != nil {
return configInfo{}, fmt.Errorf("invalid endpoint %q: %w", cfg.Endpoint, err)
}
return configInfo{Address: address, Port: port}, nil
}
2 changes: 2 additions & 0 deletions receiver/redisreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,5 @@ Redis node's role
| Name | Description | Values | Enabled |
| ---- | ----------- | ------ | ------- |
| redis.version | Redis server's version. | Any Str | true |
| server.address | Redis server's address | Any Str | false |
| server.port | Redis server's port | Any Str | false |
6 changes: 6 additions & 0 deletions receiver/redisreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func TestIntegration(t *testing.T) {
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.ChangeResourceAttributeValue("server.address", func(_ string) string {
return "localhost"
}),
pmetrictest.ChangeResourceAttributeValue("server.port", func(_ string) string {
return redisPort
}),
),
).Run(t)
}
10 changes: 9 additions & 1 deletion receiver/redisreceiver/internal/metadata/generated_config.go

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

16 changes: 12 additions & 4 deletions receiver/redisreceiver/internal/metadata/generated_config_test.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.

14 changes: 14 additions & 0 deletions receiver/redisreceiver/internal/metadata/generated_resource.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.

8 changes: 8 additions & 0 deletions receiver/redisreceiver/internal/metadata/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ all_set:
resource_attributes:
redis.version:
enabled: true
server.address:
enabled: true
server.port:
enabled: true
none_set:
metrics:
redis.clients.blocked:
Expand Down Expand Up @@ -145,3 +149,7 @@ none_set:
resource_attributes:
redis.version:
enabled: false
server.address:
enabled: false
server.port:
enabled: false
8 changes: 8 additions & 0 deletions receiver/redisreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ resource_attributes:
description: Redis server's version.
enabled: true
type: string
server.address:
description: Redis server's address
enabled: false
type: string
server.port:
description: Redis server's port
enabled: false
type: string

attributes:
state:
Expand Down
26 changes: 17 additions & 9 deletions receiver/redisreceiver/redis_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
// Runs intermittently, fetching info from Redis, creating metrics/datapoints,
// and feeding them to a metricsConsumer.
type redisScraper struct {
client client
redisSvc *redisSvc
settings component.TelemetrySettings
mb *metadata.MetricsBuilder
uptime time.Duration
client client
redisSvc *redisSvc
settings component.TelemetrySettings
mb *metadata.MetricsBuilder
uptime time.Duration
configInfo configInfo
}

const redisMaxDbs = 16 // Maximum possible number of redis databases
Expand All @@ -48,11 +49,16 @@ func newRedisScraper(cfg *Config, settings receiver.CreateSettings) (scraperhelp
}

func newRedisScraperWithClient(client client, settings receiver.CreateSettings, cfg *Config) (scraperhelper.Scraper, error) {
configInfo, err := newConfigInfo(cfg)
if err != nil {
return nil, err
}
rs := &redisScraper{
client: client,
redisSvc: newRedisSvc(client),
settings: settings.TelemetrySettings,
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
client: client,
redisSvc: newRedisSvc(client),
settings: settings.TelemetrySettings,
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings),
configInfo: configInfo,
}
return scraperhelper.NewScraper(
metadata.Type,
Expand Down Expand Up @@ -96,6 +102,8 @@ func (rs *redisScraper) Scrape(context.Context) (pmetric.Metrics, error) {
rs.recordCmdMetrics(now, inf)
rb := rs.mb.NewResourceBuilder()
rb.SetRedisVersion(rs.getRedisVersion(inf))
rb.SetServerAddress(rs.configInfo.Address)
rb.SetServerPort(rs.configInfo.Port)
return rs.mb.Emit(metadata.WithResource(rb.Emit())), nil
}

Expand Down
8 changes: 8 additions & 0 deletions receiver/redisreceiver/redis_scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestRedisRunnable(t *testing.T) {
settings := receivertest.NewNopCreateSettings()
settings.Logger = logger
cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:6379"
rs := &redisScraper{mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings)}
runner, err := newRedisScraperWithClient(newFakeClient(), settings, cfg)
require.NoError(t, err)
Expand All @@ -35,6 +36,13 @@ func TestRedisRunnable(t *testing.T) {
assert.Equal(t, "otelcol/redisreceiver", il.Name())
}

func TestNewReceiver_invalid_endpoint(t *testing.T) {
c := createDefaultConfig().(*Config)
_, err := createMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), c, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid endpoint")
}

func TestNewReceiver_invalid_auth_error(t *testing.T) {
c := createDefaultConfig().(*Config)
c.TLS = configtls.TLSClientSetting{
Expand Down

0 comments on commit dbdb683

Please sign in to comment.