Class: Napa::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/napa/stats.rb

Class Method Summary collapse

Class Method Details

.emitterObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/napa/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'
    Napa::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) ⇒ Object



5
6
7
# File 'lib/napa/stats.rb', line 5

def emitter=(emitter)
  @emitter = emitter
end

.namespaceObject



22
23
24
25
26
27
28
29
30
# File 'lib/napa/stats.rb', line 22

def namespace
  environment = ENV['RACK_ENV'] || 'development'

  if ENV['STATSD_API_KEY'].present?
    "#{ENV['STATSD_API_KEY']}.#{Napa::Identity.name}.#{environment}"
  else
    "#{Napa::Identity.name}.#{environment}"
  end
end

.path_to_key(method, path) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/napa/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