Class: Fluent::RemoveEmptyOutput::PlaceholderExpander

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ PlaceholderExpander

Returns a new instance of PlaceholderExpander.



111
112
113
# File 'lib/fluent/plugin/out_remove_empty.rb', line 111

def initialize(log)
  @log = log
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



109
110
111
# File 'lib/fluent/plugin/out_remove_empty.rb', line 109

def log
  @log
end

#placeholdersObject (readonly)

Returns the value of attribute placeholders.



109
110
111
# File 'lib/fluent/plugin/out_remove_empty.rb', line 109

def placeholders
  @placeholders
end

Instance Method Details

#expand(str) ⇒ Object



134
135
136
137
138
139
# File 'lib/fluent/plugin/out_remove_empty.rb', line 134

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

#prepare_placeholders(time, record, opts) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fluent/plugin/out_remove_empty.rb', line 115

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