Skip to content

Commit

Permalink
Modify field config behavior in Json Filter
Browse files Browse the repository at this point in the history
use GetValueRender2 to render Json.field, so user could parse what is
like `$message.inner_message_field`
  • Loading branch information
childe committed Oct 25, 2024
1 parent 2522db2 commit 144a2c9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
9 changes: 6 additions & 3 deletions filter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"strings"

"github.com/childe/gohangout/topology"
"github.com/childe/gohangout/value_render"
"k8s.io/klog/v2"
)

// JSONFilter will parse json string in `field` and put the result into `target` field
type JSONFilter struct {
field string
vr value_render.ValueRender
target string
overwrite bool
include []string
Expand All @@ -30,6 +32,7 @@ func newJSONFilter(config map[interface{}]interface{}) topology.Filter {

if field, ok := config["field"]; ok {
plugin.field = field.(string)
plugin.vr = value_render.GetValueRender2(plugin.field)
} else {
klog.Fatal("field must be set in Json filter")
}
Expand Down Expand Up @@ -58,12 +61,12 @@ func newJSONFilter(config map[interface{}]interface{}) topology.Filter {

// Filter will parse json string in `field` and put the result into `target` field
func (plugin *JSONFilter) Filter(event map[string]interface{}) (map[string]interface{}, bool) {
s, ok := event[plugin.field]
if !ok {
f := plugin.vr.Render(event)
if f == nil {
return event, false
}

ss, ok := s.(string)
ss, ok := f.(string)
if !ok {
return event, false
}
Expand Down
32 changes: 32 additions & 0 deletions filter/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ func TestJson(t *testing.T) {
},
true,
},
{
map[string]interface{}{
"message": `{"message":"hello","b":2}`,
"a": 10,
},
map[interface{}]interface{}{
"field": "message",
"overwrite": true,
},
map[string]interface{}{
"message": "hello",
"a": 10,
"b": json.Number("2"),
},
true,
},
{
map[string]interface{}{
"message": `{"message":"hello","b":2}`,
"a": 10,
},
map[interface{}]interface{}{
"field": "$.message",
"overwrite": false,
},
map[string]interface{}{
"message": `{"message":"hello","b":2}`,
"a": 10,
"b": json.Number("2"),
},
true,
},
}

for _, c := range cases {
Expand Down

0 comments on commit 144a2c9

Please sign in to comment.