Harim_Tong  
                
                  
                    November 16, 2017, 12:07pm
                   
                  1 
               
             
            
              I don't want to write too much Ruby code  in filter(Ruby filter) ,because it's hard to maintenance, can logstash filter call user defined function?foo(x)  in the file: my.rb  as below:
def foo(x)  
	#business code here
end
How to call this function in logstash filter?
             
            
              
            
           
          
            
              
                guyboertje  
              
                  
                    November 27, 2017,  3:55pm
                   
                  3 
               
             
            
              The latest version of the ruby filter includes support for loading the ruby code from a file (v3.1.1, updated in LS 6.0.0)
The file needs to be in a certain format.
  
  
    
config_name "ruby" 
 
# Any code to execute at logstash startup-time 
config :init, :validate => :string 
 
# The code to execute for every event. 
# You will have an `event` variable available that is the event itself. See the <<event-api,Event API>> for more information. 
config :code, :validate => :string 
 
# Path to the script 
config :path, :validate => :path 
 
# Parameters for this specific script 
config :script_params, :type => :hash, :default => {} 
 
# Tag to add to events that cause an exception in the script filter 
config :tag_on_exception, :type => :string, :default => "_rubyexception" 
 
def initialize(*params) 
  super(*params) 
  if @path # run tests on the ruby file script 
  
  
    
    
  
  
 
  
  
    
# Disables mutex around the `filter` function
# Only use this if you know your code is threadsafe!
def concurrency
  :shared
end
def register(params)
  @field = params['field']
  @multiplier = params['multiplier']
end
def filter(event)
  event.set(@field, event.get(@field) * @multiplier)
  # Filter blocks must return any events that are to be passed on
  # return a nil or [] here if all events are to be cancelled
  # You can even return one or more brand new events here!
  [event]
end
test "standard flow" do
show original 
   
  
    
    
  
  
 
You can define as many methods as you need for better code execution and there is a micro test harness too.
             
            
              
            
           
          
            
              
                system  
              
                  
                    December 25, 2017,  3:55pm
                   
                  4 
               
             
            
              This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.