Class: Fluent::ForestOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#outputsObject (readonly)

Returns the value of attribute outputs.



10
11
12
# File 'lib/fluent/plugin/out_forest.rb', line 10

def outputs
  @outputs
end

Instance Method Details

#configure(conf) ⇒ Object



12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fluent/plugin/out_forest.rb', line 12

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

  @mapping = {} # tag => output
  @outputs = []
  @mutex = Mutex.new

  @template = nil
  @parameter = nil
  @cases = []

  conf.elements.each do |element|
    # read and throw away to supress unread configuration warning
    touch_recursive(element)

    element.each do |k,v|
      unless v.match(/\$\{tag_parts\[\d\.\.\.?\d\]\}/).nil? or v.match(/__TAG_PARTS\[\d\.\.\.?\d\]__/).nil?
        raise Fluent::ConfigError, "${tag_parts[n]} and __TAG_PARTS[n]__ placeholder does not support range specify at #{k} #{v}"
      end
    end

    case element.name
    when 'template'
      @template = element
    when 'case'
      matcher = Fluent::GlobMatchPattern.new(element.arg)
      @cases.push([matcher, element])
    end
  end

  self
end

#emit(tag, es, chain) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fluent/plugin/out_forest.rb', line 141

def emit(tag, es, chain)
  if @remove_prefix and
      ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix)
    tag = tag[@removed_length..-1]
  end
  if @add_prefix
    tag = if tag.length > 0
            @added_prefix_string + tag
          else
            @add_prefix
          end
  end

  output = @mapping[tag]
  unless output
    output = plant(tag)
  end
  if output
    output.emit(tag, es, chain)
  else
    chain.next
  end
end

#parameter(tag, e, name = 'instance', arg = '') ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fluent/plugin/out_forest.rb', line 69

def parameter(tag, e, name = 'instance', arg = '')
  tag_parts = {}
  tag.split('.').each_with_index do |t, idx| 
    tag_parts["${tag_parts[#{idx}]}"] = t
    tag_parts["__TAG_PARTS[#{idx}]__"] = t
  end
  escaped_tag = tag.gsub('.', @escape_tag_separator)
  pairs = {}
  e.each do |k,v|
    v = v.gsub(/(__TAG_PARTS\[[0-9]+\]__|\${tag_parts\[[0-9]+\]})/) do 
      $log.warn "out_forest: missing placeholder. tag:#{tag} placeholder:#{$1} conf:#{k} #{v}" unless tag_parts.include?($1)
      tag_parts[$1]
    end
    v = v.gsub('__ESCAPED_TAG__', escaped_tag).gsub('${escaped_tag}', escaped_tag)
    pairs[k] = v.gsub('__TAG__', tag).gsub('${tag}', tag).gsub('__HOSTNAME__', @hostname).gsub('${hostname}', @hostname)
  end
  elements = e.elements.map do |child|
    parameter(tag, child, child.name, child.arg)
  end
  Fluent::Config::Element.new(name, arg, pairs, elements)
end

#plant(tag) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fluent/plugin/out_forest.rb', line 113

def plant(tag)
  output = nil
  begin
    @mutex.synchronize {
      output = @mapping[tag]
      unless output
        output = Fluent::Plugin.new_output(@subtype)
        output.configure(spec(tag))
        output.start
        @mapping[tag] = output
        @outputs.push(output)
      end
    }
    $log.info "out_forest plants new output: #{@subtype} for tag '#{tag}'"
  rescue Fluent::ConfigError => e
    $log.error "failed to configure sub output #{@subtype}: #{e.message}"
    $log.error e.backtrace.join("\n")
    $log.error "Cannot output messages with tag '#{tag}'"
    output = nil
  rescue StandardError => e
    $log.error "failed to configure/start sub output #{@subtype}: #{e.message}"
    $log.error e.backtrace.join("\n")
    $log.error "Cannot output messages with tag '#{tag}'"
    output = nil
  end
  output
end

#shutdownObject



62
63
64
65
66
67
# File 'lib/fluent/plugin/out_forest.rb', line 62

def shutdown
  super
  @mapping.values.each do |output|
    output.shutdown
  end
end

#spec(tag) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fluent/plugin/out_forest.rb', line 91

def spec(tag)
  conf = Fluent::Config::Element.new('instance', '', {}, [])
  conf = parameter(tag, @template) + conf if @template # a + b -> b.merge(a) (see: fluentd/lib/fluent/config.rb)
  @cases.each do |m,e|
    if m.match(tag)
      matched_case = parameter(tag, e)

      matched_case.elements.each { |case_child|
        unless case_child.arg.empty?
          conf.elements.delete_if { |conf_child|
            conf_child.name == case_child.name && conf_child.arg == case_child.arg
          }
        end
      }

      conf = matched_case + conf
      break
    end
  end
  conf
end

#touch_recursive(e) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/fluent/plugin/out_forest.rb', line 53

def touch_recursive(e)
  e.keys.each do |k|
    e[k]
  end
  e.elements.each do |child|
    touch_recursive(child)
  end
end