Skip to content

Commit

Permalink
[chore] [receiver/flinkmetrics] Use confighttp.NewDefaultClientConfig…
Browse files Browse the repository at this point in the history
… instead of manually creating struct (open-telemetry#35615)

**Description:**
This PR makes usage of `NewDefaultClientConfig` instead of manually
creating the confighttp.ClientConfig struct.

**Link to tracking Issue:** open-telemetry#35457
  • Loading branch information
mackjmr authored and jmichalek132 committed Oct 10, 2024
1 parent 05f482c commit def91cd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
26 changes: 14 additions & 12 deletions receiver/flinkmetricsreceiver/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
13 changes: 7 additions & 6 deletions receiver/flinkmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,19 +33,15 @@ 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 <scheme>://<hostname>:<port>: %w", errors.New(`parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`)),
},
{
desc: "valid config",
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
},
ClientConfig: clientConfig,
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
},
expectedErr: nil,
Expand Down
10 changes: 5 additions & 5 deletions receiver/flinkmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions receiver/flinkmetricsreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(),
}

Expand Down
26 changes: 14 additions & 12 deletions receiver/flinkmetricsreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
},
Expand All @@ -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(),
},
Expand Down

0 comments on commit def91cd

Please sign in to comment.