Fix exclude database and retention policy tags is shared (#7323)

This commit is contained in:
Daniel Nelson 2020-04-14 12:15:35 -07:00 committed by GitHub
parent 48f5da7e33
commit eb8d805370
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -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. // Avoid modifying the metric in case we need to retry the request.
metric = metric.Copy() metric = metric.Copy()
metric.Accept() metric.Accept()
metric.RemoveTag(c.config.DatabaseTag) if c.config.ExcludeDatabaseTag {
metric.RemoveTag(c.config.RetentionPolicyTag) metric.RemoveTag(c.config.DatabaseTag)
}
if c.config.ExcludeRetentionPolicyTag {
metric.RemoveTag(c.config.RetentionPolicyTag)
}
} }
batches[dbrp] = append(batches[dbrp], metric) batches[dbrp] = append(batches[dbrp], metric)

View File

@ -907,6 +907,37 @@ func TestDBRPTags(t *testing.T) {
w.WriteHeader(http.StatusNoContent) 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {