Reputation: 187
Was trying to execute below code in logstash but it keeps throwing an exception.
if [received_at] and [sent_at] {
ruby {
init => "require 'time'"
code => "
received_by_finacle = (Time.parse(event.get('received_at').to_f)*1000).round(2);
sent_out_by_finacle = (Time.parse(event.get('sent_at').to_f)*1000).round(2);
event.set('delta', (sent_out_by_finacle - received_by_finacle)).to_s;
event.set('epoch_received_at_in_milliseconds', received_by_finacle);
event.set('epoch_sent_at_in_milliseconds', sent_out_by_finacle);
"
add_tag => [ "calculated_time_difference" ]
}
}
the error returned is
[2018-10-10T22:01:05,631][ERROR][logstash.filters.ruby ] Ruby exception occurred: allocator undefined for Float
[2018-10-10T22:01:05,640][ERROR][logstash.filters.ruby ] Ruby exception occurred: allocator undefined for Float
any ideas why please?
Upvotes: 1
Views: 396
Reputation: 187
Thanks to a logstash community member @Ganesh_Venkataraman, i was able to figure the solution out. I had to add a "rescue nil" (will read up more on that later). Here's the code that finally worked for me.
if [received_at] and [sent_at] {
ruby {
init => "require 'time'"
code => "
received_by_finacle = (Time.parse(event.get('received_at')).to_f)*1000;
sent_out_by_finacle = (Time.parse(event.get('sent_at')).to_f)*1000;
timetaken = (sent_out_by_finacle - received_by_finacle) rescue nil;
event.set('time_delta',timetaken);
event.set('epoch_received_at_in_milliseconds',received_by_finacle);
event.set('epoch_sent_at_in_milliseconds',sent_out_by_finacle);
"
add_tag => [ "calculated_time_difference" ]
}
}
Thanks.
Upvotes: 1