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

[chore] [receiver/couchdb] Use confighttp.NewDefaultClientConfig instead of manually creating struct #35613

Merged
Merged
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
46 changes: 25 additions & 21 deletions receiver/couchdbreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ func defaultClient(t *testing.T, endpoint string) client {
}

func TestNewCouchDBClient(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint
clientConfig.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}
t.Run("Invalid config", func(t *testing.T) {
couchdbClient, err := newCouchDBClient(
context.Background(),
&Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
},
}},
ClientConfig: clientConfig,
},
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings())

Expand Down Expand Up @@ -107,14 +108,15 @@ func TestGet(t *testing.T) {
})
t.Run("401 Unauthorized", func(t *testing.T) {
url := ts.URL + "/_node/_local/_stats/couchdb"
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = url

couchdbClient, err := newCouchDBClient(
context.Background(),
&Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: url,
},
Username: "unauthorized",
Password: "unauthorized",
ClientConfig: clientConfig,
Username: "unauthorized",
Password: "unauthorized",
},
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings())
Expand Down Expand Up @@ -179,14 +181,15 @@ func TestGetNodeStats(t *testing.T) {
}

func TestBuildReq(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint

couchdbClient := couchDBClient{
client: &http.Client{},
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
},
Username: "otelu",
Password: "otelp",
ClientConfig: clientConfig,
Username: "otelu",
Password: "otelp",
},
logger: zap.NewNop(),
}
Expand All @@ -201,12 +204,13 @@ func TestBuildReq(t *testing.T) {
}

func TestBuildBadReq(t *testing.T) {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint

couchdbClient := couchDBClient{
client: &http.Client{},
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
},
ClientConfig: clientConfig,
},
logger: zap.NewNop(),
}
Expand Down
34 changes: 15 additions & 19 deletions receiver/couchdbreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import (
)

func TestValidate(t *testing.T) {
clientConfigInvalid := confighttp.NewDefaultClientConfig()
clientConfigInvalid.Endpoint = "http://localhost :5984"

clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = "http://localhost:5984"

testCases := []struct {
desc string
cfg *Config
Expand All @@ -28,9 +34,7 @@ func TestValidate(t *testing.T) {
{
desc: "missing username, password and invalid endpoint",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "http://localhost :5984",
},
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: multierr.Combine(
Expand All @@ -42,9 +46,7 @@ func TestValidate(t *testing.T) {
{
desc: "missing password and invalid endpoint",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "http://localhost :5984",
},
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Username: "otelu",
},
Expand All @@ -56,9 +58,7 @@ func TestValidate(t *testing.T) {
{
desc: "missing username and invalid endpoint",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "http://localhost :5984",
},
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Password: "otelp",
},
Expand All @@ -70,23 +70,19 @@ func TestValidate(t *testing.T) {
{
desc: "invalid endpoint",
cfg: &Config{
Username: "otel",
Password: "otel",
ClientConfig: confighttp.ClientConfig{
Endpoint: "http://localhost :5984",
},
Username: "otel",
Password: "otel",
ClientConfig: clientConfigInvalid,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: fmt.Errorf(errInvalidEndpoint.Error(), "parse \"http://localhost :5984\": invalid character \" \" in host name"),
},
{
desc: "no error",
cfg: &Config{
Username: "otel",
Password: "otel",
ClientConfig: confighttp.ClientConfig{
Endpoint: "http://localhost:5984",
},
Username: "otel",
Password: "otel",
ClientConfig: clientConfig,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: nil,
Expand Down
10 changes: 5 additions & 5 deletions receiver/couchdbreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func NewFactory() receiver.Factory {
}

func createDefaultConfig() component.Config {
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.TLSSetting = configtls.ClientConfig{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clientConfig.TLSSetting = configtls.ClientConfig{}
clientConfig.TLSSetting = configtls.NewDefaultClientConfig()

Should we update this reference too it's the same underlying concern?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are for confighttp. I can create another issue for configtls similarely to #35457 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, another issue would probably be good. It looks like the goal is to move all configs over the the NewDefault... initialization, as noted here, but I understand it's unrelated to this PR so we don't have to do it yet. 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clientConfig.Endpoint = defaultEndpoint
clientConfig.Timeout = 1 * time.Minute
return &Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
ClientConfig: confighttp.ClientConfig{
TLSSetting: configtls.ClientConfig{},
Endpoint: defaultEndpoint,
Timeout: 1 * time.Minute,
},
ClientConfig: clientConfig,
}
}

Expand Down
2 changes: 1 addition & 1 deletion receiver/couchdbreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestMetricSettings(t *testing.T) {
CouchdbHttpdViews: metadata.MetricConfig{Enabled: false},
}
cfg := &Config{
ClientConfig: confighttp.ClientConfig{},
ClientConfig: confighttp.NewDefaultClientConfig(),
MetricsBuilderConfig: mbc,
}
scraper := newCouchdbScraper(receivertest.NewNopSettings(), cfg)
Expand Down