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.



188
189
190
191
# File 'lib/fluent/plugin/out_record_reformer.rb', line 188

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

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



186
187
188
# File 'lib/fluent/plugin/out_record_reformer.rb', line 186

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



186
187
188
# File 'lib/fluent/plugin/out_record_reformer.rb', line 186

def placeholders
  @placeholders
end

Instance Method Details

#expand(str, force_stringify = false) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/fluent/plugin/out_record_reformer.rb', line 212

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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/fluent/plugin/out_record_reformer.rb', line 193

def prepare_placeholders(time, record, opts)
  placeholders = { '${time}' => Time.at(time).to_s }
  record.each {|key, value| placeholders.store("${#{key}}", value) }

  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