Accessing nested fields in ruby filter for math


I am having issues with this bit of code where I am trying to add 3 nested fields together and place them in a new nested field.

here is my bit of ruby code

ruby {
	code => "event.set('[device][disconnect][duration]', ( (event.get('[device][disconnect][hour]').to_f * 3600) + (event.get('[device][disconnect][minute]').to_f *60) + event.get('[device][disconnect][second').to_f))"

and this is the error that it is giving me

logstash[8322]: [2020-11-17T12:26:40,902][ERROR][logstash.filters.ruby  Ruby exception occurred: wrong number of arguments calling `to_f` (given 1, expected 0)

any help would be great!


You are missing a closing ] after 'second'. You also need to change *60 to * 60 with a space.

sorry for some reason that was not copied correctly. I may have deleted it by mistake. the ] is there in the config file

Oh, you replied whilst I was editing my post... Ruby is not parsing *60 as mulitply by 60, but as an argument to .to_f. Add a space to make it * 60 and it will get parsed the way you want.

thank you! I was so close...

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