From dd31b011fd76e9f4ac4a401c83ca15a7432182f4 Mon Sep 17 00:00:00 2001 From: mackjmr Date: Mon, 7 Oct 2024 11:14:43 -0400 Subject: [PATCH 1/2] [chore] [receiver/simpleprometheus] Use confighttp.NewDefaultClientConfig instead of manually creating struct **Description:** This PR makes usage of `NewDefaultClientConfig` instead of manually creating the confighttp.ClientConfig struct. **Link to tracking Issue:** #35457 --- .../simpleprometheusreceiver/config_test.go | 43 +++++----- receiver/simpleprometheusreceiver/factory.go | 12 +-- .../simpleprometheusreceiver/receiver_test.go | 85 +++++++++---------- 3 files changed, 69 insertions(+), 71 deletions(-) diff --git a/receiver/simpleprometheusreceiver/config_test.go b/receiver/simpleprometheusreceiver/config_test.go index 2fa4521019ca..7951d4465b41 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", Params: url.Values{"columns": []string{"name", "messages"}, "key": []string{"foo", "bar"}}, @@ -56,12 +66,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", }, @@ -69,9 +74,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 77870dcb4fe7..66047a02d4a8 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"}}, @@ -114,15 +127,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", }, @@ -160,9 +165,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{ @@ -207,6 +210,21 @@ func TestGetPrometheusConfig(t *testing.T) { } func TestGetPrometheusConfigWrapper(t *testing.T) { + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint + clientConfig.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 @@ -222,12 +240,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { InsecureSkipVerify: true, }, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfig, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -270,12 +283,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { httpConfig: httpConfig{ TLSEnabled: false, }, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - TLSSetting: configtls.ClientConfig{ - Insecure: true, - }, - }, + ClientConfig: clientConfig, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -312,12 +320,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"}}, @@ -354,15 +357,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"}}, From 085b7ba818b11eb9ee145bff76d24b42ebe50a5c Mon Sep 17 00:00:00 2001 From: mackjmr Date: Mon, 16 Dec 2024 10:25:34 +0100 Subject: [PATCH 2/2] fix test --- receiver/simpleprometheusreceiver/receiver_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/receiver/simpleprometheusreceiver/receiver_test.go b/receiver/simpleprometheusreceiver/receiver_test.go index d9810c11389b..b246b9b6046c 100644 --- a/receiver/simpleprometheusreceiver/receiver_test.go +++ b/receiver/simpleprometheusreceiver/receiver_test.go @@ -248,7 +248,11 @@ func TestGetPrometheusConfig(t *testing.T) { func TestGetPrometheusConfigWrapper(t *testing.T) { clientConfig := confighttp.NewDefaultClientConfig() clientConfig.Endpoint = defaultEndpoint - clientConfig.TLSSetting = configtls.ClientConfig{ + clientConfig.TLSSetting = configtls.ClientConfig{} + + clientConfigInsecure := confighttp.NewDefaultClientConfig() + clientConfigInsecure.Endpoint = defaultEndpoint + clientConfigInsecure.TLSSetting = configtls.ClientConfig{ Insecure: true, } @@ -276,7 +280,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { InsecureSkipVerify: true, }, }, - ClientConfig: clientConfig, + ClientConfig: clientConfigInsecure, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}}, @@ -319,7 +323,7 @@ func TestGetPrometheusConfigWrapper(t *testing.T) { httpConfig: httpConfig{ TLSEnabled: false, }, - ClientConfig: clientConfig, + ClientConfig: clientConfigInsecure, CollectionInterval: 10 * time.Second, MetricsPath: "/metric", Params: url.Values{"foo": []string{"bar", "foobar"}},