Skip to content
Snippets Groups Projects
Unverified Commit cc97b48c authored by Daniel Nelson's avatar Daniel Nelson Committed by GitHub
Browse files

Add support for skipping database creation (#3941)

parent 36b82201
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,11 @@ This InfluxDB output plugin writes metrics to the [InfluxDB](https://github.com/ ...@@ -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. ## The target database for metrics; will be created as needed.
# database = "telegraf" # 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 ## Name of existing retention policy to write to. Empty string writes to
## the default retention policy. ## the default retention policy.
# retention_policy = "" # retention_policy = ""
......
...@@ -31,19 +31,20 @@ type Client interface { ...@@ -31,19 +31,20 @@ type Client interface {
// InfluxDB struct is the primary data structure for the plugin // InfluxDB struct is the primary data structure for the plugin
type InfluxDB struct { type InfluxDB struct {
URL string // url deprecated in 0.1.9; use urls URL string // url deprecated in 0.1.9; use urls
URLs []string `toml:"urls"` URLs []string `toml:"urls"`
Username string Username string
Password string Password string
Database string Database string
UserAgent string UserAgent string
RetentionPolicy string RetentionPolicy string
WriteConsistency string WriteConsistency string
Timeout internal.Duration Timeout internal.Duration
UDPPayload int `toml:"udp_payload"` UDPPayload int `toml:"udp_payload"`
HTTPProxy string `toml:"http_proxy"` HTTPProxy string `toml:"http_proxy"`
HTTPHeaders map[string]string `toml:"http_headers"` HTTPHeaders map[string]string `toml:"http_headers"`
ContentEncoding string `toml:"content_encoding"` ContentEncoding string `toml:"content_encoding"`
SkipDatabaseCreation bool `toml:"skip_database_creation"`
// Path to CA file // Path to CA file
SSLCA string `toml:"ssl_ca"` SSLCA string `toml:"ssl_ca"`
...@@ -75,6 +76,11 @@ var sampleConfig = ` ...@@ -75,6 +76,11 @@ var sampleConfig = `
## The target database for metrics; will be created as needed. ## The target database for metrics; will be created as needed.
# database = "telegraf" # 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 ## Name of existing retention policy to write to. Empty string writes to
## the default retention policy. ## the default retention policy.
# retention_policy = "" # retention_policy = ""
...@@ -194,11 +200,13 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error { ...@@ -194,11 +200,13 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {
switch apiError := err.(type) { switch apiError := err.(type) {
case APIError: case APIError:
if apiError.Type == DatabaseNotFound { if !i.SkipDatabaseCreation {
err := client.CreateDatabase(ctx) if apiError.Type == DatabaseNotFound {
if err != nil { err := client.CreateDatabase(ctx)
log.Printf("E! [outputs.influxdb] when writing to [%s]: database %q not found and failed to recreate", if err != nil {
client.URL(), client.Database()) log.Printf("E! [outputs.influxdb] when writing to [%s]: database %q not found and failed to recreate",
client.URL(), client.Database())
}
} }
} }
} }
...@@ -252,16 +260,12 @@ func (i *InfluxDB) httpClient(ctx context.Context, url *url.URL, proxy *url.URL) ...@@ -252,16 +260,12 @@ 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) return nil, fmt.Errorf("error creating HTTP client [%s]: %v", url, err)
} }
err = c.CreateDatabase(ctx) if !i.SkipDatabaseCreation {
if err != nil { err = c.CreateDatabase(ctx)
if err, ok := err.(APIError); ok { if err != nil {
if err.StatusCode == 503 { log.Printf("W! [outputs.influxdb] when writing to [%s]: database %q creation failed: %v",
return c, nil c.URL(), c.Database(), err)
}
} }
log.Printf("W! [outputs.influxdb] when writing to [%s]: database %q creation failed: %v",
c.URL(), c.Database(), err)
} }
return c, nil return c, nil
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment