-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #1726
- Loading branch information
Showing
10 changed files
with
298 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package telegraf | ||
|
||
type FilterPlugin interface { | ||
// SampleConfig returns the default configuration of the Input | ||
SampleConfig() string | ||
|
||
// Description returns a one-sentence description on the Input | ||
Description() string | ||
|
||
// Apply the filter to the given metric | ||
Apply(in ...Metric) []Metric | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package models | ||
|
||
import ( | ||
"github.com/influxdata/telegraf" | ||
) | ||
|
||
type RunningFilterPlugin struct { | ||
Name string | ||
FilterPlugin telegraf.FilterPlugin | ||
Config *FilterPluginConfig | ||
} | ||
|
||
// FilterConfig containing a name and filter | ||
type FilterPluginConfig struct { | ||
Name string | ||
Filter Filter | ||
} | ||
|
||
func (rf *RunningFilterPlugin) Apply(in ...telegraf.Metric) []telegraf.Metric { | ||
ret := []telegraf.Metric{} | ||
|
||
for _, metric := range in { | ||
if rf.Config.Filter.IsActive() { | ||
// check if the filter should be applied to this metric | ||
if ok := rf.Config.Filter.Apply(metric.Name(), metric.Fields(), metric.Tags()); !ok { | ||
// this means filter should not be applied | ||
ret = append(ret, metric) | ||
continue | ||
} | ||
} | ||
// This metric should pass through the filter, so call the filter Apply | ||
// function and append results to the output slice. | ||
ret = append(ret, rf.FilterPlugin.Apply(metric)...) | ||
} | ||
|
||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package models | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/testutil" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestFilterPlugin struct { | ||
} | ||
|
||
func (f *TestFilterPlugin) SampleConfig() string { return "" } | ||
func (f *TestFilterPlugin) Description() string { return "" } | ||
|
||
// Apply renames: | ||
// "foo" to "fuz" | ||
// "bar" to "baz" | ||
func (f *TestFilterPlugin) Apply(in ...telegraf.Metric) []telegraf.Metric { | ||
out := make([]telegraf.Metric, 0) | ||
for _, m := range in { | ||
switch m.Name() { | ||
case "foo": | ||
out = append(out, testutil.TestMetric(1, "fuz")) | ||
case "bar": | ||
out = append(out, testutil.TestMetric(1, "baz")) | ||
default: | ||
out = append(out, m) | ||
} | ||
} | ||
return out | ||
} | ||
|
||
func NewTestRunningFilterPlugin() *RunningFilterPlugin { | ||
out := &RunningFilterPlugin{ | ||
Name: "test", | ||
FilterPlugin: &TestFilterPlugin{}, | ||
Config: &FilterPluginConfig{Filter: Filter{}}, | ||
} | ||
return out | ||
} | ||
|
||
func TestRunningFilterPlugin(t *testing.T) { | ||
inmetrics := []telegraf.Metric{ | ||
testutil.TestMetric(1, "foo"), | ||
testutil.TestMetric(1, "bar"), | ||
testutil.TestMetric(1, "baz"), | ||
} | ||
|
||
expectedNames := []string{ | ||
"fuz", | ||
"baz", | ||
"baz", | ||
} | ||
rfp := NewTestRunningFilterPlugin() | ||
filteredMetrics := rfp.Apply(inmetrics...) | ||
|
||
actualNames := []string{ | ||
filteredMetrics[0].Name(), | ||
filteredMetrics[1].Name(), | ||
filteredMetrics[2].Name(), | ||
} | ||
assert.Equal(t, expectedNames, actualNames) | ||
} | ||
|
||
func TestRunningFilterPlugin_WithNameDrop(t *testing.T) { | ||
inmetrics := []telegraf.Metric{ | ||
testutil.TestMetric(1, "foo"), | ||
testutil.TestMetric(1, "bar"), | ||
testutil.TestMetric(1, "baz"), | ||
} | ||
|
||
expectedNames := []string{ | ||
"foo", | ||
"baz", | ||
"baz", | ||
} | ||
rfp := NewTestRunningFilterPlugin() | ||
|
||
rfp.Config.Filter.NameDrop = []string{"foo"} | ||
assert.NoError(t, rfp.Config.Filter.Compile()) | ||
|
||
filteredMetrics := rfp.Apply(inmetrics...) | ||
|
||
actualNames := []string{ | ||
filteredMetrics[0].Name(), | ||
filteredMetrics[1].Name(), | ||
filteredMetrics[2].Name(), | ||
} | ||
assert.Equal(t, expectedNames, actualNames) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package all | ||
|
||
import ( | ||
_ "github.com/influxdata/telegraf/plugins/filters/printer" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/filters" | ||
) | ||
|
||
type Printer struct { | ||
} | ||
|
||
var sampleConfig = ` | ||
` | ||
|
||
func (p *Printer) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (p *Printer) Description() string { | ||
return "Print all metrics that pass through this filter." | ||
} | ||
|
||
func (p *Printer) Apply(in ...telegraf.Metric) []telegraf.Metric { | ||
for _, metric := range in { | ||
fmt.Println(metric.String()) | ||
} | ||
return in | ||
} | ||
|
||
func init() { | ||
filters.Add("printer", func() telegraf.FilterPlugin { | ||
return &Printer{} | ||
}) | ||
} |
Oops, something went wrong.