Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow URL format in Metricbeat Redis module #10920

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metricbeat/mb/parse/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func SetURLUser(u *url.URL, defaultUser, defaultPass string) {
pass = defaultPass
}

if userIsSet && passIsSet {
if passIsSet {
u.User = url.UserPassword(user, pass)
} else if userIsSet {
u.User = url.User(user)
Expand Down
1 change: 1 addition & 0 deletions metricbeat/mb/parse/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func TestURLHostParserBuilder(t *testing.T) {
{map[string]interface{}{}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/default"},
{map[string]interface{}{"username": "guest"}, URLHostParserBuilder{}, "http://[email protected]"},
{map[string]interface{}{"username": "guest", "password": "secret"}, URLHostParserBuilder{}, "http://guest:[email protected]"},
{map[string]interface{}{"password": "secret"}, URLHostParserBuilder{}, "http://:[email protected]"},
{map[string]interface{}{"basepath": "/foo"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "foo/"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
{map[string]interface{}{"basepath": "/foo/"}, URLHostParserBuilder{DefaultPath: "/default"}, "http://example.com/foo/default"},
Expand Down
5 changes: 3 additions & 2 deletions metricbeat/module/redis/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ import (
)

var (
debugf = logp.MakeDebug("redis-info")
debugf = logp.MakeDebug("redis-info")
hostParser = parse.URLHostParserBuilder{DefaultScheme: "redis"}.Build()
)

func init() {
mb.Registry.MustAddMetricSet("redis", "info", New,
mb.WithHostParser(parse.PassThruHostParser),
mb.WithHostParser(hostParser),
mb.DefaultMetricSet(),
)
}
Expand Down
45 changes: 45 additions & 0 deletions metricbeat/module/redis/info/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package info

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/common"
mbtest "github.com/elastic/beats/metricbeat/mb/testing"
)

func TestNewMetricSet(t *testing.T) {
t.Run("pass in host", func(t *testing.T) {
c, err := common.NewConfigFrom(map[string]interface{}{
"module": "redis",
"metricsets": []string{"info"},
"hosts": []string{
"redis://me:secret@localhost:123",
},
})
if err != nil {
t.Fatal(err)
}

ms := mbtest.NewReportingMetricSetV2(t, c)
assert.Equal(t, "secret", ms.HostData().Password)
})

t.Run("password in config", func(t *testing.T) {
c, err := common.NewConfigFrom(map[string]interface{}{
"module": "redis",
"metricsets": []string{"info"},
"hosts": []string{
"redis://localhost:123",
},
"password": "secret",
})
if err != nil {
t.Fatal(err)
}

ms := mbtest.NewReportingMetricSetV2(t, c)
assert.Equal(t, "secret", ms.HostData().Password)
})
}
5 changes: 3 additions & 2 deletions metricbeat/module/redis/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import (
)

var (
debugf = logp.MakeDebug("redis-key")
debugf = logp.MakeDebug("redis-key")
hostParser = parse.URLHostParserBuilder{DefaultScheme: "redis"}.Build()
)

func init() {
mb.Registry.MustAddMetricSet("redis", "key", New,
mb.WithHostParser(parse.PassThruHostParser),
mb.WithHostParser(hostParser),
)
}

Expand Down
5 changes: 3 additions & 2 deletions metricbeat/module/redis/keyspace/keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ import (
)

var (
debugf = logp.MakeDebug("redis-keyspace")
debugf = logp.MakeDebug("redis-keyspace")
hostParser = parse.URLHostParserBuilder{DefaultScheme: "redis"}.Build()
)

func init() {
mb.Registry.MustAddMetricSet("redis", "keyspace", New,
mb.WithHostParser(parse.PassThruHostParser),
mb.WithHostParser(hostParser),
mb.DefaultMetricSet(),
)
}
Expand Down
4 changes: 1 addition & 3 deletions metricbeat/module/redis/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) {
IdleTimeout time.Duration `config:"idle_timeout"`
Network string `config:"network"`
MaxConn int `config:"maxconn" validate:"min=1"`
Password string `config:"password"`
}{
Network: "tcp",
MaxConn: 10,
Password: "",
}
err := base.Module().UnpackConfig(&config)
if err != nil {
Expand All @@ -52,7 +50,7 @@ func NewMetricSet(base mb.BaseMetricSet) (*MetricSet, error) {

return &MetricSet{
BaseMetricSet: base,
pool: CreatePool(base.Host(), config.Password, config.Network,
pool: CreatePool(base.Host(), base.HostData().Password, config.Network,
config.MaxConn, config.IdleTimeout, base.Module().Config().Timeout),
}, nil
}
Expand Down