class Fluent::HttpRecordModifier::PlaceholderExpander

Attributes

log[R]
placeholders[R]

Public Class Methods

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

Public Instance Methods

crawl_placeholder(value, placeholder, before, limit = 50) click to toggle source
# File lib/fluent/plugin/filter_http_record_modifier.rb, line 290
def crawl_placeholder (value, placeholder, before, limit = 50)
  if limit >= 0
    if value.kind_of?(Hash) 
      value.each {|key, v| crawl_placeholder(v, placeholder, "#{before}.#{key}", limit - 1)}
    elsif value.kind_of?(Array) # tag_parts, etc
      size = value.size
      value.each_with_index { |v, idx|
        crawl_placeholder(v, placeholder, "#{before}[#{idx}]", limit - 1)
        crawl_placeholder(v, placeholder, "#{before}[#{idx-size}]", limit - 1) #suport [-1]
      }
    end
  end
  # string, interger, float, and others?
  placeholder.store("${#{before}}", value)
end
expand(str, force_stringify=false) click to toggle source
# File lib/fluent/plugin/filter_http_record_modifier.rb, line 306
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_http_record_modifier.rb, line 272
def prepare_placeholders(time, record, opts)
  placeholders = { '${time}' => Time.at(time).to_s }
  record.each {|key, value| crawl_placeholder(value, placeholders, "#{key}")}
  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_http_record_modifier.rb, line 321
def log_unknown_placeholder(placeholder)
  unless @placeholders.include?(placeholder)
    log.warn "unknown placeholder `#{placeholder}` found"
  end
end