New logstash output plugin not getting config value correctly

I am attempting to create a new output plugin. The conf is pretty simple

input {
    stdin{}
}

filter {
  mutate {
    add_field => {
      "[some_new_field]" => "some_new_field_value"
    }
  }
}

output {

  mynewplugin{
    value_to_plugin => "%{[some_new_field]}"
  }
 
  stdout { codec => rubydebug { metadata => true }}
}

Then in my plugin I simply have:

# encoding: utf-8
require "logstash/outputs/base"
require "logstash/namespace"

class LogStash::Outputs::MyNewPlugin < LogStash::Outputs::Base

  config_name "mynewplugin"
  default :codec, 'plain'

  # Replace the message with this value.
  config :value_to_plugin, :validate => :string, :default => "default_value"
  
  concurrency :single

  public
  def register
    # Add instance variables
  end # def register

  public
  def receive(event)

    File.open("/tmp/log.txt", "w") { |f| f.write @value_to_plugin }

    return "Event received"

  end # def filter

end # class LogStash::Outputs::MyNewPlugin

The plugin is getting called correctly but the string written to /tmp/log.txt is:

%{[some_new_field]}

but I expect it to be:

some_new_field_value

I have checked many other plugins as this is a pretty common operation, but can not find my mistake.

I believe you would need to call event.sprintf to make that transformation.

Within the *.rb file? I will give that a try.

Ok, that did indeed get me over the hump:

interp_value = event.sprintf(@value_to_plugin)

The resulting interp_value is what I would expect it to be. Thanks for quick feedback.

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