Length of an array in logstash

I am trying to find length of an array for a field using ruby filter. But, I'm getting below error even though nul case is handled. Please let me know if there is any error in the filter.
Thanks in advance

filter {
ruby {
code => "event['failedattemptcount'] = event['failedattempttimes'].length if event['failedattempttimes'] != 'nil'"
}
}

error:

Ruby exception occurred: undefined method `length' for nil:NilClass {:level=>:eror}

"tags" => [
[0] "_rubyexception"
]

You're checking if the failedattempttimes field is equal to the string "nil". Do this instead:

code => "event['failedattemptcount'] = event['failedattempttimes'].length unless event['failedattempttimes'].nil?"
1 Like