Class: Fluent::RecordReformerOutput::PlaceholderExpander

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



195
196
197
198
# File 'lib/fluent/plugin/out_record_reformer.rb', line 195

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

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



193
194
195
# File 'lib/fluent/plugin/out_record_reformer.rb', line 193

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



193
194
195
# File 'lib/fluent/plugin/out_record_reformer.rb', line 193

def placeholders
  @placeholders
end

Instance Method Details

#expand(str, 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



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/fluent/plugin/out_record_reformer.rb', line 235

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_if_unknown_placeholder($1)
      return @placeholders[single_placeholder_matched[1]]
    end
  end
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
    log_if_unknown_placeholder($1)
    @placeholders[$1]
  }
end

#prepare_placeholders(placeholder_values) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/fluent/plugin/out_record_reformer.rb', line 208

def prepare_placeholders(placeholder_values)
  placeholders = {}

  placeholder_values.each do |key, value|
    if value.kind_of?(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.kind_of?(Hash) # record, etc
      value.each do |k, v|
        placeholders.store("${#{k}}", v) # foo
        placeholders.store(%Q[${#{key}["#{k}"]}], v) # record["foo"]
      end
    else # string, interger, float, and others?
      placeholders.store("${#{key}}", value)
    end
  end

  @placeholders = placeholders
end

#preprocess_map(value, force_stringify = false) ⇒ Object



204
205
206
# File 'lib/fluent/plugin/out_record_reformer.rb', line 204

def preprocess_map(value, force_stringify = false)
  value
end

#time_value(time) ⇒ Object



200
201
202
# File 'lib/fluent/plugin/out_record_reformer.rb', line 200

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