Class: Librato::Metrics::Queue
- Inherits:
-
Object
- Object
- Librato::Metrics::Queue
- Defined in:
- lib/librato/metrics/queue.rb
Instance Attribute Summary collapse
-
#skip_measurement_times ⇒ Object
Returns the value of attribute skip_measurement_times.
Instance Method Summary collapse
-
#add(args) ⇒ Object
Add a metric entry to the metric set:.
-
#counters ⇒ Object
Currently queued counters.
-
#empty? ⇒ Boolean
Are any metrics currently queued?.
-
#flush ⇒ Object
(also: #clear, #flush_queued)
Remove all queued metrics.
-
#gauges ⇒ Object
Currently queued gauges.
-
#initialize(options = {}) ⇒ Queue
constructor
A new instance of Queue.
-
#persister ⇒ Object
The object this MetricSet will use to persist.
-
#queued ⇒ Object
All currently queued metrics.
-
#size ⇒ Object
(also: #length)
Count of metrics currently queued.
-
#submit ⇒ Object
Persist currently queued metrics.
-
#time(name, options = {}) ⇒ Object
(also: #benchmark)
Capture execution time for a block and queue it as the value for a metric.
Constructor Details
#initialize(options = {}) ⇒ Queue
Returns a new instance of Queue.
7 8 9 10 |
# File 'lib/librato/metrics/queue.rb', line 7 def initialize(={}) @queued ||= {} @skip_measurement_times = .delete(:skip_measurement_times) end |
Instance Attribute Details
#skip_measurement_times ⇒ Object
Returns the value of attribute skip_measurement_times.
5 6 7 |
# File 'lib/librato/metrics/queue.rb', line 5 def skip_measurement_times @skip_measurement_times end |
Instance Method Details
#add(args) ⇒ Object
Add a metric entry to the metric set:
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/librato/metrics/queue.rb', line 16 def add(args) args.each do |key, value| if value.respond_to?(:each) metric = value metric[:name] = key.to_s type = metric.delete(:type) || metric.delete('type') || 'gauge' else metric = {:name => key.to_s, :value => value} type = :gauge end type = ("#{type}s").to_sym unless skip_measurement_times metric[:measure_time] ||= epoch_time end @queued[type] ||= [] @queued[type] << metric end queued end |
#counters ⇒ Object
Currently queued counters
39 40 41 |
# File 'lib/librato/metrics/queue.rb', line 39 def counters @queued[:counters] || [] end |
#empty? ⇒ Boolean
Are any metrics currently queued?
46 47 48 |
# File 'lib/librato/metrics/queue.rb', line 46 def empty? @queued.empty? end |
#flush ⇒ Object Also known as: clear, flush_queued
Remove all queued metrics
52 53 54 |
# File 'lib/librato/metrics/queue.rb', line 52 def flush @queued = {} end |
#gauges ⇒ Object
Currently queued gauges
67 68 69 |
# File 'lib/librato/metrics/queue.rb', line 67 def gauges @queued[:gauges] || [] end |
#persister ⇒ Object
The object this MetricSet will use to persist
60 61 62 |
# File 'lib/librato/metrics/queue.rb', line 60 def persister @persister ||= create_persister end |
#queued ⇒ Object
All currently queued metrics
74 75 76 |
# File 'lib/librato/metrics/queue.rb', line 74 def queued @queued end |
#size ⇒ Object Also known as: length
Count of metrics currently queued
81 82 83 |
# File 'lib/librato/metrics/queue.rb', line 81 def size self.queued.inject(0) { |result, data| result + data.last.size } end |
#submit ⇒ Object
Persist currently queued metrics
89 90 91 92 93 94 95 |
# File 'lib/librato/metrics/queue.rb', line 89 def submit raise NoMetricsQueued if self.queued.empty? if persister.persist(self.queued) flush and return true end false end |
#time(name, options = {}) ⇒ Object Also known as: benchmark
Capture execution time for a block and queue it as the value for a metric. Times are recorded in milliseconds.
Options are the same as for #add.
115 116 117 118 119 120 121 |
# File 'lib/librato/metrics/queue.rb', line 115 def time(name, ={}) start = Time.now yield duration = (Time.now - start) * 1000.0 # milliseconds metric = {name => .merge({:value => duration})} add metric end |