class ServiceTemplate::Stats

Public Class Methods

emitter() click to toggle source
# File lib/service_template/stats.rb, line 9
def emitter
  unless @emitter
    # Log an error if StatsD settings are not configured
    message = 'StatsD host and port not configured in environment variables, using default settings'
    ServiceTemplate::Logger.logger.warn message unless ENV['STATSD_HOST'] && ENV['STATSD_PORT']

    # Create a new StatsD emitter with the service name as the namespace
    # Defaults to localhost port 8125 if env vars are nil
    @emitter = Statsd.new(ENV['STATSD_HOST'], ENV['STATSD_PORT']).tap { |sd| sd.namespace = namespace }
  end
  @emitter
end
emitter=(emitter) click to toggle source
# File lib/service_template/stats.rb, line 5
def emitter=(emitter)
  @emitter = emitter
end
namespace() click to toggle source
# File lib/service_template/stats.rb, line 22
def namespace
  environment = ENV['RACK_ENV'] || 'development'

  if ENV['STATSD_API_KEY'].present?
    "#{ENV['STATSD_API_KEY']}.#{ServiceTemplate::Identity.name}.#{environment}"
  else
    "#{ServiceTemplate::Identity.name}.#{environment}"
  end
end
path_to_key(method, path) click to toggle source
# File lib/service_template/stats.rb, line 32
def path_to_key(method, path)
  # split the path on forward slash
  # remove any elements that are empty
  # replace any number strings with _
  # join all parts with a .
  # prepend with the method
  # downcase the whole thing
  "#{method}.#{path.split(/\//).reject{|p| p.empty?}.collect{|p| p.gsub(/\d+/,'_')}.join('.')}".downcase
end