Fix Name field in template processor (#7258)

This commit is contained in:
Daniel Nelson 2020-04-13 10:57:48 -07:00 committed by GitHub
parent cc6c77f301
commit 3dab845040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 3 deletions

View File

@ -1,16 +1,17 @@
package template package template
import ( import (
"github.com/influxdata/telegraf"
"time" "time"
"github.com/influxdata/telegraf"
) )
type TemplateMetric struct { type TemplateMetric struct {
metric telegraf.Metric metric telegraf.Metric
} }
func (m *TemplateMetric) Measurement() string { func (m *TemplateMetric) Name() string {
return m.Measurement() return m.metric.Name()
} }
func (m *TemplateMetric) Tag(key string) string { func (m *TemplateMetric) Tag(key string) string {

View File

@ -7,8 +7,45 @@ import (
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestName(t *testing.T) {
plugin := TemplateProcessor{
Tag: "measurement",
Template: "{{ .Name }}",
}
err := plugin.Init()
require.NoError(t, err)
input := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{},
map[string]interface{}{
"time_idle": 42,
},
time.Unix(0, 0),
),
}
actual := plugin.Apply(input...)
expected := []telegraf.Metric{
testutil.MustMetric(
"cpu",
map[string]string{
"measurement": "cpu",
},
map[string]interface{}{
"time_idle": 42,
},
time.Unix(0, 0),
),
}
testutil.RequireMetricsEqual(t, expected, actual)
}
func TestTagTemplateConcatenate(t *testing.T) { func TestTagTemplateConcatenate(t *testing.T) {
now := time.Now() now := time.Now()