From a731f1d5c133c2693ec2ee915907d77b24854842 Mon Sep 17 00:00:00 2001 From: Philippe Laflamme Date: Sat, 24 Mar 2018 16:33:31 -0400 Subject: [PATCH] Support http client timeout This adds support for configuring the `http.Client` timeout parameter, allowing the specification of a global request timeout. --- config/http_config.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/config/http_config.go b/config/http_config.go index ea231bf8d..0f13bdc0c 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -21,7 +21,9 @@ import ( "net/http" "net/url" "strings" + "time" + "github.com/prometheus/common/model" "gopkg.in/yaml.v2" ) @@ -74,6 +76,8 @@ type HTTPClientConfig struct { ProxyURL URL `yaml:"proxy_url,omitempty"` // TLSConfig to use to connect to the targets. TLSConfig TLSConfig `yaml:"tls_config,omitempty"` + // Timeout to apply to every request. + Timeout model.Duration `yaml:"timeout,omitempty"` // Catches all undefined fields and must be empty after parsing. XXX map[string]interface{} `yaml:",inline"` @@ -149,8 +153,8 @@ func NewHTTPClientFromConfig(cfg *HTTPClientConfig) (*http.Client, error) { rt = NewBasicAuthRoundTripper(cfg.BasicAuth.Username, Secret(cfg.BasicAuth.Password), rt) } - // Return a new client with the configured round tripper. - return &http.Client{Transport: rt}, nil + // Return a new client with the configured timeout and round tripper. + return &http.Client{Timeout: time.Duration(cfg.Timeout), Transport: rt}, nil } type bearerAuthRoundTripper struct {