Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type HTTPClientConfig struct {
ProxyURL URL `yaml:"proxy_url,omitempty"`
// TLSConfig to use to connect to the targets.
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
// If set, override whether to use HTTP KeepAlive - defaults ON
KeepAlive *bool `yaml:"keep_alive,omitempty"`
}

// Validate validates the HTTPClientConfig to check only one of BearerToken,
Expand Down Expand Up @@ -128,13 +130,18 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string) (http.RoundTri
if err != nil {
return nil, err
}
// If not specified in config, default HTTP connections to allow keep-alive
disableKeepAlives := false
if cfg.KeepAlive != nil {
disableKeepAlives = !*cfg.KeepAlive
}
// The only timeout we care about is the configured scrape timeout.
// It is applied on request. So we leave out any timings here.
var rt http.RoundTripper = &http.Transport{
Proxy: http.ProxyURL(cfg.ProxyURL.URL),
MaxIdleConns: 20000,
MaxIdleConnsPerHost: 1000, // see https://github.com/golang/go/issues/13801
DisableKeepAlives: false,
DisableKeepAlives: disableKeepAlives,
TLSClientConfig: tlsConfig,
DisableCompression: true,
// 5 minutes is typically above the maximum sane scrape interval. So we can
Expand Down
14 changes: 14 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httpt
}

func TestNewClientFromConfig(t *testing.T) {
fVal := false
var newClientValidConfig = []struct {
clientConfig HTTPClientConfig
handler func(w http.ResponseWriter, r *http.Request)
Expand Down Expand Up @@ -121,6 +122,19 @@ func TestNewClientFromConfig(t *testing.T) {
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, ExpectedMessage)
},
}, {
clientConfig: HTTPClientConfig{
TLSConfig: TLSConfig{
CAFile: TLSCAChainPath,
CertFile: BarneyCertificatePath,
KeyFile: BarneyKeyNoPassPath,
ServerName: "",
InsecureSkipVerify: false},
KeepAlive: &fVal,
},
handler: func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, ExpectedMessage)
},
}, {
clientConfig: HTTPClientConfig{
BearerToken: BearerToken,
Expand Down
1 change: 1 addition & 0 deletions config/testdata/http.conf.good.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ basic_auth:
username: username
password: "mysecret"
proxy_url: "http://remote.host"
keep_alive: false