class Fluent::RecordTransformerFilter::PlaceholderExpander

Attributes

log[R]
placeholders[R]

Public Class Methods

new(params) click to toggle source
# File lib/fluent/plugin/filter_record_transformer.rb, line 167
def initialize(params)
  @log = params[:log]
  @auto_typecast = params[:auto_typecast]
end

Public Instance Methods

expand(str, force_stringify=false) click to toggle source
# File lib/fluent/plugin/filter_record_transformer.rb, line 191
def expand(str, force_stringify=false)
  if @auto_typecast and !force_stringify
    single_placeholder_matched = str.match(/\A(\${[^}]+}|__[A-Z_]+__)\z/)
    if single_placeholder_matched
      log_unknown_placeholder($1)
      return @placeholders[single_placeholder_matched[1]]
    end
  end
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
    log_unknown_placeholder($1)
    @placeholders[$1]
  }
end
prepare_placeholders(time, record, opts) click to toggle source
# File lib/fluent/plugin/filter_record_transformer.rb, line 172
def prepare_placeholders(time, record, opts)
  placeholders = { '${time}' => Time.at(time).to_s }
  record.each {|key, value| placeholders.store("${#{key}}", value) }

  opts.each do |key, value|
    if value.kind_of?(Array) # tag_parts, etc
      size = value.size
      value.each_with_index { |v, idx|
        placeholders.store("${#{key}[#{idx}]}", v)
        placeholders.store("${#{key}[#{idx-size}]}", v) # support [-1]
      }
    else # string, interger, float, and others?
      placeholders.store("${#{key}}", value)
    end
  end

  @placeholders = placeholders
end

Private Instance Methods

log_unknown_placeholder(placeholder) click to toggle source
# File lib/fluent/plugin/filter_record_transformer.rb, line 206
def log_unknown_placeholder(placeholder)
  unless @placeholders.include?(placeholder)
    log.warn "unknown placeholder `#{placeholder}` found"
  end
end