HTTP headers can be added to InfluxDB output (#3182)

This commit is contained in:
Dylan Meissner 2017-08-28 16:08:50 -07:00 committed by Daniel Nelson
parent d21fbf14f4
commit 701b34dc55
4 changed files with 35 additions and 3 deletions

View File

@ -44,6 +44,9 @@ This plugin writes to [InfluxDB](https://www.influxdb.com) via HTTP or UDP.
## HTTP Proxy Config ## HTTP Proxy Config
# http_proxy = "http://corporate.proxy:3128" # http_proxy = "http://corporate.proxy:3128"
## Optional HTTP headers
# http_headers = {"X-Special-Header" = "Special-Value"}
## Compress each HTTP request payload using GZIP. ## Compress each HTTP request payload using GZIP.
# content_encoding = "gzip" # content_encoding = "gzip"
``` ```
@ -70,4 +73,5 @@ to write to. Each URL should start with either `http://` or `udp://`
* `ssl_key`: SSL key * `ssl_key`: SSL key
* `insecure_skip_verify`: Use SSL but skip chain & host verification (default: false) * `insecure_skip_verify`: Use SSL but skip chain & host verification (default: false)
* `http_proxy`: HTTP Proxy URI * `http_proxy`: HTTP Proxy URI
* `http_headers`: HTTP headers to add to each HTTP request
* `content_encoding`: Compress each HTTP request payload using gzip if set to: "gzip" * `content_encoding`: Compress each HTTP request payload using gzip if set to: "gzip"

View File

@ -68,6 +68,8 @@ func NewHTTP(config HTTPConfig, defaultWP WriteParams) (Client, error) {
}, nil }, nil
} }
type HTTPHeaders map[string]string
type HTTPConfig struct { type HTTPConfig struct {
// URL should be of the form "http://host:port" (REQUIRED) // URL should be of the form "http://host:port" (REQUIRED)
URL string URL string
@ -95,6 +97,9 @@ type HTTPConfig struct {
// Proxy URL should be of the form "http://host:port" // Proxy URL should be of the form "http://host:port"
HTTPProxy string HTTPProxy string
// HTTP headers to append to HTTP requests.
HTTPHeaders HTTPHeaders
// The content encoding mechanism to use for each request. // The content encoding mechanism to use for each request.
ContentEncoding string ContentEncoding string
} }
@ -253,6 +258,11 @@ func (c *httpClient) makeRequest(uri string, body io.Reader) (*http.Request, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
for header, value := range c.config.HTTPHeaders {
req.Header.Set(header, value)
}
req.Header.Set("Content-Type", "text/plain") req.Header.Set("Content-Type", "text/plain")
req.Header.Set("User-Agent", c.config.UserAgent) req.Header.Set("User-Agent", c.config.UserAgent)
if c.config.Username != "" && c.config.Password != "" { if c.config.Username != "" && c.config.Password != "" {

View File

@ -55,6 +55,13 @@ func TestHTTPClient_Write(t *testing.T) {
fmt.Fprintln(w, `{"results":[{}],"error":"basic auth incorrect"}`) fmt.Fprintln(w, `{"results":[{}],"error":"basic auth incorrect"}`)
} }
// test that user-specified http header is set properly
if r.Header.Get("X-Test-Header") != "Test-Value" {
w.WriteHeader(http.StatusTeapot)
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, `{"results":[{}],"error":"wrong http header value"}`)
}
// Validate Content-Length Header // Validate Content-Length Header
if r.ContentLength != 13 { if r.ContentLength != 13 {
w.WriteHeader(http.StatusTeapot) w.WriteHeader(http.StatusTeapot)
@ -90,6 +97,9 @@ func TestHTTPClient_Write(t *testing.T) {
UserAgent: "test-agent", UserAgent: "test-agent",
Username: "test-user", Username: "test-user",
Password: "test-password", Password: "test-password",
HTTPHeaders: HTTPHeaders{
"X-Test-Header": "Test-Value",
},
} }
wp := WriteParams{ wp := WriteParams{
Database: "test", Database: "test",

View File

@ -32,9 +32,10 @@ type InfluxDB struct {
RetentionPolicy string RetentionPolicy string
WriteConsistency string WriteConsistency string
Timeout internal.Duration Timeout internal.Duration
UDPPayload int `toml:"udp_payload"` UDPPayload int `toml:"udp_payload"`
HTTPProxy string `toml:"http_proxy"` HTTPProxy string `toml:"http_proxy"`
ContentEncoding string `toml:"content_encoding"` HTTPHeaders map[string]string `toml:"http_headers"`
ContentEncoding string `toml:"content_encoding"`
// Path to CA file // Path to CA file
SSLCA string `toml:"ssl_ca"` SSLCA string `toml:"ssl_ca"`
@ -89,6 +90,9 @@ var sampleConfig = `
## HTTP Proxy Config ## HTTP Proxy Config
# http_proxy = "http://corporate.proxy:3128" # http_proxy = "http://corporate.proxy:3128"
## Optional HTTP headers
# http_headers = {"X-Special-Header" = "Special-Value"}
## Compress each HTTP request payload using GZIP. ## Compress each HTTP request payload using GZIP.
# content_encoding = "gzip" # content_encoding = "gzip"
` `
@ -132,8 +136,12 @@ func (i *InfluxDB) Connect() error {
Username: i.Username, Username: i.Username,
Password: i.Password, Password: i.Password,
HTTPProxy: i.HTTPProxy, HTTPProxy: i.HTTPProxy,
HTTPHeaders: client.HTTPHeaders{},
ContentEncoding: i.ContentEncoding, ContentEncoding: i.ContentEncoding,
} }
for header, value := range i.HTTPHeaders {
config.HTTPHeaders[header] = value
}
wp := client.WriteParams{ wp := client.WriteParams{
Database: i.Database, Database: i.Database,
RetentionPolicy: i.RetentionPolicy, RetentionPolicy: i.RetentionPolicy,