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(log) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



175
176
177
# File 'lib/fluent/plugin/out_record_reformer.rb', line 175

def initialize(log)
  @log = log
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



173
174
175
# File 'lib/fluent/plugin/out_record_reformer.rb', line 173

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



173
174
175
# File 'lib/fluent/plugin/out_record_reformer.rb', line 173

def placeholders
  @placeholders
end

Instance Method Details

#expand(str) ⇒ Object



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

def expand(str)
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
    log.warn "record_reformer: unknown placeholder `#{$1}` found" unless @placeholders.include?($1)
    @placeholders[$1]
  }
end

#prepare_placeholders(time, record, opts) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fluent/plugin/out_record_reformer.rb', line 179

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