Sha256: ac01b6cb76b543146adfa0472a1657a42668cc3a233b41a77a5a8705a2c966c5
Contents?: true
Size: 1.53 KB
Versions: 7
Compression:
Stored size: 1.53 KB
Contents
require 'thread' require 'logger' module Anschel class Stats def initialize interval, logger @logger = logger @interval = interval || 30 @stats = Hash.new @lock = Mutex.new Thread.new do loop do sleep @interval report_and_reset end end @logger.info event: 'stats-loaded' end def create name, default=0 with_lock do stats[name] = {} stats[name].default = default stats end end def delete name with_lock do stats.delete name end end def inc name, by=1 with_lock do stats[name][:val] += by end end def dec name, by=1 with_lock do stats[name][:val] -= by end end def set name, to with_lock do stats[name][:val] = to end end def get name with_lock do stats[name][:val] end end private attr_reader :stats def with_lock &block @lock.synchronize do yield end end def report_and_reset ready_stats = with_lock do stats.keys.flat_map do |k| v = stats[k].delete(:val) v ||= stats[k].default stats[k][:val] = stats[k].default [ [ k, v ], [ "#{k}-rate", 1.0 * v / @interval ] ] end end return if stats.empty? @logger.info \ event: 'stats', interval: @interval, stats: Hash[ready_stats] end end end
Version data entries
7 entries across 7 versions & 1 rubygems