2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2018-09-28 21:48:20 +00:00
|
|
|
"sort"
|
2016-09-08 14:22:10 +00:00
|
|
|
"testing"
|
2018-09-28 21:48:20 +00:00
|
|
|
"time"
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2020-06-05 14:43:43 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/processors"
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2018-09-28 21:48:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
2020-05-14 07:41:58 +00:00
|
|
|
// MockProcessor is a Processor with an overridable Apply implementation.
|
2018-09-28 21:48:20 +00:00
|
|
|
type MockProcessor struct {
|
|
|
|
ApplyF func(in ...telegraf.Metric) []telegraf.Metric
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (p *MockProcessor) SampleConfig() string {
|
|
|
|
return ""
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (p *MockProcessor) Description() string {
|
|
|
|
return ""
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (p *MockProcessor) Apply(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
return p.ApplyF(in...)
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
// TagProcessor returns a Processor whose Apply function adds the tag and
|
|
|
|
// value.
|
|
|
|
func TagProcessor(key, value string) *MockProcessor {
|
|
|
|
return &MockProcessor{
|
|
|
|
ApplyF: func(in ...telegraf.Metric) []telegraf.Metric {
|
|
|
|
for _, m := range in {
|
|
|
|
m.AddTag(key, value)
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func TestRunningProcessor_Apply(t *testing.T) {
|
|
|
|
type args struct {
|
2020-06-05 14:43:43 +00:00
|
|
|
Processor telegraf.StreamingProcessor
|
2018-09-28 21:48:20 +00:00
|
|
|
Config *ProcessorConfig
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
input []telegraf.Metric
|
|
|
|
expected []telegraf.Metric
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "inactive filter applies metrics",
|
|
|
|
args: args{
|
2020-06-05 14:43:43 +00:00
|
|
|
Processor: processors.NewStreamingProcessorFromProcessor(TagProcessor("apply", "true")),
|
2018-09-28 21:48:20 +00:00
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Filter: Filter{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"apply": "true",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter applies",
|
|
|
|
args: args{
|
2020-06-05 14:43:43 +00:00
|
|
|
Processor: processors.NewStreamingProcessorFromProcessor(TagProcessor("apply", "true")),
|
2018-09-28 21:48:20 +00:00
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"cpu"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"apply": "true",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filter doesn't apply",
|
|
|
|
args: args{
|
2020-06-05 14:43:43 +00:00
|
|
|
Processor: processors.NewStreamingProcessorFromProcessor(TagProcessor("apply", "true")),
|
2018-09-28 21:48:20 +00:00
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Filter: Filter{
|
|
|
|
NameDrop: []string{"cpu"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
2018-11-05 21:34:28 +00:00
|
|
|
testutil.MustMetric(
|
2018-09-28 21:48:20 +00:00
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42.0,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
rp := &RunningProcessor{
|
|
|
|
Processor: tt.args.Processor,
|
|
|
|
Config: tt.args.Config,
|
|
|
|
}
|
|
|
|
rp.Config.Filter.Compile()
|
|
|
|
|
2020-06-05 14:43:43 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
err := rp.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, m := range tt.input {
|
|
|
|
rp.Add(m, &acc)
|
|
|
|
}
|
|
|
|
rp.Stop()
|
|
|
|
|
|
|
|
actual := acc.GetTelegrafMetrics()
|
2018-09-28 21:48:20 +00:00
|
|
|
require.Equal(t, tt.expected, actual)
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func TestRunningProcessor_Order(t *testing.T) {
|
|
|
|
rp1 := &RunningProcessor{
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Order: 1,
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
rp2 := &RunningProcessor{
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Order: 2,
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
rp3 := &RunningProcessor{
|
|
|
|
Config: &ProcessorConfig{
|
|
|
|
Order: 3,
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
|
|
|
procs := RunningProcessors{rp2, rp3, rp1}
|
|
|
|
sort.Sort(procs)
|
|
|
|
require.Equal(t,
|
|
|
|
RunningProcessors{rp1, rp2, rp3},
|
|
|
|
procs)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|