Class: Fluent::CalcOutput
- Inherits:
-
Output
- Object
- Output
- Fluent::CalcOutput
- Defined in:
- lib/fluent/plugin/out_calc.rb
Instance Attribute Summary collapse
-
#counts ⇒ Object
Returns the value of attribute counts.
-
#last_checked ⇒ Object
Returns the value of attribute last_checked.
-
#matches ⇒ Object
Returns the value of attribute matches.
-
#saved_at ⇒ Object
Returns the value of attribute saved_at.
-
#saved_duration ⇒ Object
Returns the value of attribute saved_duration.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
-
#emit(tag, es, chain) ⇒ Object
Called when new line comes.
-
#flush_emit(step) ⇒ Object
This method is the real one to emit.
- #generate_output(count, matches) ⇒ Object
-
#initialize ⇒ CalcOutput
constructor
A new instance of CalcOutput.
-
#load_status(file_path, interval) ⇒ Object
Load internal status from a file.
- #max(a, b) ⇒ Object
- #min(a, b) ⇒ Object
-
#save_status(file_path) ⇒ Object
Store internal status into a file.
- #shutdown ⇒ Object
- #start ⇒ Object
- #sum(a, b) ⇒ Object
-
#watcher ⇒ Object
thread callback.
Constructor Details
#initialize ⇒ CalcOutput
5 6 7 8 |
# File 'lib/fluent/plugin/out_calc.rb', line 5 def initialize super require 'pathname' end |
Instance Attribute Details
#counts ⇒ Object
Returns the value of attribute counts.
28 29 30 |
# File 'lib/fluent/plugin/out_calc.rb', line 28 def counts @counts end |
#last_checked ⇒ Object
Returns the value of attribute last_checked.
32 33 34 |
# File 'lib/fluent/plugin/out_calc.rb', line 32 def last_checked @last_checked end |
#matches ⇒ Object
Returns the value of attribute matches.
29 30 31 |
# File 'lib/fluent/plugin/out_calc.rb', line 29 def matches @matches end |
#saved_at ⇒ Object
Returns the value of attribute saved_at.
31 32 33 |
# File 'lib/fluent/plugin/out_calc.rb', line 31 def saved_at @saved_at end |
#saved_duration ⇒ Object
Returns the value of attribute saved_duration.
30 31 32 |
# File 'lib/fluent/plugin/out_calc.rb', line 30 def saved_duration @saved_duration end |
Instance Method Details
#configure(conf) ⇒ 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 |
# File 'lib/fluent/plugin/out_calc.rb', line 34 def configure(conf) super @interval = @interval.to_i @sum = Regexp.new(@sum) if @sum @max = Regexp.new(@max) if @max @min = Regexp.new(@min) if @min @avg = Regexp.new(@avg) if @avg @sum_keys = @sum_keys ? @sum_keys.split(',') : [] @max_keys = @max_keys ? @max_keys.split(',') : [] @min_keys = @min_keys ? @min_keys.split(',') : [] @avg_keys = @avg_keys ? @avg_keys.split(',') : [] unless ['tag', 'all'].include?(@aggregate) raise Fluent::ConfigError, "aggregate allows tag/all" end case @aggregate when 'all' raise Fluent::ConfigError, "tag must be specified for aggregate all" if @tag.nil? end @counts = {} @matches = {} @mutex = Mutex.new end |
#emit(tag, es, chain) ⇒ Object
Called when new line comes. This method actually does not emit
75 76 77 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fluent/plugin/out_calc.rb', line 75 def emit(tag, es, chain) tag = 'all' if @aggregate == 'all' # calc count = 0; matches = { :sum => {}, :max => {}, :min => {}, :avg => {} } es.each do |time, record| @sum_keys.each do |key| matches[:sum][key] = sum(matches[:sum][key], record[key]) end @max_keys.each do |key| matches[:max][key] = max(matches[:max][key], record[key]) end @min_keys.each do |key| matches[:min][key] = min(matches[:min][key], record[key]) end @avg_keys.each do |key| matches[:avg][key] = avg(matches[:avg][key], record[key]) end record.keys.each do |key| if @sum and @sum.match(key) matches[:sum][key] = sum(matches[:sum][key], record[key]) end if @max and @max.match(key) matches[:max][key] = max(matches[:max][key], record[key]) end if @min and @min.match(key) matches[:min][key] = min(matches[:min][key], record[key]) end if @avg and @avg.match(key) matches[:avg][key] = sum(matches[:avg][key], record[key]) # sum yet end end if @sum || @max || @min || @avg count += 1 end # thread safe merge @counts[tag] ||= 0 @matches[tag] ||= { :sum => {}, :max => {}, :min => {}, :avg => {} } @mutex.synchronize do matches[:sum].keys.each do |key| @matches[tag][:sum][key] = sum(@matches[tag][:sum][key], matches[:sum][key]) end matches[:max].keys.each do |key| @matches[tag][:max][key] = max(@matches[tag][:max][key], matches[:max][key]) end matches[:min].keys.each do |key| @matches[tag][:min][key] = min(@matches[tag][:min][key], matches[:min][key]) end matches[:avg].keys.each do |key| @matches[tag][:avg][key] = sum(@matches[tag][:avg][key], matches[:avg][key]) # sum yet end @counts[tag] += count end chain.next rescue => e $log.warn "#{e.class} #{e.message} #{e.backtrace.first}" end |
#flush_emit(step) ⇒ Object
This method is the real one to emit
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/fluent/plugin/out_calc.rb', line 153 def flush_emit(step) time = Fluent::Engine.now flushed_counts, flushed_matches, @counts, @matches = @counts, @matches, {}, {} flushed_counts.keys.each do |tag| count = flushed_counts[tag] matches = flushed_matches[tag] output = generate_output(count, matches) tag = @tag ? @tag : "#{@add_tag_prefix}.#{tag}" Fluent::Engine.emit(tag, time, output) if output end end |
#generate_output(count, matches) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/fluent/plugin/out_calc.rb', line 166 def generate_output(count, matches) return nil if matches.empty? output = {} matches[:sum].keys.each do |key| output[key + @sum_suffix] = matches[:sum][key] end matches[:max].keys.each do |key| output[key + @max_suffix] = matches[:max][key] end matches[:min].keys.each do |key| output[key + @min_suffix] = matches[:min][key] end matches[:avg].keys.each do |key| output[key + @avg_suffix] = matches[:avg][key] / count.to_f # compute avg here end output end |
#load_status(file_path, interval) ⇒ Object
Load internal status from a file
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/fluent/plugin/out_calc.rb', line 227 def load_status(file_path, interval) return unless (f = Pathname.new(file_path)).exist? begin f.open('rb') do |f| stored = Marshal.load(f) if stored[:aggregate] == @aggregate and stored[:sum] == @sum and stored[:max] == @max and stored[:min] == @min and stored[:avg] == @avg if !stored[:matches].empty? and !stored[:matches].first[1].has_key?(:max) $log.warn "out_calc: stored data does not have compatibility with the current version. ignore stored data" return end if Fluent::Engine.now <= stored[:saved_at] + interval @counts = stored[:counts] @matches = stored[:matches] @saved_at = stored[:saved_at] @saved_duration = stored[:saved_duration] # skip the saved duration to continue counting @last_checked = Fluent::Engine.now - @saved_duration else $log.warn "out_calc: stored data is outdated. ignore stored data" end else $log.warn "out_calc: configuration param was changed. ignore stored data" end end rescue => e $log.warn "out_calc: Can't load store_file #{e.class} #{e.message}" end end |
#max(a, b) ⇒ Object
188 189 190 |
# File 'lib/fluent/plugin/out_calc.rb', line 188 def max(a, b) [a, b].compact.max end |
#min(a, b) ⇒ Object
192 193 194 |
# File 'lib/fluent/plugin/out_calc.rb', line 192 def min(a, b) [a, b].compact.min end |
#save_status(file_path) ⇒ Object
Store internal status into a file
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/fluent/plugin/out_calc.rb', line 199 def save_status(file_path) return unless file_path begin Pathname.new(file_path).open('wb') do |f| @saved_at = Fluent::Engine.now @saved_duration = @saved_at - @last_checked Marshal.dump({ :counts => @counts, :matches => @matches, :saved_at => @saved_at, :saved_duration => @saved_duration, :aggregate => @aggregate, :sum => @sum, :max => @max, :min => @min, :avg => @avg, }, f) end rescue => e $log.warn "out_calc: Can't write store_file #{e.class} #{e.message}" end end |
#shutdown ⇒ Object
67 68 69 70 71 72 |
# File 'lib/fluent/plugin/out_calc.rb', line 67 def shutdown super @watcher.terminate @watcher.join save_status(@store_file) if @store_file end |
#start ⇒ Object
61 62 63 64 65 |
# File 'lib/fluent/plugin/out_calc.rb', line 61 def start super load_status(@store_file, @interval) if @store_file @watcher = Thread.new(&method(:watcher)) end |
#sum(a, b) ⇒ Object
184 185 186 |
# File 'lib/fluent/plugin/out_calc.rb', line 184 def sum(a, b) [a, b].compact.inject(:+) end |
#watcher ⇒ Object
thread callback
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/fluent/plugin/out_calc.rb', line 135 def watcher # instance variable, and public accessable, for test @last_checked ||= Fluent::Engine.now while true sleep 0.5 begin if Fluent::Engine.now - @last_checked >= @interval now = Fluent::Engine.now flush_emit(now - @last_checked) @last_checked = now end rescue => e $log.warn "#{e.class} #{e.message} #{e.backtrace.first}" end end end |