Sha256: ebfd7541cc516d0f9fa412aa4aa3aeb8163763e685d8f75d9f67a3eea9f5d3c7

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

require 'singleton'
require 'configatron'
require 'lookout/statsd'

require 'lookout/rack/utils'

module Lookout::Rack::Utils
  # Statsd proxy.  This class initializes the Statsd client and
  # delegates all stats related calls to it. It uses Lookout's statsd
  # (https://github.com/lookout/statsd) as a wrapper, which can be
  # setup to use another implementation under the hood. Call
  # +Lookout::Statsd.set_instance+ BEFORE referencing the +instance+
  # here in order to use a different Statsd implementation.
  #
  # Use as:
  #   Lookout::Rack::Utils::Graphite.increment('device.associated')
  #   Lookout::Rack::Utils::Graphite.update_counter('device.associated', 5)
  #   Lookout::Rack::Utils::Graphite.timing('device.associated', 3305)
  #   Lookout::Rack::Utils::Graphite.time('device.associated') do
  #     # work
  #   end
  #
  class Graphite
    # Private Constructor -- treat this class like the Singleton it used to be
    def initialize
      if !Lookout::Statsd.instance_set?
        prefix = configatron.statsd.prefix
        unless ENV['RACK_ENV'] == 'production'
          prefix = "dev.#{ENV['USER']}.#{prefix}"
        end

        Lookout::Statsd.create_instance(:host => configatron.statsd.host,
                                        :port => configatron.statsd.port,
                                        :prefix => prefix)
      end
    end

    # Provide same interface as old Singleton, but in test-friendly manner
    def self.instance
      @instance ||= new
    end

    # Clear instance, for use in testing only
    def self.clear_instance
      @instance = nil
    end

    def self.method_missing(meth, *args, &block)
      self.instance && Lookout::Statsd.instance.send(meth, *args, &block)
    end

    def self.respond_to?(method, include_private = false)
      super || (self.instance && Lookout::Statsd.instance.respond_to?(method, include_private))
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lookout-rack-utils-5.0.0.49 lib/lookout/rack/utils/graphite.rb
lookout-rack-utils-4.0.0.44 lib/lookout/rack/utils/graphite.rb
lookout-rack-utils-3.8.0.39 lib/lookout/rack/utils/graphite.rb
lookout-rack-utils-3.7.0.37 lib/lookout/rack/utils/graphite.rb
lookout-rack-utils-3.7.0.34 lib/lookout/rack/utils/graphite.rb