Skip to content

Commit

Permalink
fix(inputs.prometheus): Remove duplicate response_timeout option (#15078
Browse files Browse the repository at this point in the history
)

(cherry picked from commit 47e28d0)
  • Loading branch information
powersj committed Apr 22, 2024
1 parent 86a00f5 commit da6999d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions plugins/inputs/prometheus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions plugins/inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand Down
21 changes: 12 additions & 9 deletions plugins/inputs/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions plugins/inputs/prometheus/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit da6999d

Please sign in to comment.