From 019a9bb61d2c3841c32e942357f747903d11191b Mon Sep 17 00:00:00 2001 From: Greg <2653109+glinton@users.noreply.github.com> Date: Tue, 30 Jul 2019 15:16:51 -0600 Subject: [PATCH] Add ability to exclude db/bucket tag from influxdb outputs (#6184) --- plugins/outputs/influxdb/README.md | 3 ++ plugins/outputs/influxdb/http.go | 5 +++ plugins/outputs/influxdb/influxdb.go | 5 +++ plugins/outputs/influxdb_v2/README.md | 3 ++ plugins/outputs/influxdb_v2/http.go | 55 ++++++++++++++----------- plugins/outputs/influxdb_v2/influxdb.go | 51 ++++++++++++----------- 6 files changed, 75 insertions(+), 47 deletions(-) diff --git a/plugins/outputs/influxdb/README.md b/plugins/outputs/influxdb/README.md index 48ab3d51b92c1..1d11443ac904b 100644 --- a/plugins/outputs/influxdb/README.md +++ b/plugins/outputs/influxdb/README.md @@ -23,6 +23,9 @@ The InfluxDB output plugin writes metrics to the [InfluxDB v1.x] HTTP or UDP ser ## tag is not set the 'database' option is used as the default. # database_tag = "" + ## If true, the database tag will not be added to the metric. + # exclude_database_tag = false + ## If true, no CREATE DATABASE queries will be sent. Set to true when using ## Telegraf with a user without permissions to create databases or when the ## database already exists. diff --git a/plugins/outputs/influxdb/http.go b/plugins/outputs/influxdb/http.go index 794eee8b8c4bd..56576082f9bda 100644 --- a/plugins/outputs/influxdb/http.go +++ b/plugins/outputs/influxdb/http.go @@ -94,6 +94,7 @@ type HTTPConfig struct { ContentEncoding string Database string DatabaseTag string + ExcludeDatabaseTag bool RetentionPolicy string Consistency string SkipDatabaseCreation bool @@ -250,6 +251,10 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error batches[db] = make([]telegraf.Metric, 0) } + if c.config.ExcludeDatabaseTag { + metric.RemoveTag(c.config.DatabaseTag) + } + batches[db] = append(batches[db], metric) } diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index b2d1a90266259..b07d58fc217e4 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -38,6 +38,7 @@ type InfluxDB struct { Password string Database string DatabaseTag string `toml:"database_tag"` + ExcludeDatabaseTag bool `toml:"exclude_database_tag"` UserAgent string RetentionPolicy string WriteConsistency string @@ -77,6 +78,9 @@ var sampleConfig = ` ## tag is not set the 'database' option is used as the default. # database_tag = "" + ## If true, the database tag will not be added to the metric. + # exclude_database_tag = false + ## If true, no CREATE DATABASE queries will be sent. Set to true when using ## Telegraf with a user without permissions to create databases or when the ## database already exists. @@ -262,6 +266,7 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) Headers: i.HTTPHeaders, Database: i.Database, DatabaseTag: i.DatabaseTag, + ExcludeDatabaseTag: i.ExcludeDatabaseTag, SkipDatabaseCreation: i.SkipDatabaseCreation, RetentionPolicy: i.RetentionPolicy, Consistency: i.WriteConsistency, diff --git a/plugins/outputs/influxdb_v2/README.md b/plugins/outputs/influxdb_v2/README.md index 830e70b41e10f..226c3ab62bdfa 100644 --- a/plugins/outputs/influxdb_v2/README.md +++ b/plugins/outputs/influxdb_v2/README.md @@ -26,6 +26,9 @@ The InfluxDB output plugin writes metrics to the [InfluxDB v2.x] HTTP service. ## tag is not set the 'bucket' option is used as the default. # bucket_tag = "" + ## If true, the bucket tag will not be added to the metric. + # exclude_bucket_tag = false + ## Timeout for HTTP messages. # timeout = "5s" diff --git a/plugins/outputs/influxdb_v2/http.go b/plugins/outputs/influxdb_v2/http.go index a57a1bc672e56..47b7368441392 100644 --- a/plugins/outputs/influxdb_v2/http.go +++ b/plugins/outputs/influxdb_v2/http.go @@ -40,28 +40,30 @@ const ( ) type HTTPConfig struct { - URL *url.URL - Token string - Organization string - Bucket string - BucketTag string - Timeout time.Duration - Headers map[string]string - Proxy *url.URL - UserAgent string - ContentEncoding string - TLSConfig *tls.Config + URL *url.URL + Token string + Organization string + Bucket string + BucketTag string + ExcludeBucketTag bool + Timeout time.Duration + Headers map[string]string + Proxy *url.URL + UserAgent string + ContentEncoding string + TLSConfig *tls.Config Serializer *influx.Serializer } type httpClient struct { - ContentEncoding string - Timeout time.Duration - Headers map[string]string - Organization string - Bucket string - BucketTag string + ContentEncoding string + Timeout time.Duration + Headers map[string]string + Organization string + Bucket string + BucketTag string + ExcludeBucketTag bool client *http.Client serializer *influx.Serializer @@ -130,13 +132,14 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) { Timeout: timeout, Transport: transport, }, - url: config.URL, - ContentEncoding: config.ContentEncoding, - Timeout: timeout, - Headers: headers, - Organization: config.Organization, - Bucket: config.Bucket, - BucketTag: config.BucketTag, + url: config.URL, + ContentEncoding: config.ContentEncoding, + Timeout: timeout, + Headers: headers, + Organization: config.Organization, + Bucket: config.Bucket, + BucketTag: config.BucketTag, + ExcludeBucketTag: config.ExcludeBucketTag, } return client, nil } @@ -185,6 +188,10 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error batches[bucket] = make([]telegraf.Metric, 0) } + if c.ExcludeBucketTag { + metric.RemoveTag(c.BucketTag) + } + batches[bucket] = append(batches[bucket], metric) } diff --git a/plugins/outputs/influxdb_v2/influxdb.go b/plugins/outputs/influxdb_v2/influxdb.go index 8998ba3c71971..0f40a96e326da 100644 --- a/plugins/outputs/influxdb_v2/influxdb.go +++ b/plugins/outputs/influxdb_v2/influxdb.go @@ -42,6 +42,9 @@ var sampleConfig = ` ## tag is not set the 'bucket' option is used as the default. # bucket_tag = "" + ## If true, the bucket tag will not be added to the metric. + # exclude_bucket_tag = false + ## Timeout for HTTP messages. # timeout = "5s" @@ -78,17 +81,18 @@ type Client interface { } type InfluxDB struct { - URLs []string `toml:"urls"` - Token string `toml:"token"` - Organization string `toml:"organization"` - Bucket string `toml:"bucket"` - BucketTag string `toml:"bucket_tag"` - Timeout internal.Duration `toml:"timeout"` - HTTPHeaders map[string]string `toml:"http_headers"` - HTTPProxy string `toml:"http_proxy"` - UserAgent string `toml:"user_agent"` - ContentEncoding string `toml:"content_encoding"` - UintSupport bool `toml:"influx_uint_support"` + URLs []string `toml:"urls"` + Token string `toml:"token"` + Organization string `toml:"organization"` + Bucket string `toml:"bucket"` + BucketTag string `toml:"bucket_tag"` + ExcludeBucketTag bool `toml:"exclude_bucket_tag"` + Timeout internal.Duration `toml:"timeout"` + HTTPHeaders map[string]string `toml:"http_headers"` + HTTPProxy string `toml:"http_proxy"` + UserAgent string `toml:"user_agent"` + ContentEncoding string `toml:"content_encoding"` + UintSupport bool `toml:"influx_uint_support"` tls.ClientConfig clients []Client @@ -179,18 +183,19 @@ func (i *InfluxDB) getHTTPClient(ctx context.Context, url *url.URL, proxy *url.U } config := &HTTPConfig{ - URL: url, - Token: i.Token, - Organization: i.Organization, - Bucket: i.Bucket, - BucketTag: i.BucketTag, - Timeout: i.Timeout.Duration, - Headers: i.HTTPHeaders, - Proxy: proxy, - UserAgent: i.UserAgent, - ContentEncoding: i.ContentEncoding, - TLSConfig: tlsConfig, - Serializer: i.serializer, + URL: url, + Token: i.Token, + Organization: i.Organization, + Bucket: i.Bucket, + BucketTag: i.BucketTag, + ExcludeBucketTag: i.ExcludeBucketTag, + Timeout: i.Timeout.Duration, + Headers: i.HTTPHeaders, + Proxy: proxy, + UserAgent: i.UserAgent, + ContentEncoding: i.ContentEncoding, + TLSConfig: tlsConfig, + Serializer: i.serializer, } c, err := NewHTTPClient(config)