Class: Fluent::Plugin::RecordTransformerFilter::PlaceholderExpander

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

Overview

THIS CLASS MUST BE THREAD-SAFE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



183
184
185
186
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 183

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

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



181
182
183
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 181

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



181
182
183
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 181

def placeholders
  @placeholders
end

Instance Method Details

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



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 222

def expand(str, placeholders, force_stringify = false)
  if @auto_typecast && !force_stringify
    single_placeholder_matched = str.match(/\A(\${[^}]+}|__[A-Z_]+__)\z/)
    if single_placeholder_matched
      log_if_unknown_placeholder($1, placeholders)
      return placeholders[single_placeholder_matched[1]]
    end
  end
  str.gsub(/(\${[^}]+}|__[A-Z_]+__)/) {
    log_if_unknown_placeholder($1, placeholders)
    placeholders[$1]
  }
end

#prepare_placeholders(placeholder_values) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 196

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

  placeholders
end

#preprocess_map(value, force_stringify = false) ⇒ Object



192
193
194
# File 'lib/fluent/plugin/filter_record_transformer.rb', line 192

def preprocess_map(value, force_stringify = false)
  value
end

#time_value(time) ⇒ Object



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

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