From 3dab8450405f63c8623c09f16efda24f8c525fb8 Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Mon, 13 Apr 2020 10:57:48 -0700 Subject: [PATCH] Fix Name field in template processor (#7258) --- .../processors/template/template_metric.go | 7 ++-- plugins/processors/template/template_test.go | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/plugins/processors/template/template_metric.go b/plugins/processors/template/template_metric.go index 47d86ec57..e4a81bd1c 100644 --- a/plugins/processors/template/template_metric.go +++ b/plugins/processors/template/template_metric.go @@ -1,16 +1,17 @@ package template import ( - "github.com/influxdata/telegraf" "time" + + "github.com/influxdata/telegraf" ) type TemplateMetric struct { metric telegraf.Metric } -func (m *TemplateMetric) Measurement() string { - return m.Measurement() +func (m *TemplateMetric) Name() string { + return m.metric.Name() } func (m *TemplateMetric) Tag(key string) string { diff --git a/plugins/processors/template/template_test.go b/plugins/processors/template/template_test.go index b8c195cda..f43d69795 100644 --- a/plugins/processors/template/template_test.go +++ b/plugins/processors/template/template_test.go @@ -7,8 +7,45 @@ import ( "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/testutil" "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) { now := time.Now()