From 0a7c6d16acfd1262932d92780111eef2849b4d85 Mon Sep 17 00:00:00 2001 From: Roger Coll Date: Wed, 9 Oct 2024 02:48:56 +0200 Subject: [PATCH] [chore] [receiver/elasticsearch] Use NewDefaultClientConfig instead of manual initialization (#35509) **Description:** Similar to https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/35452 but for the `elasticsearch` receiver. **Link to tracking Issue:** https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/35457 **Testing:** **Documentation:** --- receiver/elasticsearchreceiver/config_test.go | 16 ++++++++++++++-- receiver/elasticsearchreceiver/factory.go | 11 ++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/receiver/elasticsearchreceiver/config_test.go b/receiver/elasticsearchreceiver/config_test.go index 885e540ddb1d..149f9bed0b1f 100644 --- a/receiver/elasticsearchreceiver/config_test.go +++ b/receiver/elasticsearchreceiver/config_test.go @@ -4,6 +4,7 @@ package elasticsearchreceiver import ( + "net/http" "path/filepath" "testing" "time" @@ -14,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/confighttp" + "go.opentelemetry.io/collector/config/configopaque" "go.opentelemetry.io/collector/confmap/confmaptest" "go.opentelemetry.io/collector/receiver/scraperhelper" @@ -138,6 +140,11 @@ func TestValidateEndpoint(t *testing.T) { } func TestLoadConfig(t *testing.T) { + defaultMaxIdleConns := http.DefaultTransport.(*http.Transport).MaxIdleConns + defaultMaxIdleConnsPerHost := http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost + defaultMaxConnsPerHost := http.DefaultTransport.(*http.Transport).MaxConnsPerHost + defaultIdleConnTimeout := http.DefaultTransport.(*http.Transport).IdleConnTimeout + t.Parallel() cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) @@ -167,8 +174,13 @@ func TestLoadConfig(t *testing.T) { Username: "otel", Password: "password", ClientConfig: confighttp.ClientConfig{ - Timeout: 10000000000, - Endpoint: "http://example.com:9200", + Timeout: 10000000000, + Endpoint: "http://example.com:9200", + Headers: map[string]configopaque.String{}, + MaxIdleConns: &defaultMaxIdleConns, + MaxIdleConnsPerHost: &defaultMaxIdleConnsPerHost, + MaxConnsPerHost: &defaultMaxConnsPerHost, + IdleConnTimeout: &defaultIdleConnTimeout, }, }, }, diff --git a/receiver/elasticsearchreceiver/factory.go b/receiver/elasticsearchreceiver/factory.go index 59671343bbdd..d0f831dbea85 100644 --- a/receiver/elasticsearchreceiver/factory.go +++ b/receiver/elasticsearchreceiver/factory.go @@ -35,12 +35,13 @@ func createDefaultConfig() component.Config { cfg := scraperhelper.NewDefaultControllerConfig() cfg.CollectionInterval = defaultCollectionInterval + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = defaultEndpoint + clientConfig.Timeout = defaultHTTPClientTimeout + return &Config{ - ControllerConfig: cfg, - ClientConfig: confighttp.ClientConfig{ - Endpoint: defaultEndpoint, - Timeout: defaultHTTPClientTimeout, - }, + ControllerConfig: cfg, + ClientConfig: clientConfig, MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), Nodes: []string{"_all"}, Indices: []string{"_all"},