Ruby exception occurred: undefined method `+' for nil:NilClass

(Pierre-Vincent Ledoux) #1

I have the following ruby filter:

mutate {
  convert => ["body_size", "integer"]
  convert => ["header_size", "integer"]
# Sum of body and header size
ruby {
  code => "event.set('bytes', (event.get('body_size') + event.get('header_size')))"

Where body_size or header_size can be 0, but in those case, I get the following error:

Ruby exception occurred: undefined method+' for nil:NilClass`

How can I avoid that error and ensure that I get the sum of the field, even if there are 0?


The problem isn't that there is 0 values, it's that some document that go through this filter doesn't have the 'body_size' or header_size field.

Either filter before to be sure that this field exists:

if [body_size]{

Or you do it in ruby and all doc that go through this will have the field bytes. For ruby, you can do this:

code => "
  sum = ( (event.get('body_size') != nil)?event.get('body_size') :0)+ ( (event.get('header_size') != nil)?event.get('body_size') :0)
  event.set('bytes', sum)

(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.