Class: UnicornMetrics::Timer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/unicorn_metrics/timer.rb

Overview

UnicornMetrics::Timer keeps track of total time and the count of ‘ticks’ A simple rate of average of ticks over time elapsed can be calculated this way. For more advanced metrics (e.g., 1/5/15min moving averages) this data should be reported to an intelligent metric store (i.e. Graphite)

Direct Known Subclasses

RequestTimer

Defined Under Namespace

Classes: Stats

Constant Summary collapse

EXPONENT =

The Raindrops::Struct can only hold unsigned long ints (0 -> 4,294,967,295) Since we usually care about ms in a web application, \ let’s store 3 significant digits after the decimal

-3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Timer

Returns a new instance of Timer.

Parameters:

  • name (String)

    user-defined name



20
21
22
23
# File 'lib/unicorn_metrics/timer.rb', line 20

def initialize(name)
  @name  = name
  @stats = Stats.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/unicorn_metrics/timer.rb', line 8

def name
  @name
end

Instance Method Details

#as_jsonHash

Returns JSON representation of the object.

Returns:

  • (Hash)

    JSON representation of the object



48
49
50
51
52
53
54
55
56
# File 'lib/unicorn_metrics/timer.rb', line 48

def as_json(*)
  {
    name => {
      "type"  => type,
      "sum"   => sum,
      "value" => count
    }
  }
end

#resetObject

Reset the timer



38
39
40
# File 'lib/unicorn_metrics/timer.rb', line 38

def reset
  @stats.mantissa = 0 and @stats.count = 0
end

#sumNumeric

Returns total elapsed time.

Returns:

  • (Numeric)

    total elapsed time



43
44
45
# File 'lib/unicorn_metrics/timer.rb', line 43

def sum
  (mantissa * 10**EXPONENT).to_f.round(-EXPONENT)
end

#tick(elapsed_time) ⇒ Object

Parameters:

  • elapsed_time (Numeric)

    in seconds



30
31
32
33
34
35
# File 'lib/unicorn_metrics/timer.rb', line 30

def tick(elapsed_time)
  elapsed_time = (elapsed_time * 10**-EXPONENT).to_i

  @stats.mantissa = mantissa + elapsed_time
  @stats.incr_count
end

#typeObject



25
26
27
# File 'lib/unicorn_metrics/timer.rb', line 25

def type
  "timer"
end