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 ## Optional custom HTTP headers
# http_headers = {"X-Special-Header" = "Special-Value"} # http_headers = {"X-Special-Header" = "Special-Value"}
## Specify timeout duration for slower prometheus clients (default is 3s) ## Specify timeout duration for slower prometheus clients (default is 5s)
# timeout = "3s" # timeout = "5s"
## deprecated in 1.26; use the timeout option ## deprecated in 1.26; use the timeout option
# response_timeout = "3s" # response_timeout = "5s"
## HTTP Proxy support ## HTTP Proxy support
# use_system_proxy = false # use_system_proxy = false
# http_proxy_url = "" # http_proxy_url = ""

View File

@ -6,9 +6,7 @@ import (
_ "embed" _ "embed"
"errors" "errors"
"fmt" "fmt"
"github.com/influxdata/telegraf/models"
"io" "io"
"k8s.io/client-go/tools/cache"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -17,6 +15,9 @@ import (
"sync" "sync"
"time" "time"
"github.com/influxdata/telegraf/models"
"k8s.io/client-go/tools/cache"
"k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
@ -74,7 +75,6 @@ type Prometheus struct {
HTTPHeaders map[string]string `toml:"http_headers"` HTTPHeaders map[string]string `toml:"http_headers"`
ResponseTimeout config.Duration `toml:"response_timeout" deprecated:"1.26.0;use 'timeout' instead"` ResponseTimeout config.Duration `toml:"response_timeout" deprecated:"1.26.0;use 'timeout' instead"`
Timeout config.Duration `toml:"timeout"`
MetricVersion int `toml:"metric_version"` MetricVersion int `toml:"metric_version"`
@ -194,7 +194,10 @@ func (p *Prometheus) Init() error {
} }
ctx := context.Background() 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) client, err := p.HTTPClientConfig.CreateClient(ctx, p.Log)
if err != nil { if err != nil {
return err return err
@ -331,7 +334,9 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) error
return c, err return c, err
}, },
}, },
Timeout: time.Duration(p.ResponseTimeout), }
if p.ResponseTimeout != 0 {
uClient.Timeout = time.Duration(p.ResponseTimeout)
} }
} else { } else {
if u.URL.Path == "" { if u.URL.Path == "" {
@ -487,10 +492,9 @@ func (p *Prometheus) Stop() {
func init() { func init() {
inputs.Add("prometheus", func() telegraf.Input { inputs.Add("prometheus", func() telegraf.Input {
return &Prometheus{ return &Prometheus{
ResponseTimeout: config.Duration(time.Second * 3), kubernetesPods: map[PodID]URLAndAddress{},
kubernetesPods: map[PodID]URLAndAddress{}, consulServices: map[string]URLAndAddress{},
consulServices: map[string]URLAndAddress{}, URLTag: "url",
URLTag: "url",
} }
}) })
} }

View File

@ -205,13 +205,13 @@ func TestPrometheusGeneratesMetricsSlowEndpointNewConfigParameter(t *testing.T)
defer ts.Close() defer ts.Close()
p := &Prometheus{ p := &Prometheus{
Log: testutil.Logger{}, Log: testutil.Logger{},
URLs: []string{ts.URL}, URLs: []string{ts.URL},
URLTag: "url", URLTag: "url",
Timeout: config.Duration(time.Second * 5),
} }
err := p.Init() err := p.Init()
require.NoError(t, err) require.NoError(t, err)
p.client.Timeout = time.Second * 5
var acc testutil.Accumulator var acc testutil.Accumulator
@ -235,13 +235,13 @@ func TestPrometheusGeneratesMetricsSlowEndpointHitTheTimeoutNewConfigParameter(t
defer ts.Close() defer ts.Close()
p := &Prometheus{ p := &Prometheus{
Log: testutil.Logger{}, Log: testutil.Logger{},
URLs: []string{ts.URL}, URLs: []string{ts.URL},
URLTag: "url", URLTag: "url",
Timeout: config.Duration(time.Second * 5),
} }
err := p.Init() err := p.Init()
require.NoError(t, err) require.NoError(t, err)
p.client.Timeout = time.Second * 5
var acc testutil.Accumulator var acc testutil.Accumulator

View File

@ -108,12 +108,12 @@
## Optional custom HTTP headers ## Optional custom HTTP headers
# http_headers = {"X-Special-Header" = "Special-Value"} # http_headers = {"X-Special-Header" = "Special-Value"}
## Specify timeout duration for slower prometheus clients (default is 3s) ## Specify timeout duration for slower prometheus clients (default is 5s)
# timeout = "3s" # timeout = "5s"
## deprecated in 1.26; use the timeout option ## deprecated in 1.26; use the timeout option
# response_timeout = "3s" # response_timeout = "5s"
## HTTP Proxy support ## HTTP Proxy support
# use_system_proxy = false # use_system_proxy = false
# http_proxy_url = "" # http_proxy_url = ""