Class: LogStash::Filters::Mutate

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/mutate.rb

Overview

The mutate filter allows you to perform general mutations on fields. You can rename, remove, replace, and modify fields in your events.

Constant Summary collapse

TRUE_REGEX =
(/^(true|t|yes|y|1)$/i).freeze
FALSE_REGEX =
(/^(false|f|no|n|0)$/i).freeze
CONVERT_PREFIX =
"convert_".freeze

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/logstash/filters/mutate.rb', line 202

def filter(event)
  rename(event) if @rename
  update(event) if @update
  replace(event) if @replace
  convert(event) if @convert
  gsub(event) if @gsub
  uppercase(event) if @uppercase
  lowercase(event) if @lowercase
  strip(event) if @strip
  remove(event) if @remove
  split(event) if @split
  join(event) if @join
  merge(event) if @merge

  filter_matched(event)
end

#registerObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/logstash/filters/mutate.rb', line 169

def register
  valid_conversions = %w(string integer float boolean)
  # TODO(sissel): Validate conversion requests if provided.
  @convert.nil? or @convert.each do |field, type|
    if !valid_conversions.include?(type)
      raise LogStash::ConfigurationError, I18n.t(
        "logstash.agent.configuration.invalid_plugin_register",
        :plugin => "filter",
        :type => "mutate",
        :error => "Invalid conversion type '#{type}', expected one of '#{valid_conversions.join(',')}'"
      )
    end
  end

  @gsub_parsed = []
  @gsub.nil? or @gsub.each_slice(3) do |field, needle, replacement|
    if [field, needle, replacement].any? {|n| n.nil?}
      raise LogStash::ConfigurationError, I18n.t(
        "logstash.agent.configuration.invalid_plugin_register",
        :plugin => "filter",
        :type => "mutate",
        :error => "Invalid gsub configuration #{[field, needle, replacement]}. gsub requires 3 non-nil elements per config entry"
      )
    end

    @gsub_parsed << {
      :field        => field,
      :needle       => (needle.index("%{").nil?? Regexp.new(needle): needle),
      :replacement  => replacement
    }
  end
end