Apply global and plugin level metric modifications before filtering (#5152)

This commit is contained in:
Daniel Nelson 2018-12-18 14:20:43 -08:00 committed by GitHub
parent 7caa5d20af
commit 544262a23a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 7 deletions

View File

@ -62,12 +62,6 @@ func (r *RunningInput) MakeMetric(metric telegraf.Metric) telegraf.Metric {
return nil
}
r.Config.Filter.Modify(metric)
if len(metric.FieldList()) == 0 {
r.metricFiltered(metric)
return nil
}
m := makemetric(
metric,
r.Config.NameOverride,
@ -76,6 +70,12 @@ func (r *RunningInput) MakeMetric(metric telegraf.Metric) telegraf.Metric {
r.Config.Tags,
r.defaultTags)
r.Config.Filter.Modify(metric)
if len(metric.FieldList()) == 0 {
r.metricFiltered(metric)
return nil
}
r.MetricsGathered.Incr(1)
GlobalMetricsGathered.Incr(1)
return m

View File

@ -7,11 +7,43 @@ import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMakeMetricFilterAfterApplyingGlobalTags(t *testing.T) {
now := time.Now()
ri := NewRunningInput(&testInput{}, &InputConfig{
Filter: Filter{
TagInclude: []string{"b"},
},
})
require.NoError(t, ri.Config.Filter.Compile())
ri.SetDefaultTags(map[string]string{"a": "x", "b": "y"})
m, err := metric.New("cpu",
map[string]string{},
map[string]interface{}{
"value": 42,
},
now)
require.NoError(t, err)
actual := ri.MakeMetric(m)
expected, err := metric.New("cpu",
map[string]string{
"b": "y",
},
map[string]interface{}{
"value": 42,
},
now)
require.NoError(t, err)
testutil.RequireMetricEqual(t, expected, actual)
}
func TestMakeMetricNoFields(t *testing.T) {
now := time.Now()
ri := NewRunningInput(&testInput{}, &InputConfig{