Class: Fluent::RewriteRule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin, conf) ⇒ RewriteRule

Returns a new instance of RewriteRule.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/fluent/plugin/rewrite_rule.rb', line 5

def initialize(plugin, conf)
  @plugin = plugin
  @rules = conf.elements.select {|element| element.name == 'rule' }.map do |element|
    rule = {}
    element.keys.each do |key|
      # read and throw away to supress unread configuration warning
      rule[key] = element[key]
    end
    rule["regex"] = Regexp.new(element["pattern"]) if element.has_key?("pattern")
    rule
  end
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



3
4
5
# File 'lib/fluent/plugin/rewrite_rule.rb', line 3

def rules
  @rules
end

Instance Method Details

#apply_rule(rule, tag = nil, record) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fluent/plugin/rewrite_rule.rb', line 34

def apply_rule(rule, tag=nil, record)
  tag_prefix = tag && tag.length > 0 ? "." : ""
  key        = rule["key"]
  pattern    = rule["pattern"]
  last       = nil

  return [tag, record] if !key || !record.has_key?(key)
  return [tag, record] unless pattern

  if matched = record[key].match(rule["regex"])
    return if rule["ignore"]

    if rule["replace"]
      replace = rule["replace"]
      record[key] = record[key].gsub(rule["regex"], replace)
    end

    if rule["append_to_tag"] && @plugin.is_a?(Fluent::Plugin::Output)
      if rule["tag"]
        tag += (tag_prefix + rule["tag"])
      else
        matched.captures.each do |m|
          tag += (tag_prefix + "#{m}")
        end
      end
    end

    if rule["last"]
      last = true
    end
  else
    if rule["append_to_tag"] && rule["fallback"] && @plugin.is_a?(Fluent::Plugin::Output)
      tag += (tag_prefix + rule["fallback"])
    end
  end

  return [tag, record, last]
end

#rewrite(tag = nil, record) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fluent/plugin/rewrite_rule.rb', line 18

def rewrite(tag=nil, record)
  @rules.each do |rule|
    tag, record, last = apply_rule(rule, tag, record)

    break  if last
    if @plugin.is_a?(Fluent::Plugin::Output)
      return if !tag && !record
    else
      return if !record
    end
  end

  return [record] if not @plugin.is_a?(Fluent::Plugin::Output)
  return [tag, record] if @plugin.is_a?(Fluent::Plugin::Output)
end