feat(inputs.kubernetes): Apply timeout for the whole HTTP request (#12918)
This commit is contained in:
parent
3c8277d502
commit
771153e76d
|
|
@ -47,7 +47,7 @@ type Kubernetes struct {
|
||||||
|
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
||||||
RoundTripper http.RoundTripper
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -244,16 +244,22 @@ func (k *Kubernetes) LoadJSON(url string, v interface{}) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if k.RoundTripper == nil {
|
|
||||||
|
if k.httpClient == nil {
|
||||||
if k.ResponseTimeout < config.Duration(time.Second) {
|
if k.ResponseTimeout < config.Duration(time.Second) {
|
||||||
k.ResponseTimeout = config.Duration(time.Second * 5)
|
k.ResponseTimeout = config.Duration(time.Second * 5)
|
||||||
}
|
}
|
||||||
k.RoundTripper = &http.Transport{
|
k.httpClient = &http.Client{
|
||||||
TLSHandshakeTimeout: 5 * time.Second,
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: tlsCfg,
|
TLSClientConfig: tlsCfg,
|
||||||
ResponseHeaderTimeout: time.Duration(k.ResponseTimeout),
|
},
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
|
Timeout: time.Duration(k.ResponseTimeout),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if k.BearerToken != "" {
|
if k.BearerToken != "" {
|
||||||
token, err := os.ReadFile(k.BearerToken)
|
token, err := os.ReadFile(k.BearerToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -263,7 +269,7 @@ func (k *Kubernetes) LoadJSON(url string, v interface{}) error {
|
||||||
}
|
}
|
||||||
req.Header.Set("Authorization", "Bearer "+k.BearerTokenString)
|
req.Header.Set("Authorization", "Bearer "+k.BearerTokenString)
|
||||||
req.Header.Add("Accept", "application/json")
|
req.Header.Add("Accept", "application/json")
|
||||||
resp, err = k.RoundTripper.RoundTrip(req)
|
resp, err = k.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error making HTTP request to %q: %w", url, err)
|
return fmt.Errorf("error making HTTP request to %q: %w", url, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue