diff --git a/plugins/outputs/influxdb/http.go b/plugins/outputs/influxdb/http.go index b663d9198..5f25572d3 100644 --- a/plugins/outputs/influxdb/http.go +++ b/plugins/outputs/influxdb/http.go @@ -272,8 +272,12 @@ func (c *httpClient) Write(ctx context.Context, metrics []telegraf.Metric) error // Avoid modifying the metric in case we need to retry the request. metric = metric.Copy() metric.Accept() - metric.RemoveTag(c.config.DatabaseTag) - metric.RemoveTag(c.config.RetentionPolicyTag) + if c.config.ExcludeDatabaseTag { + metric.RemoveTag(c.config.DatabaseTag) + } + if c.config.ExcludeRetentionPolicyTag { + metric.RemoveTag(c.config.RetentionPolicyTag) + } } batches[dbrp] = append(batches[dbrp], metric) diff --git a/plugins/outputs/influxdb/http_test.go b/plugins/outputs/influxdb/http_test.go index 3f5ef0bc6..a59fadaf4 100644 --- a/plugins/outputs/influxdb/http_test.go +++ b/plugins/outputs/influxdb/http_test.go @@ -907,6 +907,37 @@ func TestDBRPTags(t *testing.T) { w.WriteHeader(http.StatusNoContent) }, }, + { + name: "exclude database tag keeps retention policy tag", + config: influxdb.HTTPConfig{ + URL: u, + SkipDatabaseCreation: true, + Database: "telegraf", + RetentionPolicyTag: "rp", + ExcludeDatabaseTag: true, + Log: testutil.Logger{}, + }, + metrics: []telegraf.Metric{ + testutil.MustMetric( + "cpu", + map[string]string{ + "rp": "foo", + }, + map[string]interface{}{ + "value": 42.0, + }, + time.Unix(0, 0), + ), + }, + handlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) { + require.Equal(t, r.FormValue("db"), "telegraf") + require.Equal(t, r.FormValue("rp"), "foo") + body, err := ioutil.ReadAll(r.Body) + require.NoError(t, err) + require.Contains(t, string(body), "cpu,rp=foo value=42") + w.WriteHeader(http.StatusNoContent) + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {