Class: UnicornMetrics::Timer
- Inherits:
-
Object
- Object
- UnicornMetrics::Timer
- 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
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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#as_json ⇒ Hash
JSON representation of the object.
-
#initialize(name) ⇒ Timer
constructor
A new instance of Timer.
-
#reset ⇒ Object
Reset the timer.
-
#sum ⇒ Numeric
Total elapsed time.
- #tick(elapsed_time) ⇒ Object
- #type ⇒ Object
Constructor Details
Instance Attribute Details
#name ⇒ Object (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_json ⇒ Hash
Returns 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 |
#reset ⇒ Object
Reset the timer
38 39 40 |
# File 'lib/unicorn_metrics/timer.rb', line 38 def reset @stats.mantissa = 0 and @stats.count = 0 end |
#sum ⇒ Numeric
Returns 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
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 |
#type ⇒ Object
25 26 27 |
# File 'lib/unicorn_metrics/timer.rb', line 25 def type "timer" end |