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.



134
135
136
# File 'lib/fluent/plugin/out_record_reformer.rb', line 134

def initialize(log)
  @log = log
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



132
133
134
# File 'lib/fluent/plugin/out_record_reformer.rb', line 132

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



132
133
134
# File 'lib/fluent/plugin/out_record_reformer.rb', line 132

def placeholders
  @placeholders
end

Instance Method Details

#expand(str) ⇒ Object



157
158
159
160
161
162
# File 'lib/fluent/plugin/out_record_reformer.rb', line 157

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

#prepare_placeholders(time, record, opts) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fluent/plugin/out_record_reformer.rb', line 138

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