diff --git a/config.go b/config.go index 4382fc7c0..48df238ac 100644 --- a/config.go +++ b/config.go @@ -51,18 +51,28 @@ func (c *Config) Plugins() map[string]*ast.Table { return c.plugins } +// The name of a tag, and the values on which to filter +type TagFilter struct { + Name string + Filter []string +} + // ConfiguredPlugin containing a name, interval, and drop/pass prefix lists +// Also lists the tags to filter type ConfiguredPlugin struct { Name string Drop []string Pass []string + TagDrop []TagFilter + TagPass []TagFilter + Interval time.Duration } // ShouldPass returns true if the metric should pass, false if should drop -func (cp *ConfiguredPlugin) ShouldPass(measurement string) bool { +func (cp *ConfiguredPlugin) ShouldPass(measurement string, tags map[string]string) bool { if cp.Pass != nil { for _, pat := range cp.Pass { if strings.HasPrefix(measurement, pat) { @@ -83,6 +93,32 @@ func (cp *ConfiguredPlugin) ShouldPass(measurement string) bool { return true } + if cp.TagPass != nil { + for _, pat := range cp.TagPass { + if tagval, ok := tags[pat.Name]; ok { + for _, filter := range pat.Filter { + if filter == tagval { + return true + } + } + } + } + return false + } + + if cp.TagDrop != nil { + for _, pat := range cp.TagDrop { + if tagval, ok := tags[pat.Name]; ok { + for _, filter := range pat.Filter { + if filter == tagval { + return false + } + } + } + } + return true + } + return true } @@ -140,9 +176,47 @@ func (c *Config) ApplyPlugin(name string, v interface{}) (*ConfiguredPlugin, err } } + if node, ok := tbl.Fields["tagpass"]; ok { + if subtbl, ok := node.(*ast.Table); ok { + for name, val := range subtbl.Fields { + if kv, ok := val.(*ast.KeyValue); ok { + tagfilter := &TagFilter{Name: name} + if ary, ok := kv.Value.(*ast.Array); ok { + for _, elem := range ary.Value { + if str, ok := elem.(*ast.String); ok { + tagfilter.Filter = append(tagfilter.Filter, str.Value) + } + } + } + cp.TagPass = append(cp.TagPass, *tagfilter) + } + } + } + } + + if node, ok := tbl.Fields["tagdrop"]; ok { + if subtbl, ok := node.(*ast.Table); ok { + for name, val := range subtbl.Fields { + if kv, ok := val.(*ast.KeyValue); ok { + tagfilter := &TagFilter{Name: name} + if ary, ok := kv.Value.(*ast.Array); ok { + for _, elem := range ary.Value { + if str, ok := elem.(*ast.String); ok { + tagfilter.Filter = append(tagfilter.Filter, str.Value) + } + } + } + cp.TagDrop = append(cp.TagDrop, *tagfilter) + } + } + } + } + delete(tbl.Fields, "drop") delete(tbl.Fields, "pass") delete(tbl.Fields, "interval") + delete(tbl.Fields, "tagdrop") + delete(tbl.Fields, "tagpass") return cp, toml.UnmarshalTable(tbl, v) }