Fix influxdb output serialization on connection closed (#6621)
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -288,7 +289,12 @@ func (c *httpClient) writeBatch(ctx context.Context, db string, metrics []telegr
|
||||
return err
|
||||
}
|
||||
|
||||
reader := influx.NewReader(metrics, c.config.Serializer)
|
||||
reader, err := c.requestBodyReader(metrics)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
req, err := c.makeWriteRequest(url, reader)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -386,12 +392,6 @@ func (c *httpClient) makeQueryRequest(query string) (*http.Request, error) {
|
||||
|
||||
func (c *httpClient) makeWriteRequest(url string, body io.Reader) (*http.Request, error) {
|
||||
var err error
|
||||
if c.config.ContentEncoding == "gzip" {
|
||||
body, err = internal.CompressWithGzip(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, body)
|
||||
if err != nil {
|
||||
@@ -408,6 +408,23 @@ func (c *httpClient) makeWriteRequest(url string, body io.Reader) (*http.Request
|
||||
return req, nil
|
||||
}
|
||||
|
||||
// requestBodyReader warp io.Reader from influx.NewReader to io.ReadCloser, which is usefully to fast close the write
|
||||
// side of the connection in case of error
|
||||
func (c *httpClient) requestBodyReader(metrics []telegraf.Metric) (io.ReadCloser, error) {
|
||||
reader := influx.NewReader(metrics, c.config.Serializer)
|
||||
|
||||
if c.config.ContentEncoding == "gzip" {
|
||||
rc, err := internal.CompressWithGzip(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
return ioutil.NopCloser(reader), nil
|
||||
}
|
||||
|
||||
func (c *httpClient) addHeaders(req *http.Request) {
|
||||
if c.config.Username != "" || c.config.Password != "" {
|
||||
req.SetBasicAuth(c.config.Username, c.config.Password)
|
||||
|
||||
Reference in New Issue
Block a user