class Fluent::Plugin::RecordsMergerOutput::PlaceholderExpander

THIS CLASS MUST BE THREAD-SAFE これを入れることで,${tag}とか使えるようになる.

Attributes

log[R]
placeholders[R]

Public Class Methods

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

Public Instance Methods

expand(str, placeholders, force_stringify = false) click to toggle source

Expand string with placeholders

@param [String] str @param [Boolean] force_stringify the value must be string, used for hash key

# File lib/fluent/plugin/out_records_merger.rb, line 369
def expand(str, placeholders, force_stringify = false)
  if @auto_typecast && !force_stringify
    single_placeholder_matched = str.match(/\A(\${[^}]+}|__[A-Z_]+__)\z/)
    if single_placeholder_matched
      log_if_unknown_placeholder(Regexp.last_match(1), placeholders)
      return placeholders[single_placeholder_matched[1]]
    end
  end
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) do
    log_if_unknown_placeholder(Regexp.last_match(1), placeholders)
    placeholders[Regexp.last_match(1)]
  end
end
prepare_placeholders(placeholder_values) click to toggle source
# File lib/fluent/plugin/out_records_merger.rb, line 343
def prepare_placeholders(placeholder_values)
  placeholders = {}

  placeholder_values.each do |key, value|
    if value.is_a?(Array) # tag_parts, etc
      size = value.size
      value.each_with_index do |v, idx|
        placeholders.store("${#{key}[#{idx}]}", v)
        placeholders.store("${#{key}[#{idx - size}]}", v) # support [-1]
      end
    elsif value.is_a?(Hash) # record, etc
      value.each do |k, v|
        placeholders.store("${#{k}}", v) unless placeholder_values.key?(k) # prevent overwriting the reserved keys such as tag
        placeholders.store(%(${#{key}["#{k}"]}), v) # record["foo"]
      end
    else # string, interger, float, and others?
      placeholders.store("${#{key}}", value)
    end
  end
  placeholders
end
preprocess_map(value, _force_stringify = false) click to toggle source
# File lib/fluent/plugin/out_records_merger.rb, line 339
def preprocess_map(value, _force_stringify = false)
  value
end
time_value(time) click to toggle source
# File lib/fluent/plugin/out_records_merger.rb, line 335
def time_value(time)
  Time.at(time).to_s
end

Private Instance Methods

log_if_unknown_placeholder(placeholder, placeholders) click to toggle source
# File lib/fluent/plugin/out_records_merger.rb, line 385
def log_if_unknown_placeholder(placeholder, placeholders)
  log.warn "record_reformer: unknown placeholder `#{placeholder}` found" unless placeholders.include?(placeholder)
end