Sha256: 58a331430244fc2817c825e0e208616b9366a937914136c88894701d6bbdf8b2

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require "rubygems"
require "thread"
require "librato/metrics"

begin
  RUBY_ENGINE
rescue NameError
  RUBY_ENGINE = "ruby"
end

module Recognizer
  class Librato
    def initialize(thread_queue, options)
      unless thread_queue && options.is_a?(Hash)
        raise "You must provide a thread queue and options"
      end
      unless options[:librato][:email] && options[:librato][:api_key]
        raise "You must provide a Librato Metrics account email and API key"
      end
      ::Librato::Metrics.authenticate(options[:librato][:email], options[:librato][:api_key])
      librato = ::Librato::Metrics::Queue.new
      mutex = Mutex.new
      Thread.abort_on_exception = true
      Thread.new do
        loop do
          sleep(options[:librato][:flush_interval] || 10)
          unless librato.queued.empty?
            puts "Attempting to flush metrics to Librato"
            mutex.synchronize do
              librato.submit
            end
            puts "Successfully flushed metrics to Librato"
          end
        end
      end
      Thread.new do
        loop do
          graphite_formated = thread_queue.pop
          puts "Adding metric to queue: #{graphite_formated}"
          metric, value, timestamp = graphite_formated.split(" ")
          mutex.synchronize do
            librato.add metric.to_sym => {:value => value.to_f, :measure_time => timestamp.to_i}
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
recognizer-0.0.3 lib/recognizer/librato.rb