Class: Fluent::ReplaceOutput

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

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object

Raises:

  • (Fluent::ConfigError)


9
10
11
12
13
14
# File 'lib/fluent/plugin/out_replace.rb', line 9

def configure(conf)
  super

  @rules = YAML.load_file(@rules_yaml) rescue nil
  raise Fluent::ConfigError, "#{@rules_yaml} is not found" unless @rules
end

#emit(tag, es, chain) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fluent/plugin/out_replace.rb', line 16

def emit(tag, es, chain)
  tag = @tag if @tag

  es.each do |time, record|
    begin
      @rules.each do |key, rule|
        target = record[key].to_s

        rule.each do |from, to|
          if match_data = from.to_s.match(/\/(?<pattern>.+)\//)
            pattern = Regexp.new(match_data[:pattern])
            record[key] = target.gsub(pattern, to) if target.match(pattern)
          else
            record[key] = to if from.to_s == target
          end
        end
      end
    rescue
      raise Fluent::ConfigError, 'Invalid YAML format'
    end
    Fluent::Engine.emit(tag, time, record)
  end

  chain.next
end