class Fluent::ElapsedTime
Attributes
hook[R]
input[R]
interval[R]
log[R]
mutex[R]
tag[R]
thread[R]
times[R]
Public Class Methods
new(input, log)
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 47 def initialize(input, log) @input = input @log = log @times = [] @mutex = Mutex.new end
Public Instance Methods
add(time)
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 63 def add(time) @times << time end
apply_hook(hook)
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 71 def apply_hook(hook) if hook.include?('.') klass_name, method_name = hook.split('.', 2) klass = constantize(klass_name) else klass = @input.class method_name = hook end old_method_name = "#{method_name}_without_elapsed".to_sym unless klass.method_defined?(old_method_name) klass.__send__(:alias_method, old_method_name, method_name) klass.__send__(:define_method, method_name) do |*args| elapsed.measure_time(klass, method_name) do self.__send__(old_method_name, *args) end end end end
clear()
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 67 def clear @times.clear end
configure(conf)
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 54 def configure(conf) @tag = conf['tag'] || 'elapsed' @interval = conf['interval'].to_i || 60 unless @hook = conf['hook'] raise Fluent::ConfigError, '`hook` option must be specified in <elapsed></elpased> directive' end apply_hook(@hook) end
flush(now)
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 123 def flush(now) times = [] @mutex.synchronize do times = @times.dup self.clear end triple = nil unless times.empty? num = times.size max = num == 0 ? 0 : times.max avg = num == 0 ? 0 : times.map(&:to_f).inject(:+) / num.to_f triple = [@tag, now, {:num => num, :max => max, :avg => avg}] Engine.emit(*triple) end triple end
measure_time(klass, method_name) { || ... }
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 90 def measure_time(klass, method_name) started = Time.now output = yield elapsed = (Time.now - started).to_f log.info "elapsed time at #{klass}##{method_name} is #{elapsed} sec" @mutex.synchronize { self.add(elapsed) } output end
run()
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 108 def run @last_checked ||= Engine.now while (sleep 0.5) begin now = Engine.now if now - @last_checked >= @interval flush(now) @last_checked = now end rescue => e log.warn "in_forward: #{e.class} #{e.message} #{e.backtrace.first}" end end end
start()
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 99 def start @thread = Thread.new(&method(:run)) end
stop()
click to toggle source
# File lib/fluent/mixin/elapsed_time.rb, line 103 def stop @thread.terminate @thread.join end
Private Instance Methods
constantize(camel_cased_word)
click to toggle source
File activesupport/lib/active_support/inflector/methods.rb, line 219
# File lib/fluent/mixin/elapsed_time.rb, line 142 def constantize(camel_cased_word) names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? names.inject(Object) do |constant, name| if constant == Object constant.const_get(name) else candidate = constant.const_get(name) next candidate if constant.const_defined?(name, false) next candidate unless Object.const_defined?(name) # Go down the ancestors to check it it's owned # directly before we reach Object or the end of ancestors. constant = constant.ancestors.inject do |const, ancestor| break const if ancestor == Object break ancestor if ancestor.const_defined?(name, false) const end # owner is in Object, so raise constant.const_get(name, false) end end end