Module: Core::Stats

Defined in:
lib/svcbase/stats.rb

Overview

Utility module to collect statistics

Defined Under Namespace

Classes: Accumulator

Class Method Summary collapse

Class Method Details

.collect(*args) ⇒ Object

collect a numeric data point first n-1 args are used as hash keys, the last arg is the data value

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
# File 'lib/svcbase/stats.rb', line 41

def self.collect(*args)
  raise ArgumentError unless args.length >= 2
  value = args.pop
  @mutex.synchronize do
    # find/create the appropriate object within the buffer
    obj = args.inject(@buffer) { |buf, key| buf[key.to_s] ||= {} }
    # add the data value to the object's accumulator
    (obj[:_data] ||= Accumulator.new).add_value(value)
  end
end

.log_and_resetObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/svcbase/stats.rb', line 65

def self.log_and_reset
  msgs = ["Server: #{Server::ID}", "Commit: #{Core::Version::ID}", "Tag: #{Core::Version::TAG}"]

  # app can define a #custom_messages method to customize reporting
  msgs.concat(Array(custom_messages)) if respond_to? :custom_messages

  @mutex.synchronize do
    traverse_data(@buffer) do |keylist, data|
      count  = data.count
      min    = data.min
      mean   = data.mean.round(2)
      max    = data.max
      stddev = data.stddev.round(2)
      msgs << "#{keylist.join('|')}: count: #{count} min: #{min} mean: #{mean} max: #{max} stddev: #{stddev}"
    end
    @buffer = {}
  end
  msgs.each { |msg| log.info('stats') { msg } }
end