Skip to content
Open
Show file tree
Hide file tree
Changes from all 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_v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ The InfluxDB output plugin writes metrics to the [InfluxDB v2.x] HTTP service.
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

## Timestamp precision
## InfluxDB accepts the following precisions: "ns" (default), "us", "ms", "s"

```

[InfluxDB v2.x]: https://github.com/influxdata/influxdb
26 changes: 24 additions & 2 deletions plugins/outputs/influxdb_v2/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type HTTPConfig struct {
UserAgent string
ContentEncoding string
TLSConfig *tls.Config
Precision string

Serializer *influx.Serializer
}
Expand All @@ -65,6 +66,7 @@ type httpClient struct {
Bucket string
BucketTag string
ExcludeBucketTag bool
Precision string

client *http.Client
serializer *influx.Serializer
Expand Down Expand Up @@ -127,6 +129,13 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) {
return nil, fmt.Errorf("unsupported scheme %q", config.URL.Scheme)
}

precision := config.Precision
if precision != "" && !checkValidPrecision(precision) {
return nil, fmt.Errorf("unsupported precision %v", precision)
} else {
precision = "ns"
}

client := &httpClient{
serializer: serializer,
client: &http.Client{
Expand All @@ -141,6 +150,7 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) {
Bucket: config.Bucket,
BucketTag: config.BucketTag,
ExcludeBucketTag: config.ExcludeBucketTag,
Precision: precision,
}
return client, nil
}
Expand Down Expand Up @@ -210,7 +220,7 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error
}

func (c *httpClient) writeBatch(ctx context.Context, bucket string, metrics []telegraf.Metric) error {
url, err := makeWriteURL(*c.url, c.Organization, bucket)
url, err := makeWriteURL(*c.url, c.Organization, bucket, c.Precision)
if err != nil {
return err
}
Expand Down Expand Up @@ -327,10 +337,11 @@ func (c *httpClient) addHeaders(req *http.Request) {
}
}

func makeWriteURL(loc url.URL, org, bucket string) (string, error) {
func makeWriteURL(loc url.URL, org, bucket string, precision string) (string, error) {
params := url.Values{}
params.Set("bucket", bucket)
params.Set("org", org)
params.Set("precision", precision)

switch loc.Scheme {
case "unix":
Expand All @@ -343,9 +354,20 @@ func makeWriteURL(loc url.URL, org, bucket string) (string, error) {
return "", fmt.Errorf("unsupported scheme: %q", loc.Scheme)
}
loc.RawQuery = params.Encode()
fmt.Printf("%v\n", loc.RequestURI())
return loc.String(), nil
}

func (c *httpClient) Close() {
internal.CloseIdleConnections(c.client)
}

func checkValidPrecision(precision string) bool {
validPrecisions := [4]string{"ns", "us", "ms", "s"}
for _, value := range validPrecisions {
if value == precision {
return true
}
}
return false
}
6 changes: 6 additions & 0 deletions plugins/outputs/influxdb_v2/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ var sampleConfig = `
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

## Timestamp precision
## InfluxDB accepts the following precisions: "ns" (default), "us", "ms", "s"

`

type Client interface {
Expand All @@ -94,6 +98,7 @@ type InfluxDB struct {
UserAgent string `toml:"user_agent"`
ContentEncoding string `toml:"content_encoding"`
UintSupport bool `toml:"influx_uint_support"`
Precision string `toml:"precision"`
tls.ClientConfig

clients []Client
Expand Down Expand Up @@ -191,6 +196,7 @@ func (i *InfluxDB) getHTTPClient(ctx context.Context, url *url.URL, proxy *url.U
ContentEncoding: i.ContentEncoding,
TLSConfig: tlsConfig,
Serializer: i.newSerializer(),
Precision: i.Precision,
}

c, err := NewHTTPClient(config)
Expand Down