Ruby into file

Hello,

I read the documentation about the ruby script and I did not correctly understand the conversion of the ruby script into a file.

If I have a converted ruby script into a file, do I have to rewrite the script into classes?

For example, I wanted to put this ruby script in a file, but it didn't work there at all. When I have this in filter section, it works:

ruby {
 code => "
  event.set('[event][duration_seconds]', event.get('[event][duration]').to_f / 1000)
    "} 

I put it into the file. In the filter was:

ruby {
path => "{{logstash.scripts_path}}/count.rb"
}

The content of the file was:

  event.set('[event][duration_seconds]', event.get('[event][duration]').to_f / 1000)

Why it didn´t work?

Thank you

Regards,
Catherine

To convert an inline script to a file you need to define two functions. Try creating a file containing

def register (params)
end

def filter (event)
    event.set("[event][duration_seconds]", event.get("[event][duration]").to_f / 1000)
    return [event]
end

I understand. Thank you so much! :slight_smile:
Is possible to add also ruby id? I mean: id => "X1_RUBY"

ruby {
id => "X1_RUBY"
code => "
event.set('[event][duration_seconds]', event.get('[event][duration]').to_f / 1000)
"}

Yes, id is a common option that all filters support.

Thank you so much. It works all perfectly. :slight_smile:

Regards,
Catherine

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