forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_test.go
117 lines (93 loc) · 2.9 KB
/
template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package template
import (
"testing"
"time"
"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()
// Create Template processor
tmp := TemplateProcessor{Tag: "topic", Template: `{{.Tag "hostname"}}.{{ .Tag "level" }}`}
// manually init
err := tmp.Init()
if err != nil {
panic(err)
}
// create metric for testing
input := []telegraf.Metric{testutil.MustMetric("Tags", map[string]string{"hostname": "localhost", "level": "debug"}, nil, now)}
// act
actual := tmp.Apply(input[0])
// assert
expected := []telegraf.Metric{testutil.MustMetric("Tags", map[string]string{"hostname": "localhost", "level": "debug", "topic": "localhost.debug"}, nil, now)}
testutil.RequireMetricsEqual(t, expected, actual)
}
func TestMetricMissingTagsIsNotLost(t *testing.T) {
now := time.Now()
// Create Template processor
tmp := TemplateProcessor{Tag: "topic", Template: `{{.Tag "hostname"}}.{{ .Tag "level" }}`}
// manually init
err := tmp.Init()
if err != nil {
panic(err)
}
// create metrics for testing
m1 := testutil.MustMetric("Works", map[string]string{"hostname": "localhost", "level": "debug"}, nil, now)
m2 := testutil.MustMetric("Fails", map[string]string{"hostname": "localhost"}, nil, now)
// act
actual := tmp.Apply(m1, m2)
// assert
// make sure no metrics are lost when a template process fails
assert.Equal(t, 2, len(actual), "Number of metrics input should equal number of metrics output")
}
func TestTagAndFieldConcatenate(t *testing.T) {
now := time.Now()
// Create Template processor
tmp := TemplateProcessor{Tag: "LocalTemp", Template: `{{.Tag "location"}} is {{ .Field "temperature" }}`}
// manually init
err := tmp.Init()
if err != nil {
panic(err)
}
// create metric for testing
m1 := testutil.MustMetric("weather", map[string]string{"location": "us-midwest"}, map[string]interface{}{"temperature": "too warm"}, now)
// act
actual := tmp.Apply(m1)
// assert
expected := []telegraf.Metric{testutil.MustMetric("weather", map[string]string{"location": "us-midwest", "LocalTemp": "us-midwest is too warm"}, map[string]interface{}{"temperature": "too warm"}, now)}
testutil.RequireMetricsEqual(t, expected, actual)
}