From a782f0a4f02d6b662ea8a48a1d46a7cf6491099a Mon Sep 17 00:00:00 2001 From: mackjmr Date: Tue, 1 Oct 2024 14:42:41 +0200 Subject: [PATCH 1/3] [exporter/influxdb] Use 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 --- exporter/influxdbexporter/config_test.go | 12 +++++++++--- exporter/influxdbexporter/factory.go | 13 +++++++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/exporter/influxdbexporter/config_test.go b/exporter/influxdbexporter/config_test.go index 7bcb99dff6f6..6df0e310b0e8 100644 --- a/exporter/influxdbexporter/config_test.go +++ b/exporter/influxdbexporter/config_test.go @@ -4,6 +4,7 @@ package influxdbexporter import ( + "net/http" "path/filepath" "testing" "time" @@ -22,6 +23,7 @@ import ( ) func TestLoadConfig(t *testing.T) { + defaultTransport := http.DefaultTransport.(*http.Transport) t.Parallel() cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) @@ -39,9 +41,13 @@ func TestLoadConfig(t *testing.T) { id: component.NewIDWithName(metadata.Type, "override-config"), expected: &Config{ ClientConfig: confighttp.ClientConfig{ - Endpoint: "http://localhost:8080", - Timeout: 500 * time.Millisecond, - Headers: map[string]configopaque.String{"User-Agent": "OpenTelemetry -> Influx"}, + Endpoint: "http://localhost:8080", + Timeout: 500 * time.Millisecond, + Headers: map[string]configopaque.String{"User-Agent": "OpenTelemetry -> Influx"}, + MaxIdleConns: &defaultTransport.MaxIdleConns, + MaxIdleConnsPerHost: &defaultTransport.MaxIdleConnsPerHost, + MaxConnsPerHost: &defaultTransport.MaxConnsPerHost, + IdleConnTimeout: &defaultTransport.IdleConnTimeout, }, QueueSettings: exporterhelper.QueueConfig{ Enabled: true, diff --git a/exporter/influxdbexporter/factory.go b/exporter/influxdbexporter/factory.go index 0c821d751b3c..38f27097895c 100644 --- a/exporter/influxdbexporter/factory.go +++ b/exporter/influxdbexporter/factory.go @@ -34,13 +34,14 @@ func NewFactory() exporter.Factory { } func createDefaultConfig() component.Config { + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Timeout = 5 * time.Second + clientConfig.Headers = map[string]configopaque.String{ + "User-Agent": "OpenTelemetry -> Influx", + } + return &Config{ - ClientConfig: confighttp.ClientConfig{ - Timeout: 5 * time.Second, - Headers: map[string]configopaque.String{ - "User-Agent": "OpenTelemetry -> Influx", - }, - }, + ClientConfig: clientConfig, QueueSettings: exporterhelper.NewDefaultQueueConfig(), BackOffConfig: configretry.NewDefaultBackOffConfig(), MetricsSchema: common.MetricsSchemaTelegrafPrometheusV1.String(), From a7ae565ed16e68e7e93e830f0cbaf4afb6cecfaf Mon Sep 17 00:00:00 2001 From: mackjmr Date: Wed, 2 Oct 2024 13:37:11 +0200 Subject: [PATCH 2/3] improve test --- exporter/influxdbexporter/config_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/exporter/influxdbexporter/config_test.go b/exporter/influxdbexporter/config_test.go index 6df0e310b0e8..60d85c890efa 100644 --- a/exporter/influxdbexporter/config_test.go +++ b/exporter/influxdbexporter/config_test.go @@ -23,7 +23,10 @@ import ( ) func TestLoadConfig(t *testing.T) { - defaultTransport := http.DefaultTransport.(*http.Transport) + 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")) @@ -44,10 +47,10 @@ func TestLoadConfig(t *testing.T) { Endpoint: "http://localhost:8080", Timeout: 500 * time.Millisecond, Headers: map[string]configopaque.String{"User-Agent": "OpenTelemetry -> Influx"}, - MaxIdleConns: &defaultTransport.MaxIdleConns, - MaxIdleConnsPerHost: &defaultTransport.MaxIdleConnsPerHost, - MaxConnsPerHost: &defaultTransport.MaxConnsPerHost, - IdleConnTimeout: &defaultTransport.IdleConnTimeout, + MaxIdleConns: &defaultMaxIdleConns, + MaxIdleConnsPerHost: &defaultMaxIdleConnsPerHost, + MaxConnsPerHost: &defaultMaxConnsPerHost, + IdleConnTimeout: &defaultIdleConnTimeout, }, QueueSettings: exporterhelper.QueueConfig{ Enabled: true, From 17238920c84c000ea6ba20834d6d07b5cf4bbec6 Mon Sep 17 00:00:00 2001 From: mackjmr Date: Fri, 4 Oct 2024 13:50:15 +0200 Subject: [PATCH 3/3] fix test --- exporter/influxdbexporter/config_test.go | 19 +++++-------------- exporter/influxdbexporter/writer_test.go | 6 +++--- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/exporter/influxdbexporter/config_test.go b/exporter/influxdbexporter/config_test.go index 60d85c890efa..6459d4cad16a 100644 --- a/exporter/influxdbexporter/config_test.go +++ b/exporter/influxdbexporter/config_test.go @@ -4,7 +4,6 @@ package influxdbexporter import ( - "net/http" "path/filepath" "testing" "time" @@ -23,10 +22,10 @@ import ( ) 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 + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = "http://localhost:8080" + clientConfig.Timeout = 500 * time.Millisecond + clientConfig.Headers = map[string]configopaque.String{"User-Agent": "OpenTelemetry -> Influx"} t.Parallel() cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) @@ -43,15 +42,7 @@ func TestLoadConfig(t *testing.T) { { id: component.NewIDWithName(metadata.Type, "override-config"), expected: &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: "http://localhost:8080", - Timeout: 500 * time.Millisecond, - Headers: map[string]configopaque.String{"User-Agent": "OpenTelemetry -> Influx"}, - MaxIdleConns: &defaultMaxIdleConns, - MaxIdleConnsPerHost: &defaultMaxIdleConnsPerHost, - MaxConnsPerHost: &defaultMaxConnsPerHost, - IdleConnTimeout: &defaultIdleConnTimeout, - }, + ClientConfig: clientConfig, QueueSettings: exporterhelper.QueueConfig{ Enabled: true, NumConsumers: 3, diff --git a/exporter/influxdbexporter/writer_test.go b/exporter/influxdbexporter/writer_test.go index 36fdffa3bbd4..8970bf6b29dc 100644 --- a/exporter/influxdbexporter/writer_test.go +++ b/exporter/influxdbexporter/writer_test.go @@ -158,13 +158,13 @@ func Test_influxHTTPWriterBatch_EnqueuePoint_emptyTagValue(t *testing.T) { t.Cleanup(noopHTTPServer.Close) nowTime := time.Unix(1000, 2000) + clientConfig := confighttp.NewDefaultClientConfig() + clientConfig.Endpoint = noopHTTPServer.URL influxWriter, err := newInfluxHTTPWriter( new(common.NoopLogger), &Config{ - ClientConfig: confighttp.ClientConfig{ - Endpoint: noopHTTPServer.URL, - }, + ClientConfig: clientConfig, }, componenttest.NewNopTelemetrySettings()) require.NoError(t, err)