Class: Librato::Metrics::Queue
- Inherits:
-
Object
- Object
- Librato::Metrics::Queue
- Defined in:
- lib/librato/metrics/queue.rb
Constant Summary collapse
- MEASUREMENTS_PER_REQUEST =
500
Instance Attribute Summary collapse
-
#per_request ⇒ Object
readonly
Returns the value of attribute per_request.
-
#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:.
-
#client ⇒ Librato::Metrics::Client
The current Client instance this queue is using to authenticate and connect to Librato Metrics.
-
#counters ⇒ Array
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.
9 10 11 12 13 14 |
# File 'lib/librato/metrics/queue.rb', line 9 def initialize(={}) @queued = {} @per_request = [:per_request] || MEASUREMENTS_PER_REQUEST @skip_measurement_times = [:skip_measurement_times] @client = [:client] || Librato::Metrics.client end |
Instance Attribute Details
#per_request ⇒ Object (readonly)
Returns the value of attribute per_request.
6 7 8 |
# File 'lib/librato/metrics/queue.rb', line 6 def per_request @per_request end |
#skip_measurement_times ⇒ Object
Returns the value of attribute skip_measurement_times.
7 8 9 |
# File 'lib/librato/metrics/queue.rb', line 7 def skip_measurement_times @skip_measurement_times end |
Instance Method Details
#add(args) ⇒ Object
Add a metric entry to the metric set:
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/librato/metrics/queue.rb', line 20 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 |
#client ⇒ Librato::Metrics::Client
The current Client instance this queue is using to authenticate and connect to Librato Metrics. This will default to the primary client used by the Librato::Metrics module unless it has been set to something else.
46 47 48 |
# File 'lib/librato/metrics/queue.rb', line 46 def client @client ||= Librato::Metrics.client end |
#counters ⇒ Array
Currently queued counters
53 54 55 |
# File 'lib/librato/metrics/queue.rb', line 53 def counters @queued[:counters] || [] end |
#empty? ⇒ Boolean
Are any metrics currently queued?
60 61 62 |
# File 'lib/librato/metrics/queue.rb', line 60 def empty? @queued.empty? end |
#flush ⇒ Object Also known as: clear, flush_queued
Remove all queued metrics
66 67 68 |
# File 'lib/librato/metrics/queue.rb', line 66 def flush @queued = {} end |
#gauges ⇒ Object
Currently queued gauges
81 82 83 |
# File 'lib/librato/metrics/queue.rb', line 81 def gauges @queued[:gauges] || [] end |
#persister ⇒ Object
The object this MetricSet will use to persist
74 75 76 |
# File 'lib/librato/metrics/queue.rb', line 74 def persister @persister ||= create_persister end |
#queued ⇒ Object
All currently queued metrics
88 89 90 |
# File 'lib/librato/metrics/queue.rb', line 88 def queued @queued end |
#size ⇒ Object Also known as: length
Count of metrics currently queued
95 96 97 |
# File 'lib/librato/metrics/queue.rb', line 95 def size self.queued.inject(0) { |result, data| result + data.last.size } end |
#submit ⇒ Object
Persist currently queued metrics
103 104 105 106 107 108 109 110 |
# File 'lib/librato/metrics/queue.rb', line 103 def submit raise NoMetricsQueued if self.queued.empty? = {:per_request => @per_request} if persister.persist(self.client, 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.
130 131 132 133 134 135 136 |
# File 'lib/librato/metrics/queue.rb', line 130 def time(name, ={}) start = Time.now yield duration = (Time.now - start) * 1000.0 # milliseconds metric = {name => .merge({:value => duration})} add metric end |