Class: Metrics::Rails::Aggregator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/metrics/rails/aggregator.rb

Instance Method Summary collapse

Constructor Details

#initializeAggregator

Returns a new instance of Aggregator.



8
9
10
11
# File 'lib/metrics/rails/aggregator.rb', line 8

def initialize
  @cache = Librato::Metrics::Aggregator.new
  @lock = Mutex.new
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/metrics/rails/aggregator.rb', line 13

def [](key)
  return nil if @cache.empty?
  gauges = nil
  @lock.synchronize { gauges = @cache.queued[:gauges] }
  gauges.each do |metric|
    return metric if metric[:name] == key.to_s
  end
  nil
end

#delete_allObject



23
24
25
# File 'lib/metrics/rails/aggregator.rb', line 23

def delete_all
  @lock.synchronize { @cache.clear }
end

#flush_to(queue, options = {}) ⇒ Object

transfer all measurements to a queue and reset internal status



29
30
31
32
33
34
35
36
37
# File 'lib/metrics/rails/aggregator.rb', line 29

def flush_to(queue, options={})
  queued = nil
  @lock.synchronize do
    return if @cache.empty?
    queued = @cache.queued
    @cache.clear
  end
  queue.merge!(queued) if queued
end

#measure(event, duration) ⇒ Object Also known as: timing



39
40
41
42
43
# File 'lib/metrics/rails/aggregator.rb', line 39

def measure(event, duration)
  @lock.synchronize do
    @cache.add event.to_s => duration
  end
end