From 37bc9cf79514184f065519bbacf972d0b686aed7 Mon Sep 17 00:00:00 2001 From: Dominik Labuda Date: Wed, 21 Dec 2016 13:41:58 +0100 Subject: [PATCH] [plugins] jolokia input plugin: configurable http timeouts (#2098) --- CHANGELOG.md | 1 + plugins/inputs/jolokia/README.md | 11 +++++++++- plugins/inputs/jolokia/jolokia.go | 34 ++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd0f848a2..dd044a941 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ plugins, not just statsd. - [#1921](https://github.com/influxdata/telegraf/issues/1921): Elasticsearch cluster stats support. - [#1942](https://github.com/influxdata/telegraf/pull/1942): Change Amazon Kinesis output plugin to use the built-in serializer plugins. - [#1980](https://github.com/influxdata/telegraf/issues/1980): Hide username/password from elasticsearch error log messages. +- [#2097](https://github.com/influxdata/telegraf/issues/2097): Configurable HTTP timeouts in Jolokia plugin ### Bugfixes diff --git a/plugins/inputs/jolokia/README.md b/plugins/inputs/jolokia/README.md index d25ab6f46..9d33c8a2b 100644 --- a/plugins/inputs/jolokia/README.md +++ b/plugins/inputs/jolokia/README.md @@ -18,7 +18,16 @@ # [inputs.jolokia.proxy] # host = "127.0.0.1" # port = "8080" - + + ## Optional http timeouts + ## + ## response_header_timeout, if non-zero, specifies the amount of time to wait + ## for a server's response headers after fully writing the request. + # response_header_timeout = "3s" + ## + ## client_timeout specifies a time limit for requests made by this client. + ## Includes connection time, any redirects, and reading the response body. + # client_timeout = "4s" ## List of servers exposing jolokia read service [[inputs.jolokia.servers]] diff --git a/plugins/inputs/jolokia/jolokia.go b/plugins/inputs/jolokia/jolokia.go index 812e5e66b..32e6a9f57 100644 --- a/plugins/inputs/jolokia/jolokia.go +++ b/plugins/inputs/jolokia/jolokia.go @@ -11,9 +11,14 @@ import ( "time" "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/plugins/inputs" ) +// Default http timeouts +var DefaultResponseHeaderTimeout = internal.Duration{Duration: 3 * time.Second} +var DefaultClientTimeout = internal.Duration{Duration: 4 * time.Second} + type Server struct { Name string Host string @@ -48,6 +53,9 @@ type Jolokia struct { Servers []Server Metrics []Metric Proxy Server + + ResponseHeaderTimeout internal.Duration `toml:"response_header_timeout"` + ClientTimeout internal.Duration `toml:"client_timeout"` } const sampleConfig = ` @@ -66,6 +74,15 @@ const sampleConfig = ` # host = "127.0.0.1" # port = "8080" + ## Optional http timeouts + ## + ## response_header_timeout, if non-zero, specifies the amount of time to wait + ## for a server's response headers after fully writing the request. + # response_header_timeout = "3s" + ## + ## client_timeout specifies a time limit for requests made by this client. + ## Includes connection time, any redirects, and reading the response body. + # client_timeout = "4s" ## List of servers exposing jolokia read service [[inputs.jolokia.servers]] @@ -232,6 +249,15 @@ func extractValues(measurement string, value interface{}, fields map[string]inte } func (j *Jolokia) Gather(acc telegraf.Accumulator) error { + + if j.jClient == nil { + tr := &http.Transport{ResponseHeaderTimeout: j.ResponseHeaderTimeout.Duration} + j.jClient = &JolokiaClientImpl{&http.Client{ + Transport: tr, + Timeout: j.ClientTimeout.Duration, + }} + } + servers := j.Servers metrics := j.Metrics tags := make(map[string]string) @@ -272,11 +298,9 @@ func (j *Jolokia) Gather(acc telegraf.Accumulator) error { func init() { inputs.Add("jolokia", func() telegraf.Input { - tr := &http.Transport{ResponseHeaderTimeout: time.Duration(3 * time.Second)} - client := &http.Client{ - Transport: tr, - Timeout: time.Duration(4 * time.Second), + return &Jolokia{ + ResponseHeaderTimeout: DefaultResponseHeaderTimeout, + ClientTimeout: DefaultClientTimeout, } - return &Jolokia{jClient: &JolokiaClientImpl{client: client}} }) }