USe Event API from ruby method

Trying to do tagging inside my own method.

   def jP(str)
      begin
    	parsed = JSON.parse(str)
    	rescue => 
    		event.tag("CATCH")
    		return
      end
    end

But got an error:

Ruby exception occurred: undefined local variable or method `event' for #LogStash::Filters::Ruby:0x4adf7f5

How can I run the event`s method inside of my methods? (for ex. event.set and other?)

Is this in a script file? If so, event is in scope because it is passed to filter(event). If you want it to be in scope in other methods you need to pass it to them.

I run code with ruby filter plugin.
Could you provide some simple examples, please?

Are you passing the code to the ruby filter using the code option or the path option (i.e. a script file).

Oh, sorry!
I pass the code by the code option

OK. event is a local variable in-scope within the code block you pass to the filter. However, it is out of scope for functions defined within that block. This will get you an undefined local error.

        code => '
            def foo
                event.tag("CATCH")
            end
            foo
        '

You have to pass event to the function.

        code => '
            def foo(e)
                e.tag("CATCH")
            end
            foo event
        '
1 Like

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