2016-07-28 11:31:11 +00:00
|
|
|
package models
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
import (
|
2016-04-12 23:06:27 +00:00
|
|
|
"fmt"
|
2016-01-22 18:54:12 +00:00
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
2016-01-22 18:54:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TagFilter is the name of a tag, and the values on which to filter
|
|
|
|
type TagFilter struct {
|
|
|
|
Name string
|
|
|
|
Filter []string
|
2016-06-02 17:47:15 +00:00
|
|
|
filter filter.Filter
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter containing drop/pass and tagdrop/tagpass rules
|
|
|
|
type Filter struct {
|
2016-02-20 05:35:12 +00:00
|
|
|
NameDrop []string
|
2016-06-02 17:47:15 +00:00
|
|
|
nameDrop filter.Filter
|
2016-02-20 05:35:12 +00:00
|
|
|
NamePass []string
|
2016-06-02 17:47:15 +00:00
|
|
|
namePass filter.Filter
|
2016-02-20 05:35:12 +00:00
|
|
|
|
|
|
|
FieldDrop []string
|
2016-06-02 17:47:15 +00:00
|
|
|
fieldDrop filter.Filter
|
2016-02-20 05:35:12 +00:00
|
|
|
FieldPass []string
|
2016-06-02 17:47:15 +00:00
|
|
|
fieldPass filter.Filter
|
2016-01-22 18:54:12 +00:00
|
|
|
|
|
|
|
TagDrop []TagFilter
|
|
|
|
TagPass []TagFilter
|
|
|
|
|
2016-04-12 23:06:27 +00:00
|
|
|
TagExclude []string
|
2016-06-02 17:47:15 +00:00
|
|
|
tagExclude filter.Filter
|
2016-04-12 23:06:27 +00:00
|
|
|
TagInclude []string
|
2016-06-02 17:47:15 +00:00
|
|
|
tagInclude filter.Filter
|
2016-04-12 23:06:27 +00:00
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
isActive bool
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
// Compile all Filter lists into filter.Filter objects.
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) Compile() error {
|
|
|
|
if len(f.NameDrop) == 0 &&
|
|
|
|
len(f.NamePass) == 0 &&
|
|
|
|
len(f.FieldDrop) == 0 &&
|
|
|
|
len(f.FieldPass) == 0 &&
|
|
|
|
len(f.TagInclude) == 0 &&
|
|
|
|
len(f.TagExclude) == 0 &&
|
|
|
|
len(f.TagPass) == 0 &&
|
|
|
|
len(f.TagDrop) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f.isActive = true
|
2016-04-12 23:06:27 +00:00
|
|
|
var err error
|
2016-09-05 15:16:37 +00:00
|
|
|
f.nameDrop, err = filter.Compile(f.NameDrop)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'namedrop', %s", err)
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
f.namePass, err = filter.Compile(f.NamePass)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'namepass', %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.fieldDrop, err = filter.Compile(f.FieldDrop)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'fielddrop', %s", err)
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
f.fieldPass, err = filter.Compile(f.FieldPass)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'fieldpass', %s", err)
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f.tagExclude, err = filter.Compile(f.TagExclude)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagexclude', %s", err)
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
f.tagInclude, err = filter.Compile(f.TagInclude)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'taginclude', %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, _ := range f.TagDrop {
|
2016-09-05 15:16:37 +00:00
|
|
|
f.TagDrop[i].filter, err = filter.Compile(f.TagDrop[i].Filter)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagdrop', %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, _ := range f.TagPass {
|
2016-09-05 15:16:37 +00:00
|
|
|
f.TagPass[i].filter, err = filter.Compile(f.TagPass[i].Filter)
|
2016-04-12 23:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error compiling 'tagpass', %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// Apply applies the filter to the given measurement name, fields map, and
|
|
|
|
// tags map. It will return false if the metric should be "filtered out", and
|
|
|
|
// true if the metric should "pass".
|
2016-09-08 14:22:10 +00:00
|
|
|
// It will modify tags & fields in-place if they need to be deleted.
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) Apply(
|
|
|
|
measurement string,
|
|
|
|
fields map[string]interface{},
|
|
|
|
tags map[string]string,
|
|
|
|
) bool {
|
|
|
|
if !f.isActive {
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-09-05 15:16:37 +00:00
|
|
|
|
|
|
|
// check if the measurement name should pass
|
|
|
|
if !f.shouldNamePass(measurement) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the tags should pass
|
|
|
|
if !f.shouldTagsPass(tags) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter fields
|
|
|
|
for fieldkey, _ := range fields {
|
|
|
|
if !f.shouldFieldPass(fieldkey) {
|
|
|
|
delete(fields, fieldkey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(fields) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter tags
|
|
|
|
f.filterTags(tags)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
// IsActive checking if filter is active
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) IsActive() bool {
|
|
|
|
return f.isActive
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// shouldNamePass returns true if the metric should pass, false if should drop
|
2016-02-20 05:35:12 +00:00
|
|
|
// based on the drop/pass filter parameters
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) shouldNamePass(key string) bool {
|
2017-07-21 17:53:57 +00:00
|
|
|
|
|
|
|
pass := func(f *Filter) bool {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.namePass.Match(key) {
|
|
|
|
return true
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
drop := func(f *Filter) bool {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.nameDrop.Match(key) {
|
|
|
|
return false
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
2017-07-21 17:53:57 +00:00
|
|
|
return true
|
2016-02-20 05:35:12 +00:00
|
|
|
}
|
2017-07-21 17:53:57 +00:00
|
|
|
|
|
|
|
if f.namePass != nil && f.nameDrop != nil {
|
|
|
|
return pass(f) && drop(f)
|
|
|
|
} else if f.namePass != nil {
|
|
|
|
return pass(f)
|
|
|
|
} else if f.nameDrop != nil {
|
|
|
|
return drop(f)
|
|
|
|
}
|
|
|
|
|
2016-02-20 05:35:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// shouldFieldPass returns true if the metric should pass, false if should drop
|
2016-01-22 18:54:12 +00:00
|
|
|
// based on the drop/pass filter parameters
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) shouldFieldPass(key string) bool {
|
2017-07-21 17:53:57 +00:00
|
|
|
|
|
|
|
pass := func(f *Filter) bool {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.fieldPass.Match(key) {
|
|
|
|
return true
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
drop := func(f *Filter) bool {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.fieldDrop.Match(key) {
|
|
|
|
return false
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2017-07-21 17:53:57 +00:00
|
|
|
return true
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
2017-07-21 17:53:57 +00:00
|
|
|
|
|
|
|
if f.fieldPass != nil && f.fieldDrop != nil {
|
|
|
|
return pass(f) && drop(f)
|
|
|
|
} else if f.fieldPass != nil {
|
|
|
|
return pass(f)
|
|
|
|
} else if f.fieldDrop != nil {
|
|
|
|
return drop(f)
|
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
// shouldTagsPass returns true if the metric should pass, false if should drop
|
2016-01-22 18:54:12 +00:00
|
|
|
// based on the tagdrop/tagpass filter parameters
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) shouldTagsPass(tags map[string]string) bool {
|
2017-07-19 20:08:40 +00:00
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
pass := func(f *Filter) bool {
|
2016-01-22 18:54:12 +00:00
|
|
|
for _, pat := range f.TagPass {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter.Match(tagval) {
|
|
|
|
return true
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-21 17:53:57 +00:00
|
|
|
drop := func(f *Filter) bool {
|
2016-01-22 18:54:12 +00:00
|
|
|
for _, pat := range f.TagDrop {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-22 18:54:12 +00:00
|
|
|
if tagval, ok := tags[pat.Name]; ok {
|
2016-04-12 23:06:27 +00:00
|
|
|
if pat.filter.Match(tagval) {
|
|
|
|
return false
|
2016-01-22 18:54:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:08:40 +00:00
|
|
|
// Add additional logic in case where both parameters are set.
|
|
|
|
// see: https://github.com/influxdata/telegraf/issues/2860
|
|
|
|
if f.TagPass != nil && f.TagDrop != nil {
|
|
|
|
// return true only in case when tag pass and won't be dropped (true, true).
|
|
|
|
// in case when the same tag should be passed and dropped it will be dropped (true, false).
|
2017-07-21 17:53:57 +00:00
|
|
|
return pass(f) && drop(f)
|
2017-07-19 20:08:40 +00:00
|
|
|
} else if f.TagPass != nil {
|
2017-07-21 17:53:57 +00:00
|
|
|
return pass(f)
|
2017-07-19 20:08:40 +00:00
|
|
|
} else if f.TagDrop != nil {
|
2017-07-21 17:53:57 +00:00
|
|
|
return drop(f)
|
2017-07-19 20:08:40 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 18:54:12 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
|
|
|
|
// Apply TagInclude and TagExclude filters.
|
|
|
|
// modifies the tags map in-place.
|
2016-09-05 15:16:37 +00:00
|
|
|
func (f *Filter) filterTags(tags map[string]string) {
|
2016-04-12 23:06:27 +00:00
|
|
|
if f.tagInclude != nil {
|
|
|
|
for k, _ := range tags {
|
|
|
|
if !f.tagInclude.Match(k) {
|
|
|
|
delete(tags, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.tagExclude != nil {
|
|
|
|
for k, _ := range tags {
|
|
|
|
if f.tagExclude.Match(k) {
|
|
|
|
delete(tags, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|