Ruby filter: Ruby exception occurred:undefined local variable or method `map'

I wanna run ruby script for calculating the diference between is my script

input {
elasticsearch {
hosts => ""
index => "test_index"
query => '{"sort": [{"logTimestamp": {"order": "asc"}}]}'
filter {
ruby {
init => "@@map = {}"
code => "
map['timeDifference']= (Time.parse(event.get('logTimestamp')).to_f - Time.parse(map['previousTime']).to_f).round(4);
event.set('timeDifference', map['timeDifference']);
map['previousTime'] = event.get('logTimestamp');
event.set('previousTime', map['previousTime']);
mutate {
remove_field => ["@version","@timestamp"]

output {
elasticsearch {
document_id => "%{logTimestamp}"
document_type => "test_index"
index => "test_index"
codec => "json"
hosts => [""]

But when I am trying to execute script its giving me error as
undefined local variable or method `map' for #LogStash::Filters::Ruby:0x3b42c4a8

Also I wanna hold the logTimestamp of first document and calculate the difference when second record is received and finally store it in second record.How can I calculate the difference?

The name of the variable you initialize via init is @@map so that's how you need to reference it in the code block.

thanks for heads up @magnusbaeck How can I hold the time of first record to calculate time difference?

Isn't that what you're doing now?

It's working but every time I am getting same time as previous time

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