Class: Fluent::Plugin::RecordTransformerFilter::RubyPlaceholderExpander

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

Overview

THIS CLASS MUST BE THREAD-SAFE

Defined Under Namespace

Classes: CleanroomExpander

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ RubyPlaceholderExpander

Returns a new instance of RubyPlaceholderExpander.



249
250
251
252
253
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 249

def initialize(params)
  @log = params[:log]
  @auto_typecast = params[:auto_typecast]
  @cleanroom_expander = CleanroomExpander.new
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



247
248
249
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 247

def log
  @log
end

Instance Method Details

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

Expand string with placeholders

Parameters:

  • str (String)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 298

def expand(str, placeholders, force_stringify = false)
  @cleanroom_expander.expand(
    str,
    placeholders['tag'],
    placeholders['time'],
    placeholders['record'],
    placeholders['tag_parts'],
    placeholders['tag_prefix'],
    placeholders['tag_suffix'],
    placeholders['hostname'],
  )
rescue => e
  raise "failed to expand `#{str}` : error = #{e}"
end

#prepare_placeholders(placeholder_values) ⇒ Object



291
292
293
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 291

def prepare_placeholders(placeholder_values)
  placeholder_values
end

#preprocess_map(value, force_stringify = false) ⇒ Object

Preprocess record map to convert into ruby string expansion

Parameters:

  • value (Hash|String|Array)

    record map config

  • force_stringify (Boolean) (defaults to: false)

    the value must be string, used for hash key



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 263

def preprocess_map(value, force_stringify = false)
  new_value = nil
  if value.is_a?(String)
    if @auto_typecast && !force_stringify
      num_placeholders = value.scan('${').size
      if num_placeholders == 1 && value.start_with?('${') && value.end_with?('}')
        new_value = value[2..-2] # ${..} => ..
      end
    end
    unless new_value
      new_value = "%Q[#{value.gsub('${', '#{')}]" # xx${..}xx => %Q[xx#{..}xx]
    end
  elsif value.is_a?(Hash)
    new_value = {}
    value.each_pair do |k, v|
      new_value[preprocess_map(k, true)] = preprocess_map(v)
    end
  elsif value.is_a?(Array)
    new_value = []
    value.each_with_index do |v, i|
      new_value[i] = preprocess_map(v)
    end
  else
    new_value = value
  end
  new_value
end

#time_value(time) ⇒ Object



255
256
257
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 255

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