Writing a Logstash GEM gives an error: undefined l

2019-08-11 12:16发布

问题:

Trying to build a gem to use it for Logstash as a filter. I'm using classes and methods from a .jar file.

The following is my Jruby code so far:

# encoding: utf-8
require "logstash/filters/base"
require "logstash/namespace"
require 'java'
require 'Processing.jar'

java_import 'process.Process'

class LogStash::Filters::Process < LogStash::Filters::Base

  config_name "process"

  StringPath = "/path/to/file/"
  ML = JavaUtilities.get_proxy_class('processing.Processing')
  @ML = ML.new(StringPath)

  public
  def register
  end

  public
  def filter(event)    
     event["result"] = @ML.Process("any string.")
     filter_matched(event)
  end

end

However, it gave me the following error:

    ←[31mException in filterworker, the pipeline stopped processing new events, plea
se check your filter configuration and restart Logstash. {"exception"=>#<NoMetho
dError: undefined method 'Process' for nil:NilClass>, "backtrace"=>[
"/path/to/logstash-2.0.0/vendor/local_gems
/dda91dc0/logstash-filter-Process-0.5.0/lib/logstash/filters/processing.rb:54:in 'filter'", "/path/to/log
stash-2.0.0/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0-java/lib/logstash/f
ilters/base.rb:152:in 'multi_filter'", "org/jruby/RubyArray.java:1613:in 'each'"
, "/path/to/logstash-2.0.0/vendor/bundle/j
ruby/1.9/gems/logstash-core-2.0.0-java/lib/logstash/filters/base.rb:149:in 'mult
i_filter'", "(eval):233:in 'filter_func'", "/path/to/logstash-2.0.0/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0-java/
lib/logstash/pipeline.rb:219:in 'filterworker'", "path/to/log/logstash-2.0.0/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0
-java/lib/logstash/pipeline.rb:154:in 'start_filters'"], :level=>:error}←[0m
NoMethodError: undefined method 'Process' for nil:NilClass
         filter at /path/to/logstash-2.0.0
/vendor/local_gems/dda91dc0/logstash-filter-Process-0.5.0/lib/logstash
/filters/Process.rb:54
   multi_filter at /path/to/logstash-2.0.0
/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0-java/lib/logstash/filters/base
.rb:152
           each at org/jruby/RubyArray.java:1613
   multi_filter at /path/to/logstash-2.0.0
/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0-java/lib/logstash/filters/base
.rb:149
    filter_func at (eval):233
   filterworker at /path/to/logstash-2.0.0
/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0-java/lib/logstash/pipeline.rb:
219
  start_filters at /path/to/logstash-2.0.0
/vendor/bundle/jruby/1.9/gems/logstash-core-2.0.0-java/lib/logstash/pipeline.rb:
154

I tried the following:

  .
  .
  .
  public
  def filter(event)
     StringPath = "/path/to/file/"
     ML = JavaUtilities.get_proxy_class('processing.Processing')
     @ML = ML.new(StringPath)    
     event["result"] = @ML.Process("any string.")
     filter_matched(event)
  end   
end

But it gave me the error:

SyntaxError: dynamic constant assignment error. 

I tried this in a normal jruby file and everything worked with me. Using Logstash's filter however is what seems to be giving me an issue here.

回答1:

It seems you are struggling with local and instance variables in Ruby. Please see this blog post for more information.

When you call event["result"] = ML.Process(event["text"]) ruby tries to find a local variable named ML which doesn't exist. Therefore you get undefined local variable or method ML.Process(event["text"]). So, messing around with your variables might solve your problem.

@ML is an instance variable and is available in all methods calling it using @ML or self.ML. Please try this:

public
def filter(event)
   event["result"] = @ML.Process(event["text"])
   filter_matched(event)
end 

Things I stumbled upon:

  • Where do MLVectorPath and MLStopWordPath come from?
  • What is StringPath? It is assigned but never used.
  • Do you have a field text? Otherwise change event["text"] into event["message"].

However, I know neither your Java code nor your logstash configuration so these things may be okay. Please let me know if this helps.