diff --git a/plugins/inputs/prometheus/README.md b/plugins/inputs/prometheus/README.md index ef7eb8690..6de4c52e2 100644 --- a/plugins/inputs/prometheus/README.md +++ b/plugins/inputs/prometheus/README.md @@ -141,6 +141,8 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. # timeout = "5s" ## deprecated in 1.26; use the timeout option + ## This option is now used by the HTTP client to set the header response + ## timeout, not the overall HTTP timeout. # response_timeout = "5s" ## HTTP Proxy support diff --git a/plugins/inputs/prometheus/prometheus.go b/plugins/inputs/prometheus/prometheus.go index 10daa5879..82805ae86 100644 --- a/plugins/inputs/prometheus/prometheus.go +++ b/plugins/inputs/prometheus/prometheus.go @@ -74,9 +74,8 @@ type Prometheus struct { HTTPHeaders map[string]string `toml:"http_headers"` - ResponseTimeout config.Duration `toml:"response_timeout" deprecated:"1.26.0;use 'timeout' instead"` - ContentLengthLimit config.Size `toml:"content_length_limit"` - EnableRequestMetrics bool `toml:"enable_request_metrics"` + ContentLengthLimit config.Size `toml:"content_length_limit"` + EnableRequestMetrics bool `toml:"enable_request_metrics"` MetricVersion int `toml:"metric_version"` @@ -213,15 +212,21 @@ func (p *Prometheus) Init() error { } ctx := context.Background() - if p.ResponseTimeout != 0 { - p.HTTPClientConfig.Timeout = p.ResponseTimeout - } client, err := p.HTTPClientConfig.CreateClient(ctx, p.Log) if err != nil { return err } p.client = client + if p.HTTPClientConfig.ResponseHeaderTimeout != 0 { + p.Log.Warn( + "Config option response_timeout was set to non-zero value. This option's behavior was " + + "changed in Telegraf 1.30.2 and now controls the HTTP client's header timeout and " + + "not the Prometheus timeout. Users can ignore this warning if that was the intention. " + + "Otherwise, please use the timeout config option for the Prometheus timeout.", + ) + } + p.headers = map[string]string{ "User-Agent": internal.ProductToken(), "Accept": acceptHeader, @@ -406,9 +411,6 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s }, }, } - if p.ResponseTimeout != 0 { - uClient.Timeout = time.Duration(p.ResponseTimeout) - } } else { if u.URL.Path == "" { u.URL.Path = "/metrics" diff --git a/plugins/inputs/prometheus/prometheus_test.go b/plugins/inputs/prometheus/prometheus_test.go index b63f05c29..d56a1cd5f 100644 --- a/plugins/inputs/prometheus/prometheus_test.go +++ b/plugins/inputs/prometheus/prometheus_test.go @@ -14,7 +14,6 @@ import ( "k8s.io/apimachinery/pkg/fields" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/testutil" ) @@ -241,10 +240,12 @@ func TestPrometheusGeneratesMetricsSlowEndpoint(t *testing.T) { defer ts.Close() p := &Prometheus{ - Log: testutil.Logger{}, - URLs: []string{ts.URL}, - URLTag: "url", - ResponseTimeout: config.Duration(time.Second * 5), + Log: testutil.Logger{}, + URLs: []string{ts.URL}, + URLTag: "url", + client: &http.Client{ + Timeout: time.Second * 5, + }, } err := p.Init() require.NoError(t, err) @@ -271,10 +272,12 @@ func TestPrometheusGeneratesMetricsSlowEndpointHitTheTimeout(t *testing.T) { defer ts.Close() p := &Prometheus{ - Log: testutil.Logger{}, - URLs: []string{ts.URL}, - URLTag: "url", - ResponseTimeout: config.Duration(time.Second * 5), + Log: testutil.Logger{}, + URLs: []string{ts.URL}, + URLTag: "url", + client: &http.Client{ + Timeout: time.Second * 5, + }, } err := p.Init() require.NoError(t, err) diff --git a/plugins/inputs/prometheus/sample.conf b/plugins/inputs/prometheus/sample.conf index 3548dee95..f2879eac9 100644 --- a/plugins/inputs/prometheus/sample.conf +++ b/plugins/inputs/prometheus/sample.conf @@ -124,6 +124,8 @@ # timeout = "5s" ## deprecated in 1.26; use the timeout option + ## This option is now used by the HTTP client to set the header response + ## timeout, not the overall HTTP timeout. # response_timeout = "5s" ## HTTP Proxy support