Time difference using ruby

I am trying to get a time difference between two time field using ruby filter but looks like one of the timefiled is empty or nill and that is giving me error
ERROR] ruby - Ruby exception occurred: undefined method `-' for nil:NilClass

I am already converting execution_end to date format above this code.
I don't think database has this field empty.

if [next_exec_start] == "nil" or [next_exec_start] == "" or [execution_end] == "" or [execution_end] == "nil" {
mutate { update => { "next_exec_start" => 0 } }
mutate { add_field => { "next_exec_min" => 0 } }
}
else {
date { match => ["next_exec_start", "dd-MMM-yy HH:mm"]
target => "next_exec_start" }
ruby {
init => "require 'time'"
code => "event.set('next_exec_min',(event.get('next_exec_start')-event.get('execution_end'))/60.round(2))"
}
}

What am I doing wrong. I want to skip ruby operation if next_exec_start is not present. but I need that record.

more update, here is the record where I get _rubyexception

{
     tags" => [
        [0] "_rubyexception"
    ],
       "execution_end" => 2019-03-21T16:31:00.000Z,
   "execution_start" => 2019-03-21T14:33:00.000Z,
    "next_exec_start" => nil,
}

Looks like it is failing when next_exec_start is nil but I am trying to replace that with 0 in if statement. looks like I am not comparing something nil might be something else in logstash

ok fixed it myself
nil value is consider nothing in logstash I think
used following and it worked
if ![next_exec_start] { assign 0 }
else { do something }

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