Skip to content

Commit

Permalink
Fix metrics reported as written but not actually written (#9526)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8d2b1e8)
  • Loading branch information
MyaLongmire authored and reimda committed Jul 28, 2021
1 parent d77b244 commit a9a95b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
17 changes: 17 additions & 0 deletions plugins/outputs/influxdb/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,19 @@ func TestDBRPTagsCreateDatabaseCalledOnDatabaseNotFound(t *testing.T) {

handlers := &MockHandlerChain{
handlers: []http.HandlerFunc{
func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/query":
if r.FormValue("q") != `CREATE DATABASE "telegraf"` {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusForbidden)
w.Write([]byte(`{"results": [{"error": "error authorizing query"}]}`))
default:
w.WriteHeader(http.StatusInternalServerError)
}
},
func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/write":
Expand Down Expand Up @@ -1133,8 +1146,12 @@ func TestDBRPTagsCreateDatabaseCalledOnDatabaseNotFound(t *testing.T) {

err = output.Connect()
require.NoError(t, err)

// this write fails, but we're expecting it to drop the metrics and not retry, so no error.
err = output.Write(metrics)
require.NoError(t, err)

// expects write to succeed
err = output.Write(metrics)
require.NoError(t, err)

Expand Down
24 changes: 14 additions & 10 deletions plugins/outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//nolint
package influxdb

import (
Expand Down Expand Up @@ -224,17 +225,20 @@ func (i *InfluxDB) Write(metrics []telegraf.Metric) error {

switch apiError := err.(type) {
case *DatabaseNotFoundError:
if !i.SkipDatabaseCreation {
allErrorsAreDatabaseNotFoundErrors = false
err := client.CreateDatabase(ctx, apiError.Database)
if err != nil {
i.Log.Errorf("When writing to [%s]: database %q not found and failed to recreate",
client.URL(), apiError.Database)
} else {
// try another client, if all clients fail with this error, do not return error
continue
}
if i.SkipDatabaseCreation {
continue
}
// retry control
// error so the write is retried
err := client.CreateDatabase(ctx, apiError.Database)
if err != nil {
i.Log.Errorf("When writing to [%s]: database %q not found and failed to recreate",
client.URL(), apiError.Database)
} else {
return errors.New("database created; retry write")
}
default:
allErrorsAreDatabaseNotFoundErrors = false
}
}

Expand Down

0 comments on commit a9a95b4

Please sign in to comment.