Simple Math Functions with Ruby in Logstash 5.3


(Seth S) #1

I'm trying to do simple math calculations, e.g. convert a number to a decimal such as 93 => 0.93.

I've looked around and can't find the proper syntax to do this. Furthermore, I don't get any warnings in the logs, just see Logstash continuously restart (even with logging set to DEBUG).

My code:

    if [utilization_perc_int] {
      ruby {
        code => 'event.set("utilization_percent") = event.get("utilization_perc_int"} / 100'
      }
    }

Can anyone tell me what's wrong with my code syntax here? More importantly, can anyone tell me where to find what Ruby code (and supporting syntactical examples if possible) is supported in Logstash? The docs are pretty thin regarding the 'code' functionality within the Ruby filter.


(Magnus B├Ąck) #2

I find it hard to believe that Logstash goes into a crash loop without any clues in the logs. --debug shouldn't be necessary and might just make it harder to find the interesting parts.

https://www.elastic.co/guide/en/logstash/current/event-api.html documents the event API.

You have two problems in your code:

  • Your use of event.set is wrong. See examples in the docs.
  • You're closing the event.get call with } instead of ).

(Seth S) #3

Thanks @magnusbaeck, I may try to update the docs at some point to reflect math functions. The event-api docs did help me find the proper syntax.


(system) #4

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