fix(inputs.prometheus): Correctly set timeout param (#12864)

This commit is contained in:
Joshua Powers 2023-03-15 10:52:51 -06:00 committed by GitHub
parent 21f542d9c7
commit bd065e3553
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 29 deletions

View File

@ -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 = ""

View File

@ -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",
}
})
}

View File

@ -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

View File

@ -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 = ""