From 144a2c9fb3584754e888a69bfc6cec1f25493f03 Mon Sep 17 00:00:00 2001 From: childe Date: Fri, 25 Oct 2024 11:51:38 +0800 Subject: [PATCH] Modify field config behavior in Json Filter use GetValueRender2 to render Json.field, so user could parse what is like `$message.inner_message_field` --- filter/json.go | 9 ++++++--- filter/json_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/filter/json.go b/filter/json.go index add8bac..9e479da 100644 --- a/filter/json.go +++ b/filter/json.go @@ -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 @@ -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") } @@ -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 } diff --git a/filter/json_test.go b/filter/json_test.go index dd1f5c2..9077630 100644 --- a/filter/json_test.go +++ b/filter/json_test.go @@ -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 {