Ruby filter logstash (.length)

Hi,
I would like to fix the length of message
logstash.conf:

filter {
mutate {
      split => {"message" => "|"}
      ruby {
     code => "event.set('number_of_elements', event.get('message').length) "
      }
      add_field => { "DateTime" => "%{[message][0]}" }
      add_field => { "version" => "%{[message][1]}" }
.............................
if (number_of_elements == 36) {
                    mutate {
                      add_field => {"Status Login" => "%{[message][31]}"}
                       ........................
                    }
                  }
}

it gives me an error at ruby filter

ruby ", :backtrace=>["D:/elastic_stack/logstash-7.16.3/logstash-core/lib/logstash/compiler.rb:32:in `compile_imperative'", "org/logstash/execution/AbstractPipelineExt.java:187:in `initialize'", "org/logstash/execution/JavaBasePipelineExt.java:72:in `initialize'", "D:/elastic_stack/logstash-7.16.3/logstash-core/lib/logstash/java_pipeline.rb:47:in `initialize'", "D:/elastic_stack/logstash-7.16.3/logstash-core/lib/logstash/pipeline_action/create.rb:52:in `execute'", "D:/elastic_stack/logstash-7.16.3/logstash-core/lib/logstash/agent.rb:383:in `block in converge_state'"]}
[2022-05-16T14:08:33,035][INFO ][logstash.runner          ] Logstash shut down.
[2022-05-16T14:08:33,047][FATAL][org.logstash.Logstash    ] Logstash stopped processing because of an error: (SystemExit) exit
org.jruby.exceptions.SystemExit: (SystemExit) exit
        at org.jruby.RubyKernel.exit(org/jruby/RubyKernel.java:747) ~[jruby-complete-9.2.20.1.jar:?]
        at org.jruby.RubyKernel.exit(org/jruby/RubyKernel.java:710) ~[jruby-complete-9.2.20.1.jar:?]
        at D_3a_.elastic_stack.logstash_minus_7_dot_16_dot_3.lib.bootstrap.environment.<main>(D:\elastic_stack\logstash-7.16.3\lib\bootstrap\environment.rb:94) ~[?:?]

Did I do something wrong?
Thanks.

Think your issue was not using mutate when doing a split. This works below.

Conf

input { generator { count => 1 lines => [ '{ "msg": "1|2|3|4|5|6" }' ] codec => json } }
filter {
  mutate {
    split => { "msg" => "|" }
  }
  ruby {
    code => "event.set('number_of_elements', event.get('msg').length) "
  }  
}
output { stdout { codec => "json_lines" } }

Output

"number_of_elements": 6
1 Like

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