-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add target for decode_json_fields #3169
Add target for decode_json_fields #3169
Conversation
Jenkins standing by to test this. If you aren't a maintainer, you can ignore this comment. Someone with commit access, please review this and clear it for Jenkins to run. |
1 similar comment
Jenkins standing by to test this. If you aren't a maintainer, you can ignore this comment. Someone with commit access, please review this and clear it for Jenkins to run. |
This implementation added the output into the |
This PR would break some of the use cases below. I recommend keeping the current behavior but adding an optional There is no need to remove the original field in this processor because that could be done using using the Use cases:
|
I'm +1 on adding a "target" option. |
Are there names which the target field can not be named to. For example |
@@ -17,18 +17,21 @@ type decodeJSONFields struct { | |||
fields []string | |||
maxDepth int | |||
processArray bool | |||
target string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if I want to target the root of the event? How would I configure this?
I think if you make it a *string
then you can determine the difference between target
being set but empty and target
not present at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for that tip 👍 Still have to learn a lot when coming from OO. There is one more question. Like you say if we want to add to root it now looks like
{
"": {
"test": {
"test": "test"
}
},
"@timestamp": "2016-12-14T08:20:16.157Z",
"beat": {
"hostname": "4201halwsd00001",
"name": "4201halwsd00001",
"version": "6.0.0-alpha1"
},
"input_type": "log",
"message": "{\n \"test\": \"test\",\n \"test\": {\n \"test\": \"test\"\n }\n}",
"offset": 73,
"source": "input.json",
"type": "log"
}
It looks right? Or should we filter for that empty string and print depth one
{
"test": {
"test": "test"
},
"@timestamp": "2016-12-14T08:20:16.157Z",
"beat": {
"hostname": "4201halwsd00001",
"name": "4201halwsd00001",
@@ -111,6 +111,36 @@ func TestValidJSONDepthTwo(t *testing.T) { | |||
|
|||
} | |||
|
|||
func TestTargetOption(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding a test case. 👍
Hi @maddin2016, we have found your signature in our records, but it seems like you have signed with a different e-mail than the one used in yout Git commit. Can you please add both of these e-mails into your Github profile (they can be hidden), so we can match your e-mails to your Github profile? |
54cc2ce
to
7568437
Compare
@@ -75,7 +77,12 @@ func (f decodeJSONFields) Run(event common.MapStr) (common.MapStr, error) { | |||
continue | |||
} | |||
|
|||
_, err = event.Put(field, output) | |||
if f.target != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There will be three cases to handle here. When *f.target == ""
you can't use event.Put
, instead you need to add the fields from output
to event
. Looking at the json decoding in Filebeat, the default behavior is to not overwrite keys unless overwrite_key
is set to true. So I would follow this model to be consistent.
01748de
to
45ecddb
Compare
@andrewkroh, how should we check if |
be86754
to
e5b4813
Compare
…arget-decode-json-field
Yeah, |
if len(*f.target) > 0 { | ||
_, err = event.Put(*f.target, output) | ||
} else { | ||
switch t := output.(type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic looks good. But now the complexity of the Run
method is a bit high with all of the nested if/for/switch statements. WDYT about breaking this into smaller functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of adding a global helper function for that switch part. Because now we duplicate code. For example, helpers.jsonhelper.OverwriteJsonKeys
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that makes sense if there is duplication. 👍
Hi @andrewkroh, do you have the chance to read my last commit. My idea was to create helper libraries under libbeat. For this case e.g function OverwriteJsonKeys(event common.Mapstr, keys map[string]interface{})
{
...
} |
@maddin2016 Thanks a lot! I'm merging this now so that it can be included in the next release. Please open a new PR for the refactoring work. |
Fixes #3134
This change add the output from decode_json_field to
json
field instead ofmessage
.