Skip to content

Commit

Permalink
Update Redis config options (#5861)
Browse files Browse the repository at this point in the history
* Add Support for TLS & Unix

* Update Read.me

* Update Docs

* PR feedback

* Use Transport instead

* Add transport to options

* Add TLS default

* Seperate newscraper funcs

* Update Readme
  • Loading branch information
Mrod1598 authored Oct 28, 2021
1 parent 4cbdfae commit e6fab41
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 11 deletions.
6 changes: 6 additions & 0 deletions receiver/redisreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ Golang's `ParseDuration` function (example: `1h30m`). Valid time units are
- `password` (no default): The password used to access the Redis instance;
must match the password specified in the `requirepass` server configuration
option.
- `transport` (default = `tcp`) Defines the network to use for connecting to the server. Valid Values are `tcp` or `Unix`
- `tls`:
- `insecure` (default = true): whether to disable client transport security for the exporter's connection.
- `ca_file`: path to the CA cert. For a client this verifies the server certificate. Should only be used if `insecure` is set to false.
- `cert_file`: path to the TLS cert to use for TLS required connections. Should only be used if `insecure` is set to false.
- `key_file`: path to the TLS key to use for TLS required connections. Should only be used if `insecure` is set to false.

Example:

Expand Down
6 changes: 5 additions & 1 deletion receiver/redisreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@
package redisreceiver

import (
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

type Config struct {
scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
// TODO: Use one of the configs from core.
// The target endpoint.
Endpoint string `mapstructure:"endpoint"`
confignet.NetAddr `mapstructure:",squash"`

// TODO allow users to add additional resource key value pairs?

// Optional password. Must match the password specified in the
// requirepass server configuration option.
Password string `mapstructure:"password"`

TLS configtls.TLSClientSetting `mapstructure:"tls,omitempty"`
}
14 changes: 9 additions & 5 deletions receiver/redisreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"context"
"time"

"github.com/go-redis/redis/v7"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
Expand All @@ -42,6 +43,12 @@ func createDefaultConfig() config.Receiver {
scs := scraperhelper.DefaultScraperControllerSettings(typeStr)
scs.CollectionInterval = 10 * time.Second
return &Config{
NetAddr: confignet.NetAddr{
Transport: "tcp",
},
TLS: configtls.TLSClientSetting{
Insecure: true,
},
ScraperControllerSettings: scs,
}
}
Expand All @@ -54,10 +61,7 @@ func createMetricsReceiver(
) (component.MetricsReceiver, error) {
oCfg := cfg.(*Config)

scrp, err := newRedisScraper(newRedisClient(&redis.Options{
Addr: oCfg.Endpoint,
Password: oCfg.Password,
}), set)
scrp, err := newRedisScraper(*oCfg, set)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion receiver/redisreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ require (
go.opentelemetry.io/otel/trace v1.0.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08 // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions receiver/redisreceiver/go.sum

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

17 changes: 16 additions & 1 deletion receiver/redisreceiver/redis_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"time"

"github.com/go-redis/redis/v7"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/collector/receiver/scraperhelper"
Expand All @@ -33,7 +34,21 @@ type redisScraper struct {
timeBundle *timeBundle
}

func newRedisScraper(client client, settings component.ReceiverCreateSettings) (scraperhelper.Scraper, error) {
func newRedisScraper(cfg Config, settings component.ReceiverCreateSettings) (scraperhelper.Scraper, error) {
opts := &redis.Options{
Addr: cfg.Endpoint,
Password: cfg.Password,
Network: cfg.Transport,
}

var err error
if opts.TLSConfig, err = cfg.TLS.LoadTLSConfig(); err != nil {
return nil, err
}
return newRedisScraperWithClient(newRedisClient(opts), settings)
}

func newRedisScraperWithClient(client client, settings component.ReceiverCreateSettings) (scraperhelper.Scraper, error) {
rs := &redisScraper{
redisSvc: newRedisSvc(client),
redisMetrics: getDefaultRedisMetrics(),
Expand Down
15 changes: 14 additions & 1 deletion receiver/redisreceiver/redis_scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configtls"
"go.uber.org/zap"
)

func TestRedisRunnable(t *testing.T) {
logger, _ := zap.NewDevelopment()
settings := componenttest.NewNopReceiverCreateSettings()
settings.Logger = logger
runner, err := newRedisScraper(newFakeClient(), settings)
runner, err := newRedisScraperWithClient(newFakeClient(), settings)
require.NoError(t, err)
md, err := runner.Scrape(context.Background())
require.NoError(t, err)
Expand All @@ -38,5 +39,17 @@ func TestRedisRunnable(t *testing.T) {
ilm := rm.InstrumentationLibraryMetrics().At(0)
il := ilm.InstrumentationLibrary()
assert.Equal(t, "otelcol/redis", il.Name())
}

func TestNewReceiver_invalid_auth_error(t *testing.T) {
c := createDefaultConfig().(*Config)
c.TLS = configtls.TLSClientSetting{
TLSSetting: configtls.TLSSetting{
CAFile: "/invalid",
},
}
r, err := createMetricsReceiver(context.Background(), componenttest.NewNopReceiverCreateSettings(), c, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to load TLS config")
assert.Nil(t, r)
}

0 comments on commit e6fab41

Please sign in to comment.