class Fluent::RecordModifierOutput

Constants

BUILTIN_CONFIGURATIONS
HOSTNAME_PLACEHOLDERS

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_record_modifier.rb, line 36
def configure(conf)
  super

  @map = {}
  conf.each_pair { |k, v|
    unless BUILTIN_CONFIGURATIONS.include?(k)
      check_config_placeholders(k, v)
      conf.has_key?(k)
      @map[k] = v
    end
  }

  if @char_encoding
    from, to = @char_encoding.split(':', 2)
    @from_enc = Encoding.find(from)
    @to_enc = Encoding.find(to) if to

    m = if @to_enc
          method(:convert_encoding)
        else
          method(:set_encoding)
        end

    (class << self; self; end).module_eval do
      define_method(:change_encoding, m)
    end
  end

  if @remove_keys and @whitelist_keys
    raise Fluent::ConfigError, "remove_keys and whitelist_keys are exclusive with each other."
  elsif @remove_keys
    @remove_keys = @remove_keys.split(',').map(&:strip)
  elsif @whitelist_keys
    @whitelist_keys = @whitelist_keys.split(',').map(&:strip)
  end
end
emit(tag, es, chain) click to toggle source
# File lib/fluent/plugin/out_record_modifier.rb, line 73
def emit(tag, es, chain)
  stream = MultiEventStream.new
  es.each { |time, record|
    filter_record(tag, time, record)
    stream.add(time, modify_record(record))
  }
  Fluent::Engine.emit_stream(@tag, stream)

  chain.next
end

Private Instance Methods

check_config_placeholders(k, v) click to toggle source
# File lib/fluent/plugin/out_record_modifier.rb, line 88
def check_config_placeholders(k, v)
  HOSTNAME_PLACEHOLDERS.each { |ph|
    if v.include?(ph)
      raise ConfigError, %Q#{ph} placeholder in #{k} is removed. Use "\#{Socket.gethostname}" instead.!
    end
  }
end
convert_encoding(record) click to toggle source
# File lib/fluent/plugin/out_record_modifier.rb, line 125
def convert_encoding(record)
  record.each_pair { |k, v|
    if v.is_a?(String)
      v.force_encoding(@from_enc) if v.encoding == Encoding::BINARY
      v.encode!(@to_enc, @from_enc, :invalid => :replace, :undef => :replace)
    end
  }
end
modify_record(record) click to toggle source
# File lib/fluent/plugin/out_record_modifier.rb, line 96
def modify_record(record)
  @map.each_pair { |k, v|
    record[k] = v
  }

  if @remove_keys
    @remove_keys.each { |v|
      record.delete(v)
    }
  elsif @whitelist_keys
    modified = {}
    record.each do |k, v|
      modified[k] = v if @whitelist_keys.include?(k)
    end
    record = modified
  end

  record = change_encoding(record) if @char_encoding
  record
end
set_encoding(record) click to toggle source
# File lib/fluent/plugin/out_record_modifier.rb, line 117
def set_encoding(record)
  record.each_pair { |k, v|
    if v.is_a?(String)
      v.force_encoding(@from_enc)
    end
  }
end