From 26e2c77aaf9b84281e76ac4c65d456239b62eec8 Mon Sep 17 00:00:00 2001 From: Simone Rotondo Date: Fri, 16 Jun 2017 01:39:17 +0200 Subject: [PATCH 1/2] HTTP Proxy in influxdb output plugin --- plugins/outputs/influxdb/README.md | 4 ++++ plugins/outputs/influxdb/client/http.go | 24 +++++++++++++++++++++--- plugins/outputs/influxdb/influxdb.go | 5 +++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/plugins/outputs/influxdb/README.md b/plugins/outputs/influxdb/README.md index 3cd029d8c955b..7d3cadc40cbb0 100644 --- a/plugins/outputs/influxdb/README.md +++ b/plugins/outputs/influxdb/README.md @@ -40,6 +40,9 @@ This plugin writes to [InfluxDB](https://www.influxdb.com) via HTTP or UDP. # ssl_key = "/etc/telegraf/key.pem" ## Use SSL but skip chain & host verification # insecure_skip_verify = false + + ## HTTP Proxy Config + # proxy = "http://corporate.proxy:3128" ``` ### Required parameters: @@ -63,3 +66,4 @@ to write to. Each URL should start with either `http://` or `udp://` * `ssl_cert`: SSL CERT * `ssl_key`: SSL key * `insecure_skip_verify`: Use SSL but skip chain & host verification (default: false) +* `proxy`: HTTP Proxy URI diff --git a/plugins/outputs/influxdb/client/http.go b/plugins/outputs/influxdb/client/http.go index 4acea23627ae6..18500467091ba 100644 --- a/plugins/outputs/influxdb/client/http.go +++ b/plugins/outputs/influxdb/client/http.go @@ -39,15 +39,30 @@ func NewHTTP(config HTTPConfig, defaultWP WriteParams) (Client, error) { return nil, fmt.Errorf("config.URL scheme must be http(s), got %s", u.Scheme) } + var transport http.Transport + if len(config.Proxy) > 0 { + proxyURL, err := url.Parse(config.Proxy) + if err != nil { + return nil, fmt.Errorf("error parsing config.Proxy: %s", err) + } + + transport = http.Transport{ + Proxy: http.ProxyURL(proxyURL), + TLSClientConfig: config.TLSConfig, + } + } else { + transport = http.Transport{ + TLSClientConfig: config.TLSConfig, + } + } + return &httpClient{ writeURL: writeURL(u, defaultWP), config: config, url: u, client: &http.Client{ Timeout: config.Timeout, - Transport: &http.Transport{ - TLSClientConfig: config.TLSConfig, - }, + Transport: &transport, }, }, nil } @@ -76,6 +91,9 @@ type HTTPConfig struct { // TLSConfig is the tls auth settings to use for each request. TLSConfig *tls.Config + // Proxy URL should be of the form "http://host:port" + Proxy string + // Gzip, if true, compresses each payload using gzip. // TODO // Gzip bool diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index fe1adc4bbce81..29bced4d0ada4 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -33,6 +33,7 @@ type InfluxDB struct { WriteConsistency string Timeout internal.Duration UDPPayload int `toml:"udp_payload"` + Proxy string // Path to CA file SSLCA string `toml:"ssl_ca"` @@ -83,6 +84,9 @@ var sampleConfig = ` # ssl_key = "/etc/telegraf/key.pem" ## Use SSL but skip chain & host verification # insecure_skip_verify = false + + ## HTTP Proxy Config + # proxy = "http://corporate.proxy:3128" ` // Connect initiates the primary connection to the range of provided URLs @@ -123,6 +127,7 @@ func (i *InfluxDB) Connect() error { UserAgent: i.UserAgent, Username: i.Username, Password: i.Password, + Proxy: i.Proxy, } wp := client.WriteParams{ Database: i.Database, From e4eea127d55c0c68c92a298165f913d57140d0ba Mon Sep 17 00:00:00 2001 From: Simone Rotondo Date: Fri, 16 Jun 2017 14:28:07 +0200 Subject: [PATCH 2/2] HTTP Proxy in influxdb output plugin --- plugins/outputs/influxdb/README.md | 4 +-- plugins/outputs/influxdb/client/http.go | 34 ++++++++++++------------- plugins/outputs/influxdb/influxdb.go | 8 +++--- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/plugins/outputs/influxdb/README.md b/plugins/outputs/influxdb/README.md index 7d3cadc40cbb0..49fdc731c4bc6 100644 --- a/plugins/outputs/influxdb/README.md +++ b/plugins/outputs/influxdb/README.md @@ -42,7 +42,7 @@ This plugin writes to [InfluxDB](https://www.influxdb.com) via HTTP or UDP. # insecure_skip_verify = false ## HTTP Proxy Config - # proxy = "http://corporate.proxy:3128" + # http_proxy = "http://corporate.proxy:3128" ``` ### Required parameters: @@ -66,4 +66,4 @@ to write to. Each URL should start with either `http://` or `udp://` * `ssl_cert`: SSL CERT * `ssl_key`: SSL key * `insecure_skip_verify`: Use SSL but skip chain & host verification (default: false) -* `proxy`: HTTP Proxy URI +* `http_proxy`: HTTP Proxy URI diff --git a/plugins/outputs/influxdb/client/http.go b/plugins/outputs/influxdb/client/http.go index 18500467091ba..6a757982d52c8 100644 --- a/plugins/outputs/influxdb/client/http.go +++ b/plugins/outputs/influxdb/client/http.go @@ -39,29 +39,29 @@ func NewHTTP(config HTTPConfig, defaultWP WriteParams) (Client, error) { return nil, fmt.Errorf("config.URL scheme must be http(s), got %s", u.Scheme) } - var transport http.Transport - if len(config.Proxy) > 0 { - proxyURL, err := url.Parse(config.Proxy) - if err != nil { - return nil, fmt.Errorf("error parsing config.Proxy: %s", err) - } - - transport = http.Transport{ - Proxy: http.ProxyURL(proxyURL), - TLSClientConfig: config.TLSConfig, - } + var transport http.Transport + if len(config.HTTPProxy) > 0 { + proxyURL, err := url.Parse(config.HTTPProxy) + if err != nil { + return nil, fmt.Errorf("error parsing config.HTTPProxy: %s", err) + } + + transport = http.Transport{ + Proxy: http.ProxyURL(proxyURL), + TLSClientConfig: config.TLSConfig, + } } else { - transport = http.Transport{ - TLSClientConfig: config.TLSConfig, - } - } + transport = http.Transport{ + TLSClientConfig: config.TLSConfig, + } + } return &httpClient{ writeURL: writeURL(u, defaultWP), config: config, url: u, client: &http.Client{ - Timeout: config.Timeout, + Timeout: config.Timeout, Transport: &transport, }, }, nil @@ -92,7 +92,7 @@ type HTTPConfig struct { TLSConfig *tls.Config // Proxy URL should be of the form "http://host:port" - Proxy string + HTTPProxy string // Gzip, if true, compresses each payload using gzip. // TODO diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index 29bced4d0ada4..1028bc257496b 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -32,8 +32,8 @@ type InfluxDB struct { RetentionPolicy string WriteConsistency string Timeout internal.Duration - UDPPayload int `toml:"udp_payload"` - Proxy string + UDPPayload int `toml:"udp_payload"` + HTTPProxy string `toml:"http_proxy"` // Path to CA file SSLCA string `toml:"ssl_ca"` @@ -86,7 +86,7 @@ var sampleConfig = ` # insecure_skip_verify = false ## HTTP Proxy Config - # proxy = "http://corporate.proxy:3128" + # http_proxy = "http://corporate.proxy:3128" ` // Connect initiates the primary connection to the range of provided URLs @@ -127,7 +127,7 @@ func (i *InfluxDB) Connect() error { UserAgent: i.UserAgent, Username: i.Username, Password: i.Password, - Proxy: i.Proxy, + HTTPProxy: i.HTTPProxy, } wp := client.WriteParams{ Database: i.Database,