Class: LogStash::Filters::Mutate

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

Overview

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

TODO(sissel): Support regexp replacements like String#gsub ?

Constant Summary

Constants inherited from Base

Base::RESERVED

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#execute, #initialize, #threadsafe?

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Filters::Base

Instance Method Details

#filter(event) ⇒ Object



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

def filter(event)
  return unless 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



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 175

def register
  valid_conversions = %w(string integer float)
  # TODO(sissel): Validate conversion requests if provided.
  @convert.nil? or @convert.each do |field, type|
    if !valid_conversions.include?(type)
      @logger.error("Invalid conversion type",
                    "type" => type, "expected one of" => valid_types)
      # TODO(sissel): It's 2011, man, let's actually make like.. a proper
      # 'configuration broken' exception
      raise "Bad configuration, aborting."
    end
  end # @convert.each
  
  @gsub_parsed = []
  @gsub.nil? or @gsub.each_slice(3) do |field, needle, replacement|
    if [field, needle, replacement].any? {|n| n.nil?}
      @logger.error("Invalid gsub configuration. gsub has to define 3 elements per config entry", :field => field, :needle => needle, :replacement => replacement)
      raise "Bad configuration, aborting."
    end
    @gsub_parsed << {
      :field        => field,
      :needle       => Regexp.new(needle),
      :replacement  => replacement
    }
  end
end