Skip to content

Commit

Permalink
[chore] [receiver/simpleprometheus] Use confighttp.NewDefaultClientCo…
Browse files Browse the repository at this point in the history
…nfig 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 <[email protected]>
  • Loading branch information
mackjmr and fatsheep9146 authored Dec 20, 2024
1 parent 5af8c70 commit 08e0bb4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 71 deletions.
43 changes: 23 additions & 20 deletions receiver/simpleprometheusreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -57,22 +67,15 @@ 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",
},
},
{
id: component.NewIDWithName(metadata.Type, "partial_tls_settings"),
expected: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: "localhost:1234",
},
ClientConfig: clientConfig,
CollectionInterval: 30 * time.Second,
MetricsPath: "/metrics",
},
Expand Down
12 changes: 6 additions & 6 deletions receiver/simpleprometheusreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
89 changes: 44 additions & 45 deletions receiver/simpleprometheusreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"}},
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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"}},
Expand Down Expand Up @@ -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"}},
Expand Down Expand Up @@ -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"}},
Expand Down Expand Up @@ -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"}},
Expand Down

0 comments on commit 08e0bb4

Please sign in to comment.