From def91cd7b6d910add1e0f7109d28b534332eaa51 Mon Sep 17 00:00:00 2001 From: Mackenzie <63265430+mackjmr@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:46:24 -0400 Subject: [PATCH] [chore] [receiver/flinkmetrics] Use confighttp.NewDefaultClientConfig instead of manually creating struct (#35615) **Description:** This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. **Link to tracking Issue:** #35457 --- receiver/flinkmetricsreceiver/client_test.go | 26 ++++++++++--------- receiver/flinkmetricsreceiver/config_test.go | 13 +++++----- receiver/flinkmetricsreceiver/factory.go | 10 +++---- receiver/flinkmetricsreceiver/factory_test.go | 8 +++--- receiver/flinkmetricsreceiver/scraper_test.go | 26 ++++++++++--------- 5 files changed, 44 insertions(+), 39 deletions(-) diff --git a/receiver/flinkmetricsreceiver/client_test.go b/receiver/flinkmetricsreceiver/client_test.go index 2403e97c36c5..2f189318560c 100644 --- a/receiver/flinkmetricsreceiver/client_test.go +++ b/receiver/flinkmetricsreceiver/client_test.go @@ -48,6 +48,18 @@ const ( ) func TestNewClient(t *testing.T) { + clientConfigNoCA := confighttp.NewDefaultClientConfig() + clientConfigNoCA.Endpoint = defaultEndpoint + clientConfigNoCA.TLSSetting = configtls.ClientConfig{ + Config: configtls.Config{ + CAFile: "/non/existent", + }, + } + + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.TLSSetting = configtls.ClientConfig{} + clientConfig.Endpoint = defaultEndpoint + testCase := []struct { desc string cfg *Config @@ -59,14 +71,7 @@ func TestNewClient(t *testing.T) { { desc: "Invalid HTTP config", cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Config: configtls.Config{ - CAFile: "/non/existent", - }, - }, - }, + ClientConfig: clientConfigNoCA, }, host: componenttest.NewNopHost(), settings: componenttest.NewNopTelemetrySettings(), @@ -76,10 +81,7 @@ func TestNewClient(t *testing.T) { { desc: "Valid Configuration", cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - TLSSetting: configtls.ClientConfig{}, - Endpoint: defaultEndpoint, - }, + ClientConfig: clientConfig, }, host: componenttest.NewNopHost(), settings: componenttest.NewNopTelemetrySettings(), diff --git a/receiver/flinkmetricsreceiver/config_test.go b/receiver/flinkmetricsreceiver/config_test.go index e228c2e9461e..44751d4317ec 100644 --- a/receiver/flinkmetricsreceiver/config_test.go +++ b/receiver/flinkmetricsreceiver/config_test.go @@ -20,6 +20,11 @@ import ( ) func TestValidate(t *testing.T) { + clientConfigInvalidEndpoint := confighttp.NewDefaultClientConfig() + clientConfigInvalidEndpoint.Endpoint = "invalid://endpoint: 12efg" + + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint testCases := []struct { desc string cfg *Config @@ -28,9 +33,7 @@ func TestValidate(t *testing.T) { { desc: "invalid endpoint", cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "invalid://endpoint: 12efg", - }, + ClientConfig: clientConfigInvalidEndpoint, ControllerConfig: scraperhelper.NewDefaultControllerConfig(), }, expectedErr: fmt.Errorf("\"endpoint\" must be in the form of ://:: %w", errors.New(`parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`)), @@ -38,9 +41,7 @@ func TestValidate(t *testing.T) { { desc: "valid config", cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - }, + ClientConfig: clientConfig, ControllerConfig: scraperhelper.NewDefaultControllerConfig(), }, expectedErr: nil, diff --git a/receiver/flinkmetricsreceiver/factory.go b/receiver/flinkmetricsreceiver/factory.go index 4fba4e426fa8..b84648d484df 100644 --- a/receiver/flinkmetricsreceiver/factory.go +++ b/receiver/flinkmetricsreceiver/factory.go @@ -31,13 +31,13 @@ func NewFactory() receiver.Factory { func createDefaultConfig() component.Config { cfg := scraperhelper.NewDefaultControllerConfig() cfg.CollectionInterval = 10 * time.Second + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint + clientConfig.Timeout = 10 * time.Second return &Config{ - ControllerConfig: cfg, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - Timeout: 10 * time.Second, - }, + ControllerConfig: cfg, + ClientConfig: clientConfig, MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), } } diff --git a/receiver/flinkmetricsreceiver/factory_test.go b/receiver/flinkmetricsreceiver/factory_test.go index 10eba95bc35f..be87adb43a46 100644 --- a/receiver/flinkmetricsreceiver/factory_test.go +++ b/receiver/flinkmetricsreceiver/factory_test.go @@ -19,6 +19,9 @@ import ( ) func TestNewFactory(t *testing.T) { + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint + clientConfig.Timeout = 10 * time.Second testCases := []struct { desc string testFunc func(*testing.T) @@ -40,10 +43,7 @@ func TestNewFactory(t *testing.T) { CollectionInterval: 10 * time.Second, InitialDelay: time.Second, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - Timeout: 10 * time.Second, - }, + ClientConfig: clientConfig, MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), } diff --git a/receiver/flinkmetricsreceiver/scraper_test.go b/receiver/flinkmetricsreceiver/scraper_test.go index 37c56c20c7a3..6f318752cd3f 100644 --- a/receiver/flinkmetricsreceiver/scraper_test.go +++ b/receiver/flinkmetricsreceiver/scraper_test.go @@ -33,6 +33,18 @@ var ( ) func TestScraperStart(t *testing.T) { + clientConfigNoCA := confighttp.NewDefaultClientConfig() + clientConfigNoCA.Endpoint = defaultEndpoint + clientConfigNoCA.TLSSetting = configtls.ClientConfig{ + Config: configtls.Config{ + CAFile: "/non/existent", + }, + } + + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.TLSSetting = configtls.ClientConfig{} + clientConfig.Endpoint = defaultEndpoint + testcases := []struct { desc string scraper *flinkmetricsScraper @@ -42,14 +54,7 @@ func TestScraperStart(t *testing.T) { desc: "Bad Config", scraper: &flinkmetricsScraper{ cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Config: configtls.Config{ - CAFile: "/non/existent", - }, - }, - }, + ClientConfig: clientConfigNoCA, }, settings: componenttest.NewNopTelemetrySettings(), }, @@ -59,10 +64,7 @@ func TestScraperStart(t *testing.T) { desc: "Valid Config", scraper: &flinkmetricsScraper{ cfg: &Config{ - ClientConfig: confighttp.ClientConfig{ - TLSSetting: configtls.ClientConfig{}, - Endpoint: defaultEndpoint, - }, + ClientConfig: clientConfig, }, settings: componenttest.NewNopTelemetrySettings(), },