Class: LogStash::Filters::Javascript

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

Defined Under Namespace

Classes: Script, ScriptError

Instance Method Summary collapse

Constructor Details

#initialize(*params) ⇒ Javascript

Flag for add exception message to tag_on_exception config :tag_with_exception_message, :type => :boolean, :default => false



46
47
48
49
50
51
# File 'lib/logstash/filters/javascript.rb', line 46

def initialize(*params)
  super(*params)
  @script = Script.new(nil, init_parameters, logger)
  @script.js_eval @init if @init
  @js_filter = nil
end

Instance Method Details

#filter(event, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/logstash/filters/javascript.rb', line 84

def filter(event, &block)
  java_event = event.to_java
  begin
    js_return = @script.js_call(@js_filter, java_event)
    filter_matched(event)
  rescue => e
    @logger.error("could not process event due:", error_details(e))
    tag_exception(event, e)
    return event
  end
  event.cancel unless filter_results(java_event, js_return, &block)
end

#registerObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/logstash/filters/javascript.rb', line 53

def register
  @js_filter = nil # TODO: devutils' sample helper causes plugin.register to happen twice

  if @path
    @path.each { |path| @script.js_eval(::File.read(path), path: path) }
    @js_filter = @script.get('filter') # `expecting a `function filter(event) {}`
    if @js_filter.nil?
      raise ScriptError, "script at '#{@path}' does not define a filter(event) function" if @code.nil?
    else
      if @js_filter.is_a?(ScriptObjectMirror)
        unless @js_filter.isFunction
          raise ScriptError, "script at '#{@path}' defines a 'filter' property that isn't a function (got type: #{@js_filter.getClassName})"
        end
      else
        raise ScriptError, "script at '#{@path}' defines a 'filter' property that isn't a function (got value: #{@js_filter.inspect})"
      end
    end
  end

  if @code
    if @js_filter
      msg = "Script(s) provided by 'path' defined a filter function while 'code' => ... is also set, this is ambiguous!"
      @logger.error(msg); raise LogStash::ConfigurationError.new(msg)
    end

    @js_filter = @script.js_eval(@code) { "(function filter(event) {\n#{@code} } )" } # jdk.nashorn.api.scripting.JSObject
  end

  @script.verify
end