class Fluent::RemoveEmptyOutput

Constants

BUILTIN_CONFIGURATIONS

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/out_remove_empty.rb, line 8
def initialize
  super
end

Public Instance Methods

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

  @map = {}
  conf.each_pair { |k, v|
    next if BUILTIN_CONFIGURATIONS.include?(k)
    conf.has_key?(k) # to suppress unread configuration warning
    @map[k] = v
  }

  if @tag.nil?
    raise Fluent::ConfigError, "out_remove_empty: `tag` must be specified"
  end
  
  @placeholder_expander =
  if @enable_ruby
      # require utilities which would be used in ruby placeholders
      require 'pathname'
      require 'uri'
      require 'cgi'
      RubyPlaceholderExpander.new(log)
      else
      PlaceholderExpander.new(log)
  end
  
  @hostname = Socket.gethostname
end
emit(tag, es, chain) click to toggle source
# File lib/fluent/plugin/out_remove_empty.rb, line 49
def emit(tag, es, chain)
  tag_parts = tag.split('.')
  tag_prefix = tag_prefix(tag_parts)
  tag_suffix = tag_suffix(tag_parts)
  placeholders = {
    'tag' => tag,
    'tags' => tag_parts,
    'tag_parts' => tag_parts,
    'tag_prefix' => tag_prefix,
    'tag_suffix' => tag_suffix,
    'hostname' => @hostname,
  }
  last_record = nil
  es.each {|time, record|
    last_record = record # for debug log
    new_tag, new_record = reform(@tag, time, record, placeholders)
    Engine.emit(new_tag, time, new_record)
  }
  chain.next
rescue => e
  log.warn "remove_empty: #{e.class} #{e.message} #{e.backtrace.first}"
  log.debug "remove_empty: tag:#{@tag} map:#{@map} record:#{last_record} placeholders:#{placeholders}"
end

Private Instance Methods

reform(tag, time, record, opts) click to toggle source
# File lib/fluent/plugin/out_remove_empty.rb, line 75
def reform(tag, time, record, opts)
  @placeholder_expander.prepare_placeholders(time, record, opts)
  new_tag = @placeholder_expander.expand(tag)

  new_record = record.dup
  new_record.each_key do |k|
    if new_record[k] == ""
      new_record.delete(k)
    end
  end

  [new_tag, new_record]
end
tag_prefix(tag_parts) click to toggle source
# File lib/fluent/plugin/out_remove_empty.rb, line 89
def tag_prefix(tag_parts)
  return [] if tag_parts.empty?
  tag_prefix = [tag_parts.first]
  1.upto(tag_parts.size-1).each do |i|
    tag_prefix[i] = "#{tag_prefix[i-1]}.#{tag_parts[i]}"
  end
  tag_prefix
end
tag_suffix(tag_parts) click to toggle source
# File lib/fluent/plugin/out_remove_empty.rb, line 98
def tag_suffix(tag_parts)
  return [] if tag_parts.empty?
  rev_tag_parts = tag_parts.reverse
  rev_tag_suffix = [rev_tag_parts.first]
  1.upto(tag_parts.size-1).each do |i|
    rev_tag_suffix[i] = "#{rev_tag_parts[i]}.#{rev_tag_suffix[i-1]}"
  end
  rev_tag_suffix.reverse
end