logstash icon indicating copy to clipboard operation
logstash copied to clipboard

"Best practice" for testing whether boolean field exists is broken

Open TheVastyDeep opened this issue 2 years ago • 3 comments

PR 12899 modified the best-practice doc to add a way to test whether a boolean field exists by using mutate+add_field to add a static value to [@metadata] and then use mutate+copy to conditinally overwrite. Nice trick! Unfortunately it is broken. Filter decoration (add_field etc) happens after the filter function executes. So the copy is done before the add_field. It has to be split into two mutate filters.

TheVastyDeep avatar Aug 04 '22 20:08 TheVastyDeep

Hi @TheVastyDeep, I tried understanding the issue, are you suggesting the following:

filter {
  mutate {
    # we use a "temporal" field with a predefined arbitrary known value that
    # lives only in filtering stage.
    add_field => { "[@metadata][test_field_check]" => "a null value" }
  }

filter {
  mutate {
    # we copy the field of interest into that temporal field.
    # If the field doesn't exist, copy is not executed.
    copy => { "test_field" => "[@metadata][test_field_check]" }
  }

If yes, I can make the changes

rneha725 avatar Oct 18 '22 10:10 rneha725

Yes, that is exactly the change required.

TheVastyDeep avatar Oct 18 '22 12:10 TheVastyDeep

@karenzone Any comments?

TheVastyDeep avatar Dec 11 '23 15:12 TheVastyDeep