Skip to content

Commit fc405e3

Browse files
authored
[chore] [receiver/couchdb] Use confighttp.NewDefaultClientConfig instead of manually creating struct (open-telemetry#35613)
**Description:** This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. **Link to tracking Issue:** open-telemetry#35457
1 parent 9a2aa1d commit fc405e3

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

receiver/couchdbreceiver/client_test.go

+25-21
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ func defaultClient(t *testing.T, endpoint string) client {
3333
}
3434

3535
func TestNewCouchDBClient(t *testing.T) {
36+
clientConfig := confighttp.NewDefaultClientConfig()
37+
clientConfig.Endpoint = defaultEndpoint
38+
clientConfig.TLSSetting = configtls.ClientConfig{
39+
Config: configtls.Config{
40+
CAFile: "/non/existent",
41+
},
42+
}
3643
t.Run("Invalid config", func(t *testing.T) {
3744
couchdbClient, err := newCouchDBClient(
3845
context.Background(),
3946
&Config{
40-
ClientConfig: confighttp.ClientConfig{
41-
Endpoint: defaultEndpoint,
42-
TLSSetting: configtls.ClientConfig{
43-
Config: configtls.Config{
44-
CAFile: "/non/existent",
45-
},
46-
},
47-
}},
47+
ClientConfig: clientConfig,
48+
},
4849
componenttest.NewNopHost(),
4950
componenttest.NewNopTelemetrySettings())
5051

@@ -107,14 +108,15 @@ func TestGet(t *testing.T) {
107108
})
108109
t.Run("401 Unauthorized", func(t *testing.T) {
109110
url := ts.URL + "/_node/_local/_stats/couchdb"
111+
clientConfig := confighttp.NewDefaultClientConfig()
112+
clientConfig.Endpoint = url
113+
110114
couchdbClient, err := newCouchDBClient(
111115
context.Background(),
112116
&Config{
113-
ClientConfig: confighttp.ClientConfig{
114-
Endpoint: url,
115-
},
116-
Username: "unauthorized",
117-
Password: "unauthorized",
117+
ClientConfig: clientConfig,
118+
Username: "unauthorized",
119+
Password: "unauthorized",
118120
},
119121
componenttest.NewNopHost(),
120122
componenttest.NewNopTelemetrySettings())
@@ -179,14 +181,15 @@ func TestGetNodeStats(t *testing.T) {
179181
}
180182

181183
func TestBuildReq(t *testing.T) {
184+
clientConfig := confighttp.NewDefaultClientConfig()
185+
clientConfig.Endpoint = defaultEndpoint
186+
182187
couchdbClient := couchDBClient{
183188
client: &http.Client{},
184189
cfg: &Config{
185-
ClientConfig: confighttp.ClientConfig{
186-
Endpoint: defaultEndpoint,
187-
},
188-
Username: "otelu",
189-
Password: "otelp",
190+
ClientConfig: clientConfig,
191+
Username: "otelu",
192+
Password: "otelp",
190193
},
191194
logger: zap.NewNop(),
192195
}
@@ -201,12 +204,13 @@ func TestBuildReq(t *testing.T) {
201204
}
202205

203206
func TestBuildBadReq(t *testing.T) {
207+
clientConfig := confighttp.NewDefaultClientConfig()
208+
clientConfig.Endpoint = defaultEndpoint
209+
204210
couchdbClient := couchDBClient{
205211
client: &http.Client{},
206212
cfg: &Config{
207-
ClientConfig: confighttp.ClientConfig{
208-
Endpoint: defaultEndpoint,
209-
},
213+
ClientConfig: clientConfig,
210214
},
211215
logger: zap.NewNop(),
212216
}

receiver/couchdbreceiver/config_test.go

+15-19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ import (
2020
)
2121

2222
func TestValidate(t *testing.T) {
23+
clientConfigInvalid := confighttp.NewDefaultClientConfig()
24+
clientConfigInvalid.Endpoint = "http://localhost :5984"
25+
26+
clientConfig := confighttp.NewDefaultClientConfig()
27+
clientConfig.Endpoint = "http://localhost:5984"
28+
2329
testCases := []struct {
2430
desc string
2531
cfg *Config
@@ -28,9 +34,7 @@ func TestValidate(t *testing.T) {
2834
{
2935
desc: "missing username, password and invalid endpoint",
3036
cfg: &Config{
31-
ClientConfig: confighttp.ClientConfig{
32-
Endpoint: "http://localhost :5984",
33-
},
37+
ClientConfig: clientConfigInvalid,
3438
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
3539
},
3640
expectedErr: multierr.Combine(
@@ -42,9 +46,7 @@ func TestValidate(t *testing.T) {
4246
{
4347
desc: "missing password and invalid endpoint",
4448
cfg: &Config{
45-
ClientConfig: confighttp.ClientConfig{
46-
Endpoint: "http://localhost :5984",
47-
},
49+
ClientConfig: clientConfigInvalid,
4850
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
4951
Username: "otelu",
5052
},
@@ -56,9 +58,7 @@ func TestValidate(t *testing.T) {
5658
{
5759
desc: "missing username and invalid endpoint",
5860
cfg: &Config{
59-
ClientConfig: confighttp.ClientConfig{
60-
Endpoint: "http://localhost :5984",
61-
},
61+
ClientConfig: clientConfigInvalid,
6262
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
6363
Password: "otelp",
6464
},
@@ -70,23 +70,19 @@ func TestValidate(t *testing.T) {
7070
{
7171
desc: "invalid endpoint",
7272
cfg: &Config{
73-
Username: "otel",
74-
Password: "otel",
75-
ClientConfig: confighttp.ClientConfig{
76-
Endpoint: "http://localhost :5984",
77-
},
73+
Username: "otel",
74+
Password: "otel",
75+
ClientConfig: clientConfigInvalid,
7876
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
7977
},
8078
expectedErr: fmt.Errorf(errInvalidEndpoint.Error(), "parse \"http://localhost :5984\": invalid character \" \" in host name"),
8179
},
8280
{
8381
desc: "no error",
8482
cfg: &Config{
85-
Username: "otel",
86-
Password: "otel",
87-
ClientConfig: confighttp.ClientConfig{
88-
Endpoint: "http://localhost:5984",
89-
},
83+
Username: "otel",
84+
Password: "otel",
85+
ClientConfig: clientConfig,
9086
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
9187
},
9288
expectedErr: nil,

receiver/couchdbreceiver/factory.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ func NewFactory() receiver.Factory {
2626
}
2727

2828
func createDefaultConfig() component.Config {
29+
clientConfig := confighttp.NewDefaultClientConfig()
30+
clientConfig.TLSSetting = configtls.ClientConfig{}
31+
clientConfig.Endpoint = defaultEndpoint
32+
clientConfig.Timeout = 1 * time.Minute
2933
return &Config{
3034
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
3135
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
32-
ClientConfig: confighttp.ClientConfig{
33-
TLSSetting: configtls.ClientConfig{},
34-
Endpoint: defaultEndpoint,
35-
Timeout: 1 * time.Minute,
36-
},
36+
ClientConfig: clientConfig,
3737
}
3838
}
3939

receiver/couchdbreceiver/scraper_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestMetricSettings(t *testing.T) {
158158
CouchdbHttpdViews: metadata.MetricConfig{Enabled: false},
159159
}
160160
cfg := &Config{
161-
ClientConfig: confighttp.ClientConfig{},
161+
ClientConfig: confighttp.NewDefaultClientConfig(),
162162
MetricsBuilderConfig: mbc,
163163
}
164164
scraper := newCouchdbScraper(receivertest.NewNopSettings(), cfg)

0 commit comments

Comments
 (0)