Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] [receiver/simpleprometheus] Use confighttp.NewDefaultClientConfig instead of manually creating struct #35652

Merged
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,
fatsheep9146 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading