Class: Fluent::HttpRecordModifier::PlaceholderExpander

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



267
268
269
270
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 267

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

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



265
266
267
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 265

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



265
266
267
# File 'lib/fluent/plugin/filter_http_record_modifier.rb', line 265

def placeholders
  @placeholders
end

Instance Method Details

#crawl_placeholder(value, placeholder, before, limit = 50) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# 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) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
# 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) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# 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