From 08e0bb4acee39a26f2227783fa22e7456bc6b1f6 Mon Sep 17 00:00:00 2001 From: Mackenzie <63265430+mackjmr@users.noreply.github.com> Date: Fri, 20 Dec 2024 12:14:21 +0100 Subject: [PATCH] [chore] [receiver/simpleprometheus] Use confighttp.NewDefaultClientConfig instead of manually creating struct (#35652) **Description:** This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. **Link to tracking Issue:** #35457 --------- Co-authored-by: Ziqi Zhao --- .../simpleprometheusreceiver/config_test.go | 43 ++++----- receiver/simpleprometheusreceiver/factory.go | 12 +-- .../simpleprometheusreceiver/receiver_test.go | 89 +++++++++---------- 3 files changed, 73 insertions(+), 71 deletions(-) diff --git a/receiver/simpleprometheusreceiver/config_test.go b/receiver/simpleprometheusreceiver/config_test.go index 49172280edd7..57165d2e22be 100644 --- a/receiver/simpleprometheusreceiver/config_test.go +++ b/receiver/simpleprometheusreceiver/config_test.go @@ -25,6 +25,26 @@ func TestLoadConfig(t *testing.T) { cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) require.NoError(t, err) + clientConfigPath := confighttp.NewDefaultClientConfig() + clientConfigPath.Endpoint = "localhost:1234" + clientConfigPath.TLSSetting = configtls.ClientConfig{ + Config: configtls.Config{ + CAFile: "path", + CertFile: "path", + KeyFile: "path", + }, + InsecureSkipVerify: true, + } + + clientConfigTLS := confighttp.NewDefaultClientConfig() + clientConfigTLS.Endpoint = "localhost:1234" + clientConfigTLS.TLSSetting = configtls.ClientConfig{ + Insecure: true, + } + + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = "localhost:1234" + tests := []struct { id component.ID expected component.Config @@ -36,17 +56,7 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "all_settings"), expected: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "localhost:1234", - TLSSetting: configtls.ClientConfig{ - Config: configtls.Config{ - CAFile: "path", - CertFile: "path", - KeyFile: "path", - }, - InsecureSkipVerify: true, - }, - }, + ClientConfig: clientConfigPath, CollectionInterval: 30 * time.Second, MetricsPath: "/v2/metrics", JobName: "job123", @@ -57,12 +67,7 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "partial_settings"), expected: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "localhost:1234", - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfigTLS, CollectionInterval: 30 * time.Second, MetricsPath: "/metrics", }, @@ -70,9 +75,7 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "partial_tls_settings"), expected: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "localhost:1234", - }, + ClientConfig: clientConfig, CollectionInterval: 30 * time.Second, MetricsPath: "/metrics", }, diff --git a/receiver/simpleprometheusreceiver/factory.go b/receiver/simpleprometheusreceiver/factory.go index 8df5b63639fc..4728ccf74556 100644 --- a/receiver/simpleprometheusreceiver/factory.go +++ b/receiver/simpleprometheusreceiver/factory.go @@ -33,13 +33,13 @@ func NewFactory() receiver.Factory { } func createDefaultConfig() component.Config { + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint + clientConfig.TLSSetting = configtls.ClientConfig{ + Insecure: true, + } return &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfig, MetricsPath: defaultMetricsPath, CollectionInterval: defaultCollectionInterval, } diff --git a/receiver/simpleprometheusreceiver/receiver_test.go b/receiver/simpleprometheusreceiver/receiver_test.go index d9b1307cea7a..b246b9b6046c 100644 --- a/receiver/simpleprometheusreceiver/receiver_test.go +++ b/receiver/simpleprometheusreceiver/receiver_test.go @@ -67,6 +67,24 @@ func TestReceiver(t *testing.T) { } func TestGetPrometheusConfig(t *testing.T) { + clientConfigTLS := confighttp.NewDefaultClientConfig() + clientConfigTLS.Endpoint = "localhost:1234" + clientConfigTLS.TLSSetting = configtls.ClientConfig{ + Insecure: true, + } + + clientConfigCA := confighttp.NewDefaultClientConfig() + clientConfigCA.Endpoint = "localhost:1234" + clientConfigCA.TLSSetting = configtls.ClientConfig{ + Config: configtls.Config{ + CAFile: "./testdata/test_cert.pem", + }, + InsecureSkipVerify: true, + } + + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = "localhost:1234" + tests := []struct { name string config *Config @@ -75,12 +93,7 @@ func TestGetPrometheusConfig(t *testing.T) { { name: "Test without TLS", config: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "localhost:1234", - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfigTLS, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -149,15 +162,7 @@ func TestGetPrometheusConfig(t *testing.T) { { name: "Test with TLS", config: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "localhost:1234", - TLSSetting: configtls.ClientConfig{ - Config: configtls.Config{ - CAFile: "./testdata/test_cert.pem", - }, - InsecureSkipVerify: true, - }, - }, + ClientConfig: clientConfigCA, CollectionInterval: 10 * time.Second, MetricsPath: "/metrics", }, @@ -195,9 +200,7 @@ func TestGetPrometheusConfig(t *testing.T) { { name: "Test with TLS - default CA", config: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "localhost:1234", - }, + ClientConfig: clientConfig, CollectionInterval: 10 * time.Second, MetricsPath: "/metrics", Labels: map[string]string{ @@ -243,6 +246,25 @@ func TestGetPrometheusConfig(t *testing.T) { } func TestGetPrometheusConfigWrapper(t *testing.T) { + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint + clientConfig.TLSSetting = configtls.ClientConfig{} + + clientConfigInsecure := confighttp.NewDefaultClientConfig() + clientConfigInsecure.Endpoint = defaultEndpoint + clientConfigInsecure.TLSSetting = configtls.ClientConfig{ + Insecure: true, + } + + clientConfigCA := confighttp.NewDefaultClientConfig() + clientConfigCA.Endpoint = defaultEndpoint + clientConfigCA.TLSSetting = configtls.ClientConfig{ + Insecure: false, + Config: configtls.Config{ + CAFile: "./testdata/test_cert.pem", + }, + } + tests := []struct { name string config *Config @@ -258,12 +280,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { InsecureSkipVerify: true, }, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfigInsecure, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -306,12 +323,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { httpConfig: httpConfig{ TLSEnabled: false, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfigInsecure, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -348,12 +360,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { httpConfig: httpConfig{ TLSEnabled: false, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: false, - }, - }, + ClientConfig: clientConfig, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -390,15 +397,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { httpConfig: httpConfig{ TLSEnabled: false, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: false, - Config: configtls.Config{ - CAFile: "./testdata/test_cert.pem", - }, - }, - }, + ClientConfig: clientConfigCA, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}},