Class: Fluent::Plugin::NumericMonitorOutput

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

Constant Summary collapse

EMIT_STREAM_RECORDS =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject

Returns the value of attribute count.



34
35
36
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 34

def count
  @count
end

#last_checkedObject

Returns the value of attribute last_checked.



34
35
36
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 34

def last_checked
  @last_checked
end

Instance Method Details

#configure(conf) ⇒ Object



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
72
73
74
75
76
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 36

def configure(conf)
  label_routing_specified = conf.has_key?('@label')

  super

  if @unit
    @count_interval = case @unit
                      when :minute then 60
                      when :hour then 3600
                      when :day then 86400
                      else
                        raise "unknown unit: #{@unit}"
                      end
  end

  if @input_tag_remove_prefix
    @removed_prefix_string = @input_tag_remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end

  @key_prefix_string = ''
  if @output_key_prefix
    @key_prefix_string = @output_key_prefix + '_'
  end

  if @output_per_tag && (!label_routing_specified && !@tag_prefix)
    raise Fluent::ConfigError, "specify @label to route output events into other <label> sections."
  end
  if @output_per_tag && @tag_prefix
    @tag_prefix_string = @tag_prefix + '.'
  else
    @tag_prefix_string = nil
  end

  if system_config.workers > 1
    log.warn "Fluentd is now working with multi process workers, and numeric_monitor plugin will produce monitor results in each separated processes."
  end

  @count = count_initialized
  @mutex = Mutex.new
end

#count_initialized(keys = nil) ⇒ Object



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

def count_initialized(keys=nil)
  # counts['tag'] = {:min => num, :max => num, :sum => num, :num => num [, :sample => [....]]}
  if @aggregate == :all
    if @percentiles
      {'all' => {min: nil, max: nil, sum: nil, num: 0, sample: []}}
    else
      {'all' => {min: nil, max: nil, sum: nil, num: 0}}
    end
  elsif keys
    values = if @percentiles
               Array.new(keys.length) {|i| {min: nil, max: nil, sum: nil, num: 0, sample: []}}
             else
               Array.new(keys.length) {|i| {min: nil, max: nil, sum: nil, num: 0}}
             end
    Hash[[keys, values].transpose]
  else
    {}
  end
end

#countups(tag, min, max, sum, num, sample) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 187

def countups(tag, min, max, sum, num, sample)
  if @aggregate == :all
    tag = 'all'
  end

  @mutex.synchronize do
    c = (@count[tag] ||= {min: nil, max: nil, sum: nil, num: 0})

    if c[:min].nil? or c[:min] > min
      c[:min] = min
    end
    if c[:max].nil? or c[:max] < max
      c[:max] = max
    end
    c[:sum] = (c[:sum] || 0) + sum
    c[:num] += num

    if @percentiles
      c[:sample] ||= []
      if c[:sample].size + sample.size > @samples_limit
        (c[:sample].size + sample.size - @samples_limit).times do
          c[:sample].delete_at(rand(c[:sample].size))
        end
      end
      c[:sample] += sample
    end
  end
end

#flushObject



167
168
169
170
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 167

def flush
  flushed,@count = @count,count_initialized(@count.keys.dup)
  generate_output(flushed)
end

#flush_emitObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 172

def flush_emit
  if @output_per_tag
    time = Fluent::Engine.now
    flush.each do |tag, message|
      if @tag_prefix_string
        router.emit(@tag_prefix_string + tag, time, message)
      else
        router.emit(tag, time, message)
      end
    end
  else
    router.emit(@tag, Fluent::Engine.now, flush)
  end
end

#generate_fields(count, key_prefix = '', output = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 120

def generate_fields(count, key_prefix = '', output = {})
  output[key_prefix + 'num'] = count[:num] if count[:num]
  output[key_prefix + 'min'] = count[:min] if count[:min]
  output[key_prefix + 'max'] = count[:max] if count[:max]
  output[key_prefix + 'avg'] = (count[:sum] / (count[:num] * 1.0)) if count[:num] > 0
  output[key_prefix + 'sum'] = count[:sum] if count[:sum]
  if @percentiles
    sorted = count[:sample].sort
    @percentiles.each do |p|
      i = (count[:num] * p / 100).floor
      if i > 0
        i -= 1
      end
      output[key_prefix + "percentile_#{p}"] = sorted[i]
    end
  end
  output
end

#generate_output(count) ⇒ Object



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

def generate_output(count)
  if @aggregate == :all
    if @output_per_tag
      # tag_prefix_all: { 'key_prefix_min' => -10, 'key_prefix_max' => 10, ... } }
      output = {'all' => generate_fields(count['all'], @key_prefix_string)}
    else
      # tag: { 'key_prefix_min' => -10, 'key_prefix_max' => 10, ... }
      output = generate_fields(count['all'], @key_prefix_string)
    end
  else
    output = {}
    if @output_per_tag
      # tag_prefix_tag1: { 'key_prefix_min' => -10, 'key_prefix_max' => 10, ... }
      # tag_prefix_tag2: { 'key_prefix_min' => -10, 'key_prefix_max' => 10, ... }
      count.keys.each do |tag|
        output[stripped_tag(tag)] = generate_fields(count[tag], @key_prefix_string)
      end
    else
      # tag: { 'key_prefix_tag1_min' => -10, 'key_prefix_tag1_max' => 10, ..., 'key_prefix_tag2_min' => -10, 'key_prefix_tag2_max' => 10, ... }
      count.keys.each do |tag|
        key_prefix = @key_prefix_string + stripped_tag(tag) + '_'
        generate_fields(count[tag], key_prefix, output)
      end
    end
  end
  output
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 78

def multi_workers_ready?
  true
end

#process(tag, es) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 216

def process(tag, es)
  min = nil
  max = nil
  sum = 0
  num = 0
  sample = if @percentiles then [] else nil end

  es.each do |time,record|
    value = record[@monitor_key]
    next if value.nil?

    value = value.to_f
    if min.nil? or min > value
      min = value
    end
    if max.nil? or max < value
      max = value
    end
    sum += value
    num += 1

    if @percentiles
      sample.push(value)
    end
  end
  if @percentiles && sample.size > @samples_limit
    (sample.size - @samples_limit / 2).to_i.times do
      sample.delete_at(rand(sample.size))
    end
  end
  countups(tag, min, max, sum, num, sample)
end

#startObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 82

def start
  super

  @last_checked = Fluent::Engine.now
  timer_execute(:out_numeric_counter_watcher, @count_interval) do
    now = Fluent::Engine.now
    flush_emit
    @last_checked = now
  end
end

#stripped_tag(tag) ⇒ Object



113
114
115
116
117
118
# File 'lib/fluent/plugin/out_numeric_monitor.rb', line 113

def stripped_tag(tag)
  return tag unless @input_tag_remove_prefix
  return tag[@removed_length..-1] if tag.start_with?(@removed_prefix_string) and tag.length > @removed_length
  return tag[@removed_length..-1] if tag == @input_tag_remove_prefix
  tag
end