Class: Fluent::Plugin::RecordsMergerOutput::PlaceholderExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/out_records_merger.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



330
331
332
333
# File 'lib/fluent/plugin/out_records_merger.rb', line 330

def initialize(params)
  @log = params[:log]
  @auto_typecast = params[:auto_typecast]
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



328
329
330
# File 'lib/fluent/plugin/out_records_merger.rb', line 328

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



328
329
330
# File 'lib/fluent/plugin/out_records_merger.rb', line 328

def placeholders
  @placeholders
end

Instance Method Details

#expand(str, placeholders, force_stringify = false) ⇒ Object

Expand string with placeholders

Parameters:

  • str (String)
  • force_stringify (Boolean) (defaults to: false)

    the value must be string, used for hash key



369
370
371
372
373
374
375
376
377
378
379
380
381
# 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) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# 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) ⇒ Object



339
340
341
# File 'lib/fluent/plugin/out_records_merger.rb', line 339

def preprocess_map(value, _force_stringify = false)
  value
end

#time_value(time) ⇒ Object



335
336
337
# File 'lib/fluent/plugin/out_records_merger.rb', line 335

def time_value(time)
  Time.at(time).to_s
end