Skip to content

Commit

Permalink
Add influxdb gzip support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Shannon committed Jul 3, 2017
1 parent 74a764d commit 599aa5d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions plugins/outputs/influxdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ This plugin writes to [InfluxDB](https://www.influxdb.com) via HTTP or UDP.

## HTTP Proxy Config
# http_proxy = "http://corporate.proxy:3128"

## Compress each HTTP request payload using GZIP, defaults to false.
# gzip = false
```

### Required parameters:
Expand All @@ -67,3 +70,4 @@ to write to. Each URL should start with either `http://` or `udp://`
* `ssl_key`: SSL key
* `insecure_skip_verify`: Use SSL but skip chain & host verification (default: false)
* `http_proxy`: HTTP Proxy URI
* `gzip`: Compress each HTTP request payload using gzip (default: false)
38 changes: 31 additions & 7 deletions plugins/outputs/influxdb/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -95,8 +96,7 @@ type HTTPConfig struct {
HTTPProxy string

// Gzip, if true, compresses each payload using gzip.
// TODO
// Gzip bool
Gzip bool
}

// Response represents a list of statement results.
Expand Down Expand Up @@ -233,15 +233,27 @@ func (c *httpClient) makeWriteRequest(
return nil, err
}
req.Header.Set("Content-Length", fmt.Sprint(contentLength))
// TODO
// if gzip {
// req.Header.Set("Content-Encoding", "gzip")
// }
if c.config.Gzip {
req.Header.Set("Content-Encoding", "gzip")
}
return req, nil
}

func (c *httpClient) makeRequest(uri string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest("POST", uri, body)
var req *http.Request
var err error
if c.config.Gzip {
// If gzip is set to true, then compress
// the payload.
buf := new(bytes.Buffer)
buf.ReadFrom(body)
compressed, err := compressWithGzip(buf.Bytes())
if err != nil {
return nil, err
}
body = bytes.NewBuffer(compressed)
}
req, err = http.NewRequest("POST", uri, body)
if err != nil {
return nil, err
}
Expand All @@ -253,6 +265,18 @@ func (c *httpClient) makeRequest(uri string, body io.Reader) (*http.Request, err
return req, nil
}

func compressWithGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write(data); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func (c *httpClient) Close() error {
// Nothing to do.
return nil
Expand Down
5 changes: 5 additions & 0 deletions plugins/outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type InfluxDB struct {
Timeout internal.Duration
UDPPayload int `toml:"udp_payload"`
HTTPProxy string `toml:"http_proxy"`
Gzip bool

// Path to CA file
SSLCA string `toml:"ssl_ca"`
Expand Down Expand Up @@ -87,6 +88,9 @@ var sampleConfig = `
## HTTP Proxy Config
# http_proxy = "http://corporate.proxy:3128"
## Compress each HTTP request payload using GZIP, defaults to false.
# gzip = false
`

// Connect initiates the primary connection to the range of provided URLs
Expand Down Expand Up @@ -128,6 +132,7 @@ func (i *InfluxDB) Connect() error {
Username: i.Username,
Password: i.Password,
HTTPProxy: i.HTTPProxy,
Gzip: i.Gzip,
}
wp := client.WriteParams{
Database: i.Database,
Expand Down

0 comments on commit 599aa5d

Please sign in to comment.