Skip to content

Commit

Permalink
fix #99
Browse files Browse the repository at this point in the history
建议添加KV模板的include_keys方法
  • Loading branch information
childe committed Aug 31, 2020
1 parent 9cef8ab commit 83aa87a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
47 changes: 37 additions & 10 deletions filter/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
)

type KVFilter struct {
config map[interface{}]interface{}
fields map[field_setter.FieldSetter]value_render.ValueRender
src value_render.ValueRender
target string
field_split string
value_split string
trim string
trim_key string
config map[interface{}]interface{}
fields map[field_setter.FieldSetter]value_render.ValueRender
src value_render.ValueRender
target string
field_split string
value_split string
trim string
trim_key string
include_keys map[string]bool
exclude_keys map[string]bool
}

func (l *MethodLibrary) NewKVFilter(config map[interface{}]interface{}) *KVFilter {
Expand Down Expand Up @@ -60,6 +62,21 @@ func (l *MethodLibrary) NewKVFilter(config map[interface{}]interface{}) *KVFilte
} else {
plugin.trim_key = ""
}

plugin.include_keys = make(map[string]bool)
if include_keys, ok := config["include_keys"]; ok {
for _, k := range include_keys.([]interface{}) {
plugin.include_keys[k.(string)] = true
}
}

plugin.exclude_keys = make(map[string]bool)
if exclude_keys, ok := config["exclude_keys"]; ok {
for _, k := range exclude_keys.([]interface{}) {
plugin.exclude_keys[k.(string)] = true
}
}

return plugin
}

Expand All @@ -79,14 +96,24 @@ func (p *KVFilter) Filter(event map[string]interface{}) (map[string]interface{},
event[p.target] = o
}

success := true
var success bool = true
var key string
for _, kv := range A {
a := strings.SplitN(kv, p.value_split, 2)
if len(a) != 2 {
success = false
continue
}
o[strings.Trim(a[0], p.trim_key)] = strings.Trim(a[1], p.trim)

key = strings.Trim(a[0], p.trim_key)

if _, ok := p.exclude_keys[key]; ok {
continue
}

if _, ok := p.include_keys[key]; len(p.include_keys) == 0 || ok {
o[key] = strings.Trim(a[1], p.trim)
}
}
return event, success
}
36 changes: 36 additions & 0 deletions filter/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@ package filter

import "testing"

func TestIncludeKeys(t *testing.T) {
config := make(map[interface{}]interface{})
config["field_split"] = " "
config["value_split"] = "="
config["src"] = "message"
config["include_keys"] = []interface{}{"a", "b", "c", "xyz"}
config["exclude_keys"] = []interface{}{"c"}
f := methodLibrary.NewKVFilter(config)

event := make(map[string]interface{})
event["message"] = "a=aaa b=bbb c=ccc xyz=\txyzxyz\t d=ddd"
t.Log(event)

event, ok := f.Filter(event)
if !ok {
t.Error("kv failed")
}
t.Log(event)

if event["a"] != "aaa" {
t.Error("kv failed")
}
if event["b"] != "bbb" {
t.Error("kv failed")
}
if _, ok := event["c"]; ok {
t.Error("c is excluded")
}
if event["xyz"] != "\txyzxyz\t" {
t.Error("kv failed")
}
if _, ok := event["d"]; ok {
t.Error("d is excluded")
}
}

func TestKVFilter(t *testing.T) {
config := make(map[interface{}]interface{})
config["field_split"] = " "
Expand Down

0 comments on commit 83aa87a

Please sign in to comment.