[QENG-4075] Refactor statsd methods / classes

Prior to this we could easily run into situations where `statds_prefix` would
be `nil` (and possibly the `statsd` handle itself). There was some significant
complexity and brittleness in how statsd was set up.

Refactored so that:

 - `statsd_prefix` is no longer exposed to any callers of statsd methods
 - there is now a `Vmpooler::DummyStatsd` class which can be returned when we are not actually going to publish stats, but would like to keep the calling interface consistent
 - setup of the statsd handle is via just passing in `config[:statsd]`, if `nil`, this will result in a dummy handle being return
 - defaulting of `server` values was fixed -- this did not actually work in the previous implementation. `config[:statsd][:server]` is now required.
 - tests use a `DummyStatsd` instance instead of an rspec double.
 - calls to `statsd.increment` were taking incorrect arguments (some our fault, some part of the prior implementation), and were not collecting data on which pools were "invalid" or "empty". Fixed this and are now explicitly tracking the invalid/empty pool names.
This commit is contained in:
Rick Bradley 2016-07-12 13:15:48 -05:00
parent f45cf10839
commit e9fae3fab2
7 changed files with 54 additions and 32 deletions

View file

@ -92,12 +92,9 @@ module Vmpooler
end
end
def self.new_statsd(server, port)
if server.nil? || server.empty?
nil
else
Statsd.new server, port
end
def self.new_statsd(params)
return DummyStatsd.new unless params
Statsd.new params
end
def self.pools(conf)

View file

@ -16,12 +16,6 @@ module Vmpooler
Vmpooler::API.settings.statsd
end
def statsd_prefix
if Vmpooler::API.settings.statsd
Vmpooler::API.settings.config[:statsd]['prefix'] ? Vmpooler::API.settings.config[:statsd]['prefix'] : 'vmpooler'
end
end
def config
Vmpooler::API.settings.config[:config]
end
@ -98,11 +92,11 @@ module Vmpooler
vm, name = fetch_single_vm(requested)
if !vm
failed = true
statsd.increment(statsd_prefix + '.checkout.empty.' + requested, 1)
statsd.increment('checkout.empty.' + requested)
break
else
vms << [ name, vm ]
statsd.increment(statsd_prefix + '.checkout.success.' + name, 1)
statsd.increment('checkout.success.' + name)
end
end
end
@ -388,7 +382,7 @@ module Vmpooler
result = atomically_allocate_vms(payload)
else
invalid.each do |bad_template|
statsd.increment(statsd_prefix + '.checkout.invalid', bad_template)
statsd.increment('checkout.invalid.' + bad_template)
end
status 404
end
@ -430,7 +424,7 @@ module Vmpooler
result = atomically_allocate_vms(payload)
else
invalid.each do |bad_template|
statsd.increment(statsd_prefix + '.checkout.invalid', bad_template)
statsd.increment('checkout.invalid.' + bad_template)
end
status 404
end

View file

@ -2,8 +2,48 @@ require 'rubygems' unless defined?(Gem)
module Vmpooler
class Statsd
def initialize(server = 'statsd', port = 8125)
@server = Statsd.new(server, port)
attr_reader :server, :port, :prefix
def initialize(params = {})
if params[:server].nil? || params[:server].empty?
raise ArgumentError, "Statsd server is required. Config: #{params.inspect}"
end
host = params[:server]
port = params[:port] || 8125
@prefix = params[:prefix] || 'vmpooler'
@server = Statsd.new(host, port)
end
def increment(label)
server.increment(prefix + "." + label)
end
def gauge(label, value)
server.gauge(prefix + "." + label, value)
end
def timing(label, duration)
server.timing(prefix + "." + label, duration)
end
end
class DummyStatsd
attr_reader :server, :port, :prefix
def initialize(params = {})
end
def increment(label)
true
end
def gauge(label, value)
true
end
def timing(label, duration)
true
end
end
end