From bd065e35535a0f971ae055aeb74cb1f82d6ad3c2 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Wed, 15 Mar 2023 10:52:51 -0600 Subject: [PATCH] fix(inputs.prometheus): Correctly set timeout param (#12864) --- plugins/inputs/prometheus/README.md | 12 +++++------ plugins/inputs/prometheus/prometheus.go | 22 ++++++++++++-------- plugins/inputs/prometheus/prometheus_test.go | 16 +++++++------- plugins/inputs/prometheus/sample.conf | 12 +++++------ 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/plugins/inputs/prometheus/README.md b/plugins/inputs/prometheus/README.md index 1d3b7bf12..e049652a1 100644 --- a/plugins/inputs/prometheus/README.md +++ b/plugins/inputs/prometheus/README.md @@ -125,12 +125,12 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ## Optional custom HTTP headers # http_headers = {"X-Special-Header" = "Special-Value"} - ## Specify timeout duration for slower prometheus clients (default is 3s) - # timeout = "3s" - - ## deprecated in 1.26; use the timeout option - # response_timeout = "3s" - + ## Specify timeout duration for slower prometheus clients (default is 5s) + # timeout = "5s" + + ## deprecated in 1.26; use the timeout option + # response_timeout = "5s" + ## HTTP Proxy support # use_system_proxy = false # http_proxy_url = "" diff --git a/plugins/inputs/prometheus/prometheus.go b/plugins/inputs/prometheus/prometheus.go index 7d0e9acba..03c0b3811 100644 --- a/plugins/inputs/prometheus/prometheus.go +++ b/plugins/inputs/prometheus/prometheus.go @@ -6,9 +6,7 @@ import ( _ "embed" "errors" "fmt" - "github.com/influxdata/telegraf/models" "io" - "k8s.io/client-go/tools/cache" "net" "net/http" "net/url" @@ -17,6 +15,9 @@ import ( "sync" "time" + "github.com/influxdata/telegraf/models" + "k8s.io/client-go/tools/cache" + "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/labels" @@ -74,7 +75,6 @@ type Prometheus struct { HTTPHeaders map[string]string `toml:"http_headers"` ResponseTimeout config.Duration `toml:"response_timeout" deprecated:"1.26.0;use 'timeout' instead"` - Timeout config.Duration `toml:"timeout"` MetricVersion int `toml:"metric_version"` @@ -194,7 +194,10 @@ func (p *Prometheus) Init() error { } ctx := context.Background() - p.HTTPClientConfig.Timeout = p.ResponseTimeout + if p.ResponseTimeout != 0 { + p.HTTPClientConfig.Timeout = p.ResponseTimeout + } + client, err := p.HTTPClientConfig.CreateClient(ctx, p.Log) if err != nil { return err @@ -331,7 +334,9 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error return c, err }, }, - Timeout: time.Duration(p.ResponseTimeout), + } + if p.ResponseTimeout != 0 { + uClient.Timeout = time.Duration(p.ResponseTimeout) } } else { if u.URL.Path == "" { @@ -487,10 +492,9 @@ func (p *Prometheus) Stop() { func init() { inputs.Add("prometheus", func() telegraf.Input { return &Prometheus{ - ResponseTimeout: config.Duration(time.Second * 3), - kubernetesPods: map[PodID]URLAndAddress{}, - consulServices: map[string]URLAndAddress{}, - URLTag: "url", + kubernetesPods: map[PodID]URLAndAddress{}, + consulServices: map[string]URLAndAddress{}, + URLTag: "url", } }) } diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index ffef88faf..005256056 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -205,13 +205,13 @@ func TestPrometheusGeneratesMetricsSlowEndpointNewConfigParameter(t *testing.T) defer ts.Close() p := &Prometheus{ - Log: testutil.Logger{}, - URLs: []string{ts.URL}, - URLTag: "url", - Timeout: config.Duration(time.Second * 5), + Log: testutil.Logger{}, + URLs: []string{ts.URL}, + URLTag: "url", } err := p.Init() require.NoError(t, err) + p.client.Timeout = time.Second * 5 var acc testutil.Accumulator @@ -235,13 +235,13 @@ func TestPrometheusGeneratesMetricsSlowEndpointHitTheTimeoutNewConfigParameter(t defer ts.Close() p := &Prometheus{ - Log: testutil.Logger{}, - URLs: []string{ts.URL}, - URLTag: "url", - Timeout: config.Duration(time.Second * 5), + Log: testutil.Logger{}, + URLs: []string{ts.URL}, + URLTag: "url", } err := p.Init() require.NoError(t, err) + p.client.Timeout = time.Second * 5 var acc testutil.Accumulator diff --git a/plugins/inputs/prometheus/sample.conf b/plugins/inputs/prometheus/sample.conf index 538acf517..33c9a90a5 100644 --- a/plugins/inputs/prometheus/sample.conf +++ b/plugins/inputs/prometheus/sample.conf @@ -108,12 +108,12 @@ ## Optional custom HTTP headers # http_headers = {"X-Special-Header" = "Special-Value"} - ## Specify timeout duration for slower prometheus clients (default is 3s) - # timeout = "3s" - - ## deprecated in 1.26; use the timeout option - # response_timeout = "3s" - + ## Specify timeout duration for slower prometheus clients (default is 5s) + # timeout = "5s" + + ## deprecated in 1.26; use the timeout option + # response_timeout = "5s" + ## HTTP Proxy support # use_system_proxy = false # http_proxy_url = ""