class Pliny::Librato::Metrics::Backend

Implements the Pliny::Metrics.backends API. Puts any metrics sent from Pliny::Metrics onto a queue that gets submitted in batches.

Attributes

aggregator[R]
counter_cache[R]
interval[R]
source[R]
timer[R]

Public Class Methods

new(source: nil, interval: 60) click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 12
def initialize(source: nil, interval: 60)
  @source        = source
  @interval      = interval
  @mutex         = Mutex.new
  @counter_cache = ::Librato::Collector::CounterCache.new(default_tags: nil)
  @aggregator    = ::Librato::Metrics::Aggregator.new
end

Public Instance Methods

new_librato_queue() click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 20
def new_librato_queue
  ::Librato::Metrics::Queue.new(
    source: source,
    skip_measurement_times: true
  )
end
report_counts(counts) click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 27
def report_counts(counts)
  sync do
    counts.each do |name, val|
      counter_cache.increment(name, val)
    end
  end
end
report_measures(measures) click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 35
def report_measures(measures)
  sync do
    aggregator.add(measures)
  end
end
start() click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 41
def start
  start_timer
  self
end
stop() click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 46
def stop
  # Ensure timer is not running when we terminate it
  sync { timer.terminate }
  flush_librato
end

Private Instance Methods

flush_librato() click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 67
def flush_librato
  queue = new_librato_queue

  sync do
    # Gather all counters / measures from the aggregator / counter_cache.
    counter_cache.flush_to(queue)
    queue.merge!(aggregator)
    aggregator.clear
  end

  # Submit explicitly, given the queue won't pass autosubmit_check
  # (because @autosubmit_count=nil @autosubmit_interval=nil)
  queue.submit
end
start_timer() click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 56
def start_timer
  @timer = Thread.new do
    loop do
      sleep interval
      wrap_errors do
        flush_librato
      end
    end
  end
end
sync(&block) click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 82
def sync(&block)
  wrap_errors do
    @mutex.synchronize(&block)
  end
end
wrap_errors() { || ... } click to toggle source
# File lib/pliny/librato/metrics/backend.rb, line 88
def wrap_errors
  yield
rescue => error
  begin
    Pliny::ErrorReporters.notify(error)
  rescue
  end
end