Skip to content

Commit

Permalink
Fix Name field in template processor (#7258)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3dab845)
  • Loading branch information
danielnelson committed Apr 13, 2020
1 parent 88b075d commit 4ca8014
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
7 changes: 4 additions & 3 deletions plugins/processors/template/template_metric.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
37 changes: 37 additions & 0 deletions plugins/processors/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 4ca8014

Please sign in to comment.