From b68c2d48ef3679dd013f898817103e507edc7040 Mon Sep 17 00:00:00 2001 From: Simone Rotondo Date: Fri, 16 Jun 2017 21:05:08 +0200 Subject: [PATCH] Add HTTP Proxy support to influxdb output (#2929) --- plugins/outputs/influxdb/README.md | 4 ++++ plugins/outputs/influxdb/client/http.go | 26 +++++++++++++++++++++---- plugins/outputs/influxdb/influxdb.go | 7 ++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/plugins/outputs/influxdb/README.md b/plugins/outputs/influxdb/README.md index 3cd029d8c955b..49fdc731c4bc6 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 + # http_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) +* `http_proxy`: HTTP Proxy URI diff --git a/plugins/outputs/influxdb/client/http.go b/plugins/outputs/influxdb/client/http.go index 4acea23627ae6..6a757982d52c8 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.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, + } + } + return &httpClient{ writeURL: writeURL(u, defaultWP), config: config, url: u, client: &http.Client{ - Timeout: config.Timeout, - Transport: &http.Transport{ - TLSClientConfig: config.TLSConfig, - }, + Timeout: config.Timeout, + 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" + HTTPProxy 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..1028bc257496b 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -32,7 +32,8 @@ type InfluxDB struct { RetentionPolicy string WriteConsistency string Timeout internal.Duration - UDPPayload int `toml:"udp_payload"` + UDPPayload int `toml:"udp_payload"` + HTTPProxy string `toml:"http_proxy"` // 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 + # http_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, + HTTPProxy: i.HTTPProxy, } wp := client.WriteParams{ Database: i.Database,