Skip to content

Commit

Permalink
feat: make write request public
Browse files Browse the repository at this point in the history
We have a use case where manipulating the information we have to shape
them as influxdb.Metric is very expensive. We already have the line
protocol for them.

This change allow us to pass the line protocol directly to the Write
function without having to do any extra manipulation.

Signed-off-by: Gianluca Arbezzano <[email protected]>
Co-Authored-by: Chris Goller  <[email protected]>
  • Loading branch information
Gianluca Arbezzano and goller committed Feb 6, 2020
1 parent 0ee3dfa commit 7753914
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ func (c *Client) Write(ctx context.Context, bucket, org string, m ...Metric) (n
}
}

req, err := c.makeWriteRequest(bucket, org, buf)
var req *http.Request

if c.contentEncoding == "gzip" {
req, err = NewWriteGzipRequest(c.url, c.userAgent, c.authorization, bucket, org, c.compressionLevel, buf)
} else {
req, err = NewWriteRequest(c.url, c.userAgent, c.authorization, bucket, org, buf)
}
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -78,16 +84,13 @@ func parseInt32(v string) (int32, error) {
return int32(retry), nil
}

func (c *Client) makeWriteRequest(bucket, org string, body io.Reader) (*http.Request, error) {
var err error
if c.contentEncoding == "gzip" {
body, err = gzip.CompressWithGzip(body, c.compressionLevel)
if err != nil {
return nil, err
}
func NewWriteGzipRequest(url *url.URL, userAgent, token, bucket, org string, compressionLevel int, body io.Reader) (*http.Request, error) {
body, err := gzip.CompressWithGzip(body, compressionLevel)
if err != nil {
return nil, err
}

u, err := makeWriteURL(c.url, bucket, org)
u, err := makeWriteURL(url, bucket, org)
if err != nil {
return nil, err
}
Expand All @@ -98,14 +101,26 @@ func (c *Client) makeWriteRequest(bucket, org string, body io.Reader) (*http.Req
}

req.Header.Set("Content-Type", "text/plain; charset=utf-8")
req.Header.Set("Content-Encoding", "gzip")
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Authorization", token)
return req, nil
}

if c.contentEncoding == "gzip" {
req.Header.Set("Content-Encoding", "gzip")
func NewWriteRequest(url *url.URL, userAgent, token, bucket, org string, body io.Reader) (*http.Request, error) {
u, err := makeWriteURL(url, bucket, org)
if err != nil {
return nil, err
}

req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Authorization", c.authorization)
req, err := http.NewRequest(http.MethodPost, u, body)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "text/plain; charset=utf-8")
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Authorization", token)
return req, nil
}

Expand Down

0 comments on commit 7753914

Please sign in to comment.