Module: LogStash::Config::Mixin

Includes:
Util::SubstitutionVariables
Included in:
LogStash::Codecs::Base, Filters::Base, Inputs::Base, Outputs::Base, Plugin
Defined in:
lib/logstash/config/mixin.rb

Overview

This module is meant as a mixin to classes wishing to be configurable from config files

The idea is that you can do this:

class Foo < LogStash::Config

# Add config file settings
config "path" => ...
config "tag" => ...

# Add global flags (becomes --foo-bar)
flag "bar" => ...

end

And the config file should let you do:

foo

"path" => ...
"tag" => ...

Defined Under Namespace

Modules: DSL

Constant Summary collapse

PLUGIN_VERSION_1_0_0 =
LogStash::Util::PluginVersion.new(1, 0, 0)
PLUGIN_VERSION_0_9_0 =
LogStash::Util::PluginVersion.new(0, 9, 0)

Constants included from Util::SubstitutionVariables

Util::SubstitutionVariables::SUBSTITUTION_PLACEHOLDER_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::SubstitutionVariables

#deep_replace, #replace_placeholders

Instance Attribute Details

#configObject

Returns the value of attribute config.



37
38
39
# File 'lib/logstash/config/mixin.rb', line 37

def config
  @config
end

#original_paramsObject

Returns the value of attribute original_params.



38
39
40
# File 'lib/logstash/config/mixin.rb', line 38

def original_params
  @original_params
end

Class Method Details

.included(base) ⇒ Object

This method is called when someone does ‘include LogStash::Config’



44
45
46
47
# File 'lib/logstash/config/mixin.rb', line 44

def self.included(base)
  # Add the DSL methods to the 'base' given.
  base.extend(LogStash::Config::Mixin::DSL)
end

Instance Method Details

#config_init(params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/logstash/config/mixin.rb', line 49

def config_init(params)
  # Validation will modify the values inside params if necessary.
  # For example: converting a string to a number, etc.

  # Keep a copy of the original config params so that we can later
  # differentiate between explicit configuration and implicit (default)
  # configuration.
  original_params = params.clone

  # store the plugin type, turns LogStash::Inputs::Base into 'input'
  @plugin_type = self.class.ancestors.find { |a| a.name =~ /::Base$/ }.config_name

  # Set defaults from 'config :foo, :default => somevalue'
  self.class.get_config.each do |name, opts|
    next if params.include?(name.to_s)
    if opts.include?(:default) and (name.is_a?(Symbol) or name.is_a?(String))
      # default values should be cloned if possible
      # cloning prevents
      case opts[:default]
        when FalseClass, TrueClass, NilClass, Numeric
          params[name.to_s] = opts[:default]
        else
          params[name.to_s] = opts[:default].clone
      end
    end

    # Allow plugins to override default values of config settings
    if self.class.default?(name)
      params[name.to_s] = self.class.get_default(name)
    end
  end

  # Resolve environment variables references
  params.each do |name, value|
    params[name.to_s] = deep_replace(value)
  end

  if !self.class.validate(params)
    raise LogStash::ConfigurationError,
      I18n.t("logstash.runner.configuration.invalid_plugin_settings")
  end

  # now that we know the parameters are valid, we can obfuscate the original copy
  # of the parameters before storing them as an instance variable
  self.class.secure_params!(original_params)
  @original_params = original_params

  # warn about deprecated variable use
  original_params.each do |name, value|
    opts = self.class.get_config[name]
    if opts && opts[:deprecated]
      extra = opts[:deprecated].is_a?(String) ? opts[:deprecated] : ""
      extra.gsub!("%PLUGIN%", self.class.config_name)
      self.logger.warn("You are using a deprecated config setting " +
                   "#{name.inspect} set in #{self.class.config_name}. " +
                   "Deprecated settings will continue to work, " +
                   "but are scheduled for removal from logstash " +
                   "in the future. #{extra} If you have any questions " +
                   "about this, please visit the #logstash channel " +
                   "on freenode irc.", :name => name, :plugin => self)

    end

    if opts && opts[:obsolete]
      extra = opts[:obsolete].is_a?(String) ? opts[:obsolete] : ""
      extra.gsub!("%PLUGIN%", self.class.config_name)
      raise LogStash::ConfigurationError,
        I18n.t("logstash.runner.configuration.obsolete", :name => name,
               :plugin => self.class.config_name, :extra => extra)
    end
  end

  # We remove any config options marked as obsolete,
  # no code should be associated to them and their values should not bleed
  # to the plugin context.
  #
  # This need to be done after fetching the options from the parents class
  params.reject! do |name, value|
    opts = self.class.get_config[name]
    opts.include?(:obsolete)
  end

  # set instance variables like '@foo'  for each config value given.
  params.each do |key, value|
    next if key[0, 1] == "@"

    # Set this key as an instance variable only if it doesn't start with an '@'
    self.logger.debug("config #{self.class.name}/@#{key} = #{value.inspect}")
    instance_variable_set("@#{key}", value)
  end

  @config = params
end