Ruby script file with dynamic param


#1

hi everyone !

in order to factoring my logstash configuration i wanna replace many similar ruby inline code by a ruby script with dynamic param.

what i got before my ruby code :
[
'key1 => 'value1',
'key2 => 'value2'
]

what i need after :

?key1=value1&key2=value2

this inline ruby work fine :

# construct the cleaned params string for canonical params
# there isn't functions in logstash to implode array so we used "Ruby" code
if [og_url_param_decode_array] {
    ruby {
        code => "
            param_string='?'
            event.get('[og_url_param_decode_array]').each { |k, v|
                param_string+=k+'='+v+'&'
            }
            event.set('og_url_param_decode', param_string.chomp('&'))
        "
    }
}

but when i'm try to do the same thing in a ruby script i can't get input data from the event if it's an array but this work normaly if i try to get a string

see the script bellow

def register(params)
  @input_var_name = params["var_name"]
end

def filter(event)
  param_string='?'
  event.get(@input_var_name).each { |k, v|
      param_string+=k+'='+v+'&'
  }
  event.set('test', param_string)

  return [event]
end

this is how the script is called

ruby {
   path => "/etc/logstash/ruby/create_url_param.rb"
   script_params => { "var_name" => canonical_param_decode_array }
}

if canonical_param_decode_array is an array my logstash pipeline is not working


#2

What error message do you get when this fails? Is your canonical_param_decode_array an array of hashes?

You left the chomp out of the function :slight_smile:


#3

there is no error message, when my script is running nothing come in my elastic with the same comportement as a drop filter pluging

thanks for the chomp :slight_smile:


#4

I found something, it was a mapping problem. i was trying to insert an array into a string field.


(system) #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.