Skip to content
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

merge into existing target, new tests and DRYing #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

colinsurprenant
Copy link
Contributor

@colinsurprenant colinsurprenant commented Aug 15, 2018

This fixes issue #43 which is a regression introduced into the kv filter v2.0.3 and above.
Previously if an existing target field existed in the event, the kv fields were merged but the refactor at 2.0.3 changed that and now the target field is always overwritten.

This PR brings back the pre 2.0.3 behaviour and also add a specific test for that. The specs have also been cleaned up a little bit to DRY the plugin instance creation.

Note that this regression has made it through 2 major versions of the plugin but nonetheless I think it makes sense to not loose existing fields in the target if it exists.

@@ -411,8 +411,10 @@ def filter(event)
return if kv.empty?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you fancy, removing this line should satisfy #11 too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. my concern is with BWC but we could make this a major version bump in that respect and have better consistency in the plugin behaviour.

event.set(@target, kv)
@logger.debug? && @logger.debug("Merging into existing target field", :target => @target)
t = event.get(@target)
t = {} unless t.is_a?(Hash)
Copy link

@guyboertje guyboertje Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider that if t is a scalar or array, it just goes away, this becomes a "if target is a hash: merge, otherwise overwrite" operation.

I think this is a very uncommon edge case that can be covered by updating the docs to advise the user, that if they want to keep the value at target, they should stash the value at target and add it back to the new target hash at the key of their choosing. If we support #11 then the "add back" will not fail because target is always a hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was my reasoning: if t is anything else than a hash then drop it. Thanks for mentioning #11 - this is also a legitimate concern indeed. Also looking at the add_field behaviour, it will turn the value into an array if the field exists, so we do try to avoid loosing values.

@guyboertje
Copy link

@colinsurprenant

Have a look at something you did in the csv filter on Dec 3, 2015 - for consistency in writing fields and values to an event. 😉 😆

@colinsurprenant
Copy link
Contributor Author

@guyboertje good catch about csv. I actually forgot about that. But at the same time it does not really help with our problem here since kv is always about creating a hash into the target.

In csv, all field/value are converted into event.set("[target][field]", value) meaning that if a previous value existed at [target][field] it would be overwritten.

Our problem here is what do we do with

  • an existing non-hash value at [target][field] when setting the result of the kv, for both an empty result and non empty result.
  • non existing value at [target][field] when we have an empty kv result - ie, do we set an empty hash result? (I'd say yes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants