Add support for skipping database creation (#3941)

This commit is contained in:
Daniel Nelson 2018-03-27 17:59:57 -07:00 committed by GitHub
parent e0c3b7ff2b
commit c695fdf77e
2 changed files with 36 additions and 27 deletions

View File

@ -17,6 +17,11 @@ This InfluxDB output plugin writes metrics to the [InfluxDB](https://github.com/
## The target database for metrics; will be created as needed.
# database = "telegraf"
## 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.
# skip_database_creation = false
## Name of existing retention policy to write to. Empty string writes to
## the default retention policy.
# retention_policy = ""

View File

@ -44,6 +44,7 @@ type InfluxDB struct {
HTTPProxy string `toml:"http_proxy"`
HTTPHeaders map[string]string `toml:"http_headers"`
ContentEncoding string `toml:"content_encoding"`
SkipDatabaseCreation bool `toml:"skip_database_creation"`
// Path to CA file
SSLCA string `toml:"ssl_ca"`
@ -75,6 +76,11 @@ var sampleConfig = `
## The target database for metrics; will be created as needed.
# database = "telegraf"
## 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.
# skip_database_creation = false
## Name of existing retention policy to write to. Empty string writes to
## the default retention policy.
# retention_policy = ""
@ -194,6 +200,7 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
switch apiError := err.(type) {
case APIError:
if !i.SkipDatabaseCreation {
if apiError.Type == DatabaseNotFound {
err := client.CreateDatabase(ctx)
if err != nil {
@ -202,6 +209,7 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
}
}
}
}
log.Printf("E! [outputs.influxdb]: when writing to [%s]: %v", client.URL(), err)
}
@ -252,17 +260,13 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL)
return nil, fmt.Errorf("error creating HTTP client [%s]: %v", url, err)
}
if !i.SkipDatabaseCreation {
err = c.CreateDatabase(ctx)
if err != nil {
if err, ok := err.(APIError); ok {
if err.StatusCode == 503 {
return c, nil
}
}
log.Printf("W! [outputs.influxdb] when writing to [%s]: database %q creation failed: %v",
c.URL(), c.Database(), err)
}
}
return c, nil
}