0

I have started writing my own Logstash-filter, based on the example filter provided on Github:

https://github.com/logstash-plugins/logstash-filter-example

My new filter reads from a jar file called Classficiation.jar. I would like to take the values coming from text and based on that, classify these texts. This will require creating a new field and adding the classification in it.

The operation should result in the following:

INPUT

{"text" : "This is a happy thought :)"}

OUTPUT

{"text" : "This is a happy thought :)", "classification" : "Positive"}

However......I'm not quiet sure how I should create this new field. As you can see, the class takes one parameter, which is text.

I would really appreciate guidance on how I can create the new field for the output from this logstash filter.

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

import 'classficiation.Classficiation'

class LogStash::Filters::Classify < LogStash::Filters::Base

  # This is how to configure this filter from Logstash config.
  #
  # filter {
  #   classify {
  #   }
  # }
  #
  config_name "classify"


  t = Java::classficiation::Classficiation.new
  result = t.Classify(:text)
  # This should return a classification for the text, either positive
  # or negative

  public
  def register
  end

  public
  def filter(event)    
    ..........

    filter_matched(event)
  end
end

Thank you.


UPDATE

I have followed the instructions provided to me by @hurb, and edited my file so that it looks like this:

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

import 'classficiation.Classficiation'

class LogStash::Filters::Classify < LogStash::Filters::Base

  config_name "classify"

  t = Java::classficiation::Classficiation.new

  public
  def register
  end

  public
  def filter(event)    
    event["result"] = t.Classify(event["text"])
    filter_matched(event)
  end
end

However, this started giving me the error:

NameError: undefined local variable or method 'Classify' for #<LogStash::Filters::Classify:0x44850d11>

And when I try to include t = Java::classficiation::Classficiation.new in the filter, it gives me the error:

dynamic constant assignment

Why is that???

hello_its_me
  • 743
  • 2
  • 19
  • 52

1 Answers1

0

You can access all event fields inside the filter(event) function at the end of your class. Fields can be accessed using the event parameter: event["message"]. So, something like event["result"] = t.Classify(event["text"]) might be what you are looking for (it creates a new field result.

However, I think there are some other issues with your code. Please double check your filter(event) function. There seems to be an unnecessary end. I'm not sure about the java import but I think it should be java_import 'classficiation.Classficiation' instead of import.

Start like this:

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

java_import 'classficiation.Classficiation'

class LogStash::Filters::Classify < LogStash::Filters::Base

  config_name "classify"

  public
  def register
  end

  public
  def filter(event)    
     t = Java::classficiation::Classficiation.new
     event["result"] = t.Classify(event["text"])
     filter_matched(event)
  end

end

Let me know if you need further assistance.


UPDATE

In order to debug your jruby code I would recommend to test its functionality outside logstash. Something like this should work:

#!/usr/bin/env jruby

require 'java'
java_import 'classficiation.Classficiation'
t = Classficiation.new
result = t.Classify("foobar")
puts result

You could try to execute a script like this and see if it gives the desired output. Afterwards try to migrate it to a logstash filter.

hurb
  • 2,177
  • 3
  • 18
  • 32
  • hmm okay, so I did that, and then wanted to test the filter with Logstash. However, when I installed the gem as filter there, it gave the error `filter not installed` even though listing the installed filters show this particular filter installed.. Do you have any idea why I might be getting that error? Thanks – hello_its_me Nov 10 '15 at 07:13
  • Also, I would like to add more than one field. Event will take more than one field, correct? so it would look something like this: `event["result"] = t.Classify(event["text"]) \n event["result2"] = a.Classify2(event["text"])` – hello_its_me Nov 10 '15 at 08:21
  • Hm, which version of logstash are you using? Have you tried to install other filters? E.g. `logstash-filter-example`? Which command do you use to install the filter? It should be somehting like `logstash/bin/plugin install logstash-filter-abc.gem` To answer your second question: Yes, multiple fields are perfectly fine. – hurb Nov 11 '15 at 18:34
  • Never mind the previous issues, I was able to fix them all. And I was able to include my custom filter in the logstash .conf file, and start logstash with no errors. However, when I input something, it gives me the error `NameError: undefined local variable or method 'Classify' for #` – hello_its_me Nov 12 '15 at 09:29
  • Please note that I had to move `t = Java::classficiation::Classficiation.new` outside `filter` because it gave me the error `dynamic constant assignment` – hello_its_me Nov 12 '15 at 09:59
  • I've updated my question with the new error, could you take a look please? – hello_its_me Nov 12 '15 at 10:32
  • thank you! I have already done that, and confirmed that outside of Logstash, my code does work. It's when I try to use Logstash's `filter` that issues start to rise.. I must be declaring the variables wrong, or not calling them inside of `filter` correctly. – hello_its_me Nov 12 '15 at 12:27
  • I have opened a new question, more detailed that describes my issue better. Could you kindly take a look? The solution to the original question here has been answered. (how to create a field in logstash filters) http://stackoverflow.com/questions/33671160/writing-a-logstash-gem-gives-an-error-undefined-local-variable-or-method – hello_its_me Nov 12 '15 at 12:34