Add ability to exclude db/bucket tag from influxdb outputs (#6184)

This commit is contained in:
Greg 2019-07-30 15:16:51 -06:00 committed by Daniel Nelson
parent 3f63c14179
commit a1bff8f550
6 changed files with 75 additions and 47 deletions

View File

@ -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. ## tag is not set the 'database' option is used as the default.
# database_tag = "" # 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 ## 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 ## Telegraf with a user without permissions to create databases or when the
## database already exists. ## database already exists.

View File

@ -94,6 +94,7 @@ type HTTPConfig struct {
ContentEncoding string ContentEncoding string
Database string Database string
DatabaseTag string DatabaseTag string
ExcludeDatabaseTag bool
RetentionPolicy string RetentionPolicy string
Consistency string Consistency string
SkipDatabaseCreation bool SkipDatabaseCreation bool
@ -250,6 +251,10 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error
batches[db] = make([]telegraf.Metric, 0) batches[db] = make([]telegraf.Metric, 0)
} }
if c.config.ExcludeDatabaseTag {
metric.RemoveTag(c.config.DatabaseTag)
}
batches[db] = append(batches[db], metric) batches[db] = append(batches[db], metric)
} }

View File

@ -38,6 +38,7 @@ type InfluxDB struct {
Password string Password string
Database string Database string
DatabaseTag string `toml:"database_tag"` DatabaseTag string `toml:"database_tag"`
ExcludeDatabaseTag bool `toml:"exclude_database_tag"`
UserAgent string UserAgent string
RetentionPolicy string RetentionPolicy string
WriteConsistency string WriteConsistency string
@ -77,6 +78,9 @@ var sampleConfig = `
## tag is not set the 'database' option is used as the default. ## tag is not set the 'database' option is used as the default.
# database_tag = "" # 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 ## 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 ## Telegraf with a user without permissions to create databases or when the
## database already exists. ## database already exists.
@ -262,6 +266,7 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL)
Headers: i.HTTPHeaders, Headers: i.HTTPHeaders,
Database: i.Database, Database: i.Database,
DatabaseTag: i.DatabaseTag, DatabaseTag: i.DatabaseTag,
ExcludeDatabaseTag: i.ExcludeDatabaseTag,
SkipDatabaseCreation: i.SkipDatabaseCreation, SkipDatabaseCreation: i.SkipDatabaseCreation,
RetentionPolicy: i.RetentionPolicy, RetentionPolicy: i.RetentionPolicy,
Consistency: i.WriteConsistency, Consistency: i.WriteConsistency,

View File

@ -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. ## tag is not set the 'bucket' option is used as the default.
# bucket_tag = "" # bucket_tag = ""
## If true, the bucket tag will not be added to the metric.
# exclude_bucket_tag = false
## Timeout for HTTP messages. ## Timeout for HTTP messages.
# timeout = "5s" # timeout = "5s"

View File

@ -40,28 +40,30 @@ const (
) )
type HTTPConfig struct { type HTTPConfig struct {
URL *url.URL URL *url.URL
Token string Token string
Organization string Organization string
Bucket string Bucket string
BucketTag string BucketTag string
Timeout time.Duration ExcludeBucketTag bool
Headers map[string]string Timeout time.Duration
Proxy *url.URL Headers map[string]string
UserAgent string Proxy *url.URL
ContentEncoding string UserAgent string
TLSConfig *tls.Config ContentEncoding string
TLSConfig *tls.Config
Serializer *influx.Serializer Serializer *influx.Serializer
} }
type httpClient struct { type httpClient struct {
ContentEncoding string ContentEncoding string
Timeout time.Duration Timeout time.Duration
Headers map[string]string Headers map[string]string
Organization string Organization string
Bucket string Bucket string
BucketTag string BucketTag string
ExcludeBucketTag bool
client *http.Client client *http.Client
serializer *influx.Serializer serializer *influx.Serializer
@ -130,13 +132,14 @@ func NewHTTPClient(config *HTTPConfig) (*httpClient, error) {
Timeout: timeout, Timeout: timeout,
Transport: transport, Transport: transport,
}, },
url: config.URL, url: config.URL,
ContentEncoding: config.ContentEncoding, ContentEncoding: config.ContentEncoding,
Timeout: timeout, Timeout: timeout,
Headers: headers, Headers: headers,
Organization: config.Organization, Organization: config.Organization,
Bucket: config.Bucket, Bucket: config.Bucket,
BucketTag: config.BucketTag, BucketTag: config.BucketTag,
ExcludeBucketTag: config.ExcludeBucketTag,
} }
return client, nil return client, nil
} }
@ -185,6 +188,10 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error
batches[bucket] = make([]telegraf.Metric, 0) batches[bucket] = make([]telegraf.Metric, 0)
} }
if c.ExcludeBucketTag {
metric.RemoveTag(c.BucketTag)
}
batches[bucket] = append(batches[bucket], metric) batches[bucket] = append(batches[bucket], metric)
} }

View File

@ -42,6 +42,9 @@ var sampleConfig = `
## tag is not set the 'bucket' option is used as the default. ## tag is not set the 'bucket' option is used as the default.
# bucket_tag = "" # bucket_tag = ""
## If true, the bucket tag will not be added to the metric.
# exclude_bucket_tag = false
## Timeout for HTTP messages. ## Timeout for HTTP messages.
# timeout = "5s" # timeout = "5s"
@ -78,17 +81,18 @@ type Client interface {
} }
type InfluxDB struct { type InfluxDB struct {
URLs []string `toml:"urls"` URLs []string `toml:"urls"`
Token string `toml:"token"` Token string `toml:"token"`
Organization string `toml:"organization"` Organization string `toml:"organization"`
Bucket string `toml:"bucket"` Bucket string `toml:"bucket"`
BucketTag string `toml:"bucket_tag"` BucketTag string `toml:"bucket_tag"`
Timeout internal.Duration `toml:"timeout"` ExcludeBucketTag bool `toml:"exclude_bucket_tag"`
HTTPHeaders map[string]string `toml:"http_headers"` Timeout internal.Duration `toml:"timeout"`
HTTPProxy string `toml:"http_proxy"` HTTPHeaders map[string]string `toml:"http_headers"`
UserAgent string `toml:"user_agent"` HTTPProxy string `toml:"http_proxy"`
ContentEncoding string `toml:"content_encoding"` UserAgent string `toml:"user_agent"`
UintSupport bool `toml:"influx_uint_support"` ContentEncoding string `toml:"content_encoding"`
UintSupport bool `toml:"influx_uint_support"`
tls.ClientConfig tls.ClientConfig
clients []Client clients []Client
@ -179,18 +183,19 @@ func (i *InfluxDB) getHTTPClient(ctx context.Context, url *url.URL, proxy *url.U
} }
config := &HTTPConfig{ config := &HTTPConfig{
URL: url, URL: url,
Token: i.Token, Token: i.Token,
Organization: i.Organization, Organization: i.Organization,
Bucket: i.Bucket, Bucket: i.Bucket,
BucketTag: i.BucketTag, BucketTag: i.BucketTag,
Timeout: i.Timeout.Duration, ExcludeBucketTag: i.ExcludeBucketTag,
Headers: i.HTTPHeaders, Timeout: i.Timeout.Duration,
Proxy: proxy, Headers: i.HTTPHeaders,
UserAgent: i.UserAgent, Proxy: proxy,
ContentEncoding: i.ContentEncoding, UserAgent: i.UserAgent,
TLSConfig: tlsConfig, ContentEncoding: i.ContentEncoding,
Serializer: i.serializer, TLSConfig: tlsConfig,
Serializer: i.serializer,
} }
c, err := NewHTTPClient(config) c, err := NewHTTPClient(config)