Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add influxdb gzip support #2978

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change the option to be content_encoding = "gzip" supporting only gzip, this will allow us to potentially add more encodings later and IMO reads better.

```

### 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)
37 changes: 30 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,24 @@ func (c *httpClient) makeWriteRequest(
return nil, err
}
req.Header.Set("Content-Length", fmt.Sprint(contentLength))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to complicate things... if we are using gzip we need to report the length of the body here after compression, but since we are now streaming the data it is trickier. I think we need to use Transfer-Encoding: chunked and this header is unset for gzip.

I haven't ever done this with go, maybe we can use https://golang.org/pkg/net/http/httputil/#NewChunkedWriter? Or maybe it will be automatically handled if we don't set this header?

// TODO
// if gzip {
// req.Header.Set("Content-Encoding", "gzip")
// }
if c.config.Gzip {
req.Header.Set("Content-Encoding", "gzip")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move all the Header code into makeRequest, I'm not sure why they should be separate.

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.
body, err = compressWithGzip(body)
if err != nil {
return nil, err
}
}
req, err = http.NewRequest("POST", uri, body)
if err != nil {
return nil, err
}
Expand All @@ -253,6 +262,20 @@ func (c *httpClient) makeRequest(uri string, body io.Reader) (*http.Request, err
return req, nil
}

func compressWithGzip(data io.Reader) (io.Reader, error) {
pr, pw := io.Pipe()
gw := gzip.NewWriter(pw)
var err error

go func() {
_, err = io.Copy(gw, data)
gw.Close()
pw.Close()
}()

return pr, err
}

func (c *httpClient) Close() error {
// Nothing to do.
return nil
Expand Down
22 changes: 22 additions & 0 deletions plugins/outputs/influxdb/client/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bytes"
"compress/gzip"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -341,3 +342,24 @@ func TestHTTPClient_Query_JSONDecodeError(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "json")
}

func TestGzipCompression(t *testing.T) {
influxLine := "cpu value=99\n"

// Compress the payload using GZIP.
payload := bytes.NewReader([]byte(influxLine))
compressed, err := compressWithGzip(payload)
assert.Nil(t, err)

// Decompress the compressed payload and make sure
// that its original value has not changed.
gr, err := gzip.NewReader(compressed)
assert.Nil(t, err)
gr.Close()

var uncompressed bytes.Buffer
_, err = uncompressed.ReadFrom(gr)
assert.Nil(t, err)

assert.Equal(t, []byte(influxLine), uncompressed.Bytes())
}
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