2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-09-28 21:48:20 +00:00
|
|
|
"time"
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/metric"
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-04-12 23:06:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-01-22 18:54:12 +00:00
|
|
|
)
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
func TestFilter_ApplyEmpty(t *testing.T) {
|
|
|
|
f := Filter{}
|
|
|
|
require.NoError(t, f.Compile())
|
2018-09-28 21:48:20 +00:00
|
|
|
require.False(t, f.IsActive())
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("m",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{"value": int64(1)},
|
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, f.Select(m))
|
2016-09-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_ApplyTagsDontPass(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2016-09-05 15:16:37 +00:00
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
f := Filter{
|
|
|
|
TagDrop: filters,
|
|
|
|
}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
require.NoError(t, f.Compile())
|
2018-09-28 21:48:20 +00:00
|
|
|
require.True(t, f.IsActive())
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("m",
|
|
|
|
map[string]string{"cpu": "cpu-total"},
|
2016-09-05 15:16:37 +00:00
|
|
|
map[string]interface{}{"value": int64(1)},
|
2018-09-28 21:48:20 +00:00
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, f.Select(m))
|
2016-09-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_ApplyDeleteFields(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldDrop: []string{"value"},
|
|
|
|
}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
require.NoError(t, f.Compile())
|
2018-09-28 21:48:20 +00:00
|
|
|
require.True(t, f.IsActive())
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("m",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(1),
|
|
|
|
"value2": int64(2),
|
|
|
|
},
|
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, f.Select(m))
|
|
|
|
f.Modify(m)
|
|
|
|
require.Equal(t, map[string]interface{}{"value2": int64(2)}, m.Fields())
|
2016-09-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_ApplyDeleteAllFields(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldDrop: []string{"value*"},
|
|
|
|
}
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
require.NoError(t, f.Compile())
|
2018-09-28 21:48:20 +00:00
|
|
|
require.True(t, f.IsActive())
|
2016-09-05 15:16:37 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("m",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(1),
|
|
|
|
"value2": int64(2),
|
|
|
|
},
|
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, f.Select(m))
|
|
|
|
f.Modify(m)
|
|
|
|
require.Len(t, m.FieldList(), 0)
|
2016-09-05 15:16:37 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
func TestFilter_Empty(t *testing.T) {
|
|
|
|
f := Filter{}
|
|
|
|
|
|
|
|
measurements := []string{
|
|
|
|
"foo",
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"supercalifradjulisticexpialidocious",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range measurements {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldFieldPass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
func TestFilter_NamePass(t *testing.T) {
|
2016-01-22 18:54:12 +00:00
|
|
|
f := Filter{
|
2016-02-20 05:35:12 +00:00
|
|
|
NamePass: []string{"foo*", "cpu_usage_idle"},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldNamePass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldNamePass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
func TestFilter_NameDrop(t *testing.T) {
|
2016-01-22 18:54:12 +00:00
|
|
|
f := Filter{
|
2016-02-20 05:35:12 +00:00
|
|
|
NameDrop: []string{"foo*", "cpu_usage_idle"},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldNamePass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldNamePass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_FieldPass(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldPass: []string{"foo*", "cpu_usage_idle"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldFieldPass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldFieldPass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_FieldDrop(t *testing.T) {
|
|
|
|
f := Filter{
|
|
|
|
FieldDrop: []string{"foo*", "cpu_usage_idle"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
drops := []string{
|
|
|
|
"foo",
|
|
|
|
"foo_bar",
|
|
|
|
"foo.bar",
|
|
|
|
"foo-bar",
|
|
|
|
"cpu_usage_idle",
|
|
|
|
}
|
|
|
|
|
|
|
|
passes := []string{
|
|
|
|
"bar",
|
|
|
|
"barfoo",
|
|
|
|
"bar_foo",
|
|
|
|
"cpu_usage_busy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldFieldPass(measurement) {
|
2016-02-20 05:35:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to pass", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, measurement := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldFieldPass(measurement) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected measurement %s to drop", measurement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_TagPass(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2016-01-22 18:54:12 +00:00
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2016-01-22 18:54:12 +00:00
|
|
|
Name: "mem",
|
|
|
|
Filter: []string{"mem_free"},
|
|
|
|
}}
|
|
|
|
f := Filter{
|
|
|
|
TagPass: filters,
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
passes := [][]*telegraf.Tag{
|
2018-10-19 20:32:54 +00:00
|
|
|
{{Key: "cpu", Value: "cpu-total"}},
|
|
|
|
{{Key: "cpu", Value: "cpu-0"}},
|
|
|
|
{{Key: "cpu", Value: "cpu-1"}},
|
|
|
|
{{Key: "cpu", Value: "cpu-2"}},
|
|
|
|
{{Key: "mem", Value: "mem_free"}},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
drops := [][]*telegraf.Tag{
|
2018-10-19 20:32:54 +00:00
|
|
|
{{Key: "cpu", Value: "cputotal"}},
|
|
|
|
{{Key: "cpu", Value: "cpu0"}},
|
|
|
|
{{Key: "cpu", Value: "cpu1"}},
|
|
|
|
{{Key: "cpu", Value: "cpu2"}},
|
|
|
|
{{Key: "mem", Value: "mem_used"}},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to pass", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to drop", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_TagDrop(t *testing.T) {
|
|
|
|
filters := []TagFilter{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2016-01-22 18:54:12 +00:00
|
|
|
Name: "cpu",
|
|
|
|
Filter: []string{"cpu-*"},
|
|
|
|
},
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2016-01-22 18:54:12 +00:00
|
|
|
Name: "mem",
|
|
|
|
Filter: []string{"mem_free"},
|
|
|
|
}}
|
|
|
|
f := Filter{
|
|
|
|
TagDrop: filters,
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
drops := [][]*telegraf.Tag{
|
2018-10-19 20:32:54 +00:00
|
|
|
{{Key: "cpu", Value: "cpu-total"}},
|
|
|
|
{{Key: "cpu", Value: "cpu-0"}},
|
|
|
|
{{Key: "cpu", Value: "cpu-1"}},
|
|
|
|
{{Key: "cpu", Value: "cpu-2"}},
|
|
|
|
{{Key: "mem", Value: "mem_free"}},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
passes := [][]*telegraf.Tag{
|
2018-10-19 20:32:54 +00:00
|
|
|
{{Key: "cpu", Value: "cputotal"}},
|
|
|
|
{{Key: "cpu", Value: "cpu0"}},
|
|
|
|
{{Key: "cpu", Value: "cpu1"}},
|
|
|
|
{{Key: "cpu", Value: "cpu2"}},
|
|
|
|
{{Key: "mem", Value: "mem_used"}},
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range passes {
|
2016-09-05 15:16:37 +00:00
|
|
|
if !f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to pass", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range drops {
|
2016-09-05 15:16:37 +00:00
|
|
|
if f.shouldTagsPass(tags) {
|
2016-01-22 18:54:12 +00:00
|
|
|
t.Errorf("Expected tags %v to drop", tags)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
func TestFilter_FilterTagsNoMatches(t *testing.T) {
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("m",
|
|
|
|
map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{"value": int64(1)},
|
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
2016-04-12 23:06:27 +00:00
|
|
|
f := Filter{
|
|
|
|
TagExclude: []string{"nomatch"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
f.filterTags(m)
|
|
|
|
require.Equal(t, map[string]string{
|
2016-04-12 23:06:27 +00:00
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
2018-09-28 21:48:20 +00:00
|
|
|
}, m.Tags())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
f = Filter{
|
|
|
|
TagInclude: []string{"nomatch"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
f.filterTags(m)
|
|
|
|
require.Equal(t, map[string]string{}, m.Tags())
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_FilterTagsMatches(t *testing.T) {
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("m",
|
|
|
|
map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{"value": int64(1)},
|
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
2016-04-12 23:06:27 +00:00
|
|
|
f := Filter{
|
|
|
|
TagExclude: []string{"ho*"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
f.filterTags(m)
|
|
|
|
require.Equal(t, map[string]string{
|
2016-04-12 23:06:27 +00:00
|
|
|
"mytag": "foobar",
|
2018-09-28 21:48:20 +00:00
|
|
|
}, m.Tags())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err = metric.New("m",
|
|
|
|
map[string]string{
|
|
|
|
"host": "localhost",
|
|
|
|
"mytag": "foobar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{"value": int64(1)},
|
|
|
|
time.Now())
|
|
|
|
require.NoError(t, err)
|
2016-04-12 23:06:27 +00:00
|
|
|
f = Filter{
|
|
|
|
TagInclude: []string{"my*"},
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
require.NoError(t, f.Compile())
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
f.filterTags(m)
|
|
|
|
require.Equal(t, map[string]string{
|
2016-04-12 23:06:27 +00:00
|
|
|
"mytag": "foobar",
|
2018-09-28 21:48:20 +00:00
|
|
|
}, m.Tags())
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|
2017-07-19 20:08:40 +00:00
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
// TestFilter_FilterNamePassAndDrop used for check case when
|
|
|
|
// both parameters were defined
|
|
|
|
// see: https://github.com/influxdata/telegraf/issues/2860
|
|
|
|
func TestFilter_FilterNamePassAndDrop(t *testing.T) {
|
|
|
|
|
|
|
|
inputData := []string{"name1", "name2", "name3", "name4"}
|
|
|
|
expectedResult := []bool{false, true, false, false}
|
|
|
|
|
|
|
|
f := Filter{
|
|
|
|
NamePass: []string{"name1", "name2"},
|
|
|
|
NameDrop: []string{"name1", "name3"},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
|
|
|
|
for i, name := range inputData {
|
2018-09-28 21:48:20 +00:00
|
|
|
require.Equal(t, f.shouldNamePass(name), expectedResult[i])
|
2017-07-21 17:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestFilter_FilterFieldPassAndDrop used for check case when
|
|
|
|
// both parameters were defined
|
|
|
|
// see: https://github.com/influxdata/telegraf/issues/2860
|
|
|
|
func TestFilter_FilterFieldPassAndDrop(t *testing.T) {
|
|
|
|
|
|
|
|
inputData := []string{"field1", "field2", "field3", "field4"}
|
|
|
|
expectedResult := []bool{false, true, false, false}
|
|
|
|
|
|
|
|
f := Filter{
|
|
|
|
FieldPass: []string{"field1", "field2"},
|
|
|
|
FieldDrop: []string{"field1", "field3"},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
|
|
|
|
for i, field := range inputData {
|
2018-09-28 21:48:20 +00:00
|
|
|
require.Equal(t, f.shouldFieldPass(field), expectedResult[i])
|
2017-07-21 17:53:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:08:40 +00:00
|
|
|
// TestFilter_FilterTagsPassAndDrop used for check case when
|
|
|
|
// both parameters were defined
|
|
|
|
// see: https://github.com/influxdata/telegraf/issues/2860
|
|
|
|
func TestFilter_FilterTagsPassAndDrop(t *testing.T) {
|
2018-09-28 21:48:20 +00:00
|
|
|
inputData := [][]*telegraf.Tag{
|
2018-10-19 20:32:54 +00:00
|
|
|
{{Key: "tag1", Value: "1"}, {Key: "tag2", Value: "3"}},
|
|
|
|
{{Key: "tag1", Value: "1"}, {Key: "tag2", Value: "2"}},
|
|
|
|
{{Key: "tag1", Value: "2"}, {Key: "tag2", Value: "1"}},
|
|
|
|
{{Key: "tag1", Value: "4"}, {Key: "tag2", Value: "1"}},
|
2017-07-19 20:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedResult := []bool{false, true, false, false}
|
|
|
|
|
|
|
|
filterPass := []TagFilter{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2017-07-19 20:08:40 +00:00
|
|
|
Name: "tag1",
|
|
|
|
Filter: []string{"1", "4"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
filterDrop := []TagFilter{
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2017-07-19 20:08:40 +00:00
|
|
|
Name: "tag1",
|
|
|
|
Filter: []string{"4"},
|
|
|
|
},
|
2018-10-19 20:32:54 +00:00
|
|
|
{
|
2017-07-19 20:08:40 +00:00
|
|
|
Name: "tag2",
|
|
|
|
Filter: []string{"3"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
f := Filter{
|
|
|
|
TagDrop: filterDrop,
|
|
|
|
TagPass: filterPass,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, f.Compile())
|
|
|
|
|
|
|
|
for i, tag := range inputData {
|
2018-09-28 21:48:20 +00:00
|
|
|
require.Equal(t, f.shouldTagsPass(tag), expectedResult[i])
|
2017-07-19 20:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-05 21:34:28 +00:00
|
|
|
|
|
|
|
func BenchmarkFilter(b *testing.B) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
filter Filter
|
|
|
|
metric telegraf.Metric
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty filter",
|
|
|
|
filter: Filter{},
|
|
|
|
metric: testutil.MustMetric("cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "namepass",
|
|
|
|
filter: Filter{
|
|
|
|
NamePass: []string{"cpu"},
|
|
|
|
},
|
|
|
|
metric: testutil.MustMetric("cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
b.Run(tt.name, func(b *testing.B) {
|
|
|
|
require.NoError(b, tt.filter.Compile())
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tt.filter.Select(tt.metric)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|