Class: LogStash::Filters::Ruby

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/ruby.rb

Overview

Execute ruby code.

For example, to cancel 90% of events, you can do this:

source,ruby

filter {

ruby {
  # Cancel 90% of events
  code => "event.cancel if rand <= 0.90"
}

}

If you need to create additional events, it cannot be done as in other filters where you would use ‘yield`, you must use a specific syntax `new_event_block.call(event)` like in this example duplicating the input event

source,ruby

filter {

ruby {
  code => "new_event_block.call(event.clone)"
}

}

Instance Method Summary collapse

Instance Method Details

#filter(event, &block) ⇒ Object

def register



41
42
43
44
45
46
47
48
49
# File 'lib/logstash/filters/ruby.rb', line 41

def filter(event,&block)
  begin
    @codeblock.call(event,&block)
    filter_matched(event)
  rescue Exception => e
    @logger.error("Ruby exception occurred: #{e}")
    event.tag("_rubyexception")
  end
end

#registerObject



35
36
37
38
39
# File 'lib/logstash/filters/ruby.rb', line 35

def register
  # TODO(sissel): Compile the ruby code
  eval(@init, binding, "(ruby filter init)") if @init
  eval("@codeblock = lambda { |event, &new_event_block| #{@code} }", binding, "(ruby filter code)")
end