Class: Fluent::RewriteOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



9
10
11
# File 'lib/fluent/plugin/out_rewrite.rb', line 9

def rules
  @rules
end

Instance Method Details

#apply_rule(rule, tag, record) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/fluent/plugin/out_rewrite.rb', line 78

def apply_rule(rule, tag, 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"]
      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"]
      tag += (tag_prefix + rule["fallback"])
    end
  end

  [tag, record, last]
end

#configure(conf) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fluent/plugin/out_rewrite.rb', line 11

def configure(conf)
  super

  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  if @add_prefix
    @added_prefix_string = @add_prefix + '.'
  end

  @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

#emit(tag, es, chain) ⇒ Object



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
# File 'lib/fluent/plugin/out_rewrite.rb', line 41

def emit(tag, es, chain)
  _tag = tag.clone

  if @remove_prefix and
    ((tag.start_with?(@removed_prefix_string) && tag.length > @removed_length) || tag == @remove_prefix)
    tag = tag[@removed_length..-1] || ''
  end

  if @add_prefix
    tag = tag && tag.length > 0 ? @added_prefix_string + tag : @add_prefix
  end

  es.each do |time, record|
    filtered_tag, record = rewrite(tag, record)
    if filtered_tag && record && _tag != filtered_tag
      Engine.emit(filtered_tag, time, record)
    else
      if @enable_warnings
        $log.warn "Can not emit message because the tag(#{tag}) has not changed. Dropped record #{record}"
      end
    end
  end

  chain.next
end

#rewrite(tag, record) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/fluent/plugin/out_rewrite.rb', line 67

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

    break  if last
    return if !tag && !record
  end

  [tag, record]
end

#shutdownObject



37
38
39
# File 'lib/fluent/plugin/out_rewrite.rb', line 37

def shutdown
  super
end

#startObject



33
34
35
# File 'lib/fluent/plugin/out_rewrite.rb', line 33

def start
  super
end