Module: LogStash::Config::Mixin

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)
ENV_PLACEHOLDER_REGEX =
/\$\{(?<name>\w+)(\:(?<default>[^}]*))?\}/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#original_paramsObject

Returns the value of attribute original_params.



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

def original_params
  @original_params
end

Class Method Details

.included(base) ⇒ Object

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



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

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



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
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/logstash/config/mixin.rb', line 50

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

  # warn about deprecated variable use
  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

  # 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|
    if (value.is_a?(Hash))
      value.each do |valueHashKey, valueHashValue|
        value[valueHashKey.to_s] = replace_env_placeholders(valueHashValue)
      end
    else
      if (value.is_a?(Array))
        value.each_index do |valueArrayIndex|
          value[valueArrayIndex] = replace_env_placeholders(value[valueArrayIndex])
        end
      else
        params[name.to_s] = replace_env_placeholders(value)
      end
    end
  end


  if !self.class.validate(params)
    raise LogStash::ConfigurationError,
      I18n.t("logstash.runner.configuration.invalid_plugin_settings")
  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 classed
  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

  # 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

  @config = params
end

#replace_env_placeholders(value) ⇒ Object

Replace all environment variable references in ‘value’ param by environment variable value and return updated value Process following patterns : $VAR, $VAR, $VAR:defaultValue



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/logstash/config/mixin.rb', line 158

def replace_env_placeholders(value)
  return value unless value.is_a?(String)

  value.gsub(ENV_PLACEHOLDER_REGEX) do |placeholder|
    # Note: Ruby docs claim[1] Regexp.last_match is thread-local and scoped to
    # the call, so this should be thread-safe.
    #
    # [1] http://ruby-doc.org/core-2.1.1/Regexp.html#method-c-last_match
    name = Regexp.last_match(:name)
    default = Regexp.last_match(:default)

    replacement = ENV.fetch(name, default)
    if replacement.nil?
      raise LogStash::ConfigurationError, "Cannot evaluate `#{placeholder}`. Environment variable `#{name}` is not set and there is no default value given."
    end
    replacement
  end
end