Unexpected error in ruby code in logstash

Hi,
An unexpected error in occuring for the following ruby code in logstash filter.

SyntaxError: (ruby filter code):5: syntax error, unexpected tRCURLY
** }**

Following is the ruby code:

ruby {
code => ' allscores = event["score"]
if allscores.length != '1'
t = allscores.inject(:+)
event["total_score"] = t
'
add_tag => [sum_score]
}

The error is as follows:

SyntaxError: (ruby filter code):5: syntax error, unexpected tRCURLY
}
^
eval at org/jruby/RubyKernel.java:1079
register at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-filter-ruby-2.0.5/lib/logstash/filters/ruby.rb:29
start_workers at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/pipeline.rb:174
each at org/jruby/RubyArray.java:1613
start_workers at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/pipeline.rb:174
run at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/pipeline.rb:126
execute at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/agent.rb:210
run at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/runner.rb:90
call at org/jruby/RubyProc.java:281
run at /opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-core-2.2.4-java/lib/logstash/runner.rb:95
call at org/jruby/RubyProc.java:281
initialize at /opt/logstash/vendor/bundle/jruby/1.9/gems/stud-0.0.22/lib/stud/task.rb:24

This error does not occur if I use the following code:

ruby {
code => ' allscores = event["score"]

                 t = allscores.inject(:+)
                 event["total_score"] = t
            '
   add_tag => [sum_score]
  }

But I get an error

Ruby exception occurred: undefined method `inject' for 5:Fixnum {:level=>:error}

How can these error be mitigated?

If you use single quotes to delimit the Ruby code snippet you can't use single quotes inside the Ruby code. Your second attempt solves that but doesn't work because the score field contains an integer and inject doesn't work on integers. What are you trying to do here?

Hi Magnus,

I am trying to add the elements of the array 'score'. It contains integers and it's length varies dynamically. This error occurs when the length of array is 1 i.e. the score contains only one value. If the length is more than one, the inject function works well and I get the 'total_score' and no error occurs.

If inject doesn't work for integer then I should have got error for 'total_score' .

What could be the reason for this?

This error occurs when the length of array is 1 i.e. the score contains only one value.

No, it occurs when the value isn't an array at all.

$ irb
irb(main):001:0> [5].inject(:+)
=> 5
irb(main):002:0> 5.inject(:+)
NoMethodError: undefined method `inject' for 5:Fixnum
	from (irb):2
	from /usr/bin/irb:12:in `<main>'

If you need to support non-array score values just use a conditional or a ternary expression:

scores = event["score"]
event["total_score"] = scores.is_a?(Array) ? scores.inject(:+) : scores

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