(RE-7014) statsd nitpicks and additional rspec

Cleaned up some code review nitpicks and added pool_manager_spec for empty pool.
This commit is contained in:
Rick Sherman 2016-06-08 11:03:22 -05:00
parent b983472088
commit c133bed945
5 changed files with 39 additions and 23 deletions

View file

@ -88,7 +88,7 @@ module Vmpooler
end end
def self.new_statsd(server, port) def self.new_statsd(server, port)
if server.nil? or server.empty? or server.length == 0 if server.nil? || server.empty?
nil nil
else else
Statsd.new server, port Statsd.new server, port

View file

@ -18,7 +18,7 @@ module Vmpooler
def statsd_prefix def statsd_prefix
if Vmpooler::API.settings.statsd if Vmpooler::API.settings.statsd
Vmpooler::API.settings.config[:statsd]['prefix']? Vmpooler::API.settings.config[:statsd]['prefix'] : 'vmpooler' Vmpooler::API.settings.config[:statsd]['prefix'] ? Vmpooler::API.settings.config[:statsd]['prefix'] : 'vmpooler'
end end
end end
@ -393,9 +393,9 @@ module Vmpooler
if jdata if jdata
empty = jdata.delete('empty') empty = jdata.delete('empty')
invalid = jdata.delete('invalid') invalid = jdata.delete('invalid')
statsd.increment(statsd_prefix + '.checkout.empty', empty) if !empty.nil? statsd.increment(statsd_prefix + '.checkout.empty', empty) if empty
statsd.increment(statsd_prefix + '.checkout.invalid', invalid) if !invalid.nil? statsd.increment(statsd_prefix + '.checkout.invalid', invalid) if invalid
if !jdata.empty? unless jdata.empty?
result = atomically_allocate_vms(jdata) result = atomically_allocate_vms(jdata)
else else
status 404 status 404
@ -426,9 +426,9 @@ module Vmpooler
if payload if payload
empty = payload.delete('empty') empty = payload.delete('empty')
invalid = payload.delete('invalid') invalid = payload.delete('invalid')
statsd.increment(statsd_prefix + '.checkout.empty', empty) if !empty.nil? statsd.increment(statsd_prefix + '.checkout.empty', empty) if empty
statsd.increment(statsd_prefix + '.checkout.invalid', invalid) if !invalid.nil? statsd.increment(statsd_prefix + '.checkout.invalid', invalid) if invalid
if !payload.empty? unless payload.empty?
result = atomically_allocate_vms(payload) result = atomically_allocate_vms(payload)
else else
status 404 status 404

View file

@ -1,17 +1,15 @@
module Vmpooler module Vmpooler
class PoolManager class PoolManager
def initialize(config, logger, redis, graphite=nil, statsd=nil) def initialize(config, logger, redis, graphite = nil, statsd = nil)
$config = config $config = config
# Load logger library # Load logger library
$logger = logger $logger = logger
# statsd and graphite are mutex in the context of vmpooler # statsd and graphite are mutex in the context of vmpooler
unless statsd.nil? if statsd
$statsd = statsd $statsd = statsd
end elsif graphite
unless graphite.nil? || !statsd.nil?
$graphite = graphite $graphite = graphite
end end
@ -263,8 +261,8 @@ module Vmpooler
$redis.decr('vmpooler__tasks__clone') $redis.decr('vmpooler__tasks__clone')
begin begin
$statsd.timing($config[:statsd]['prefix'] + ".clone.#{vm['template']}", finish) if defined? $statsd $statsd.timing($config[:statsd]['prefix'] + ".clone.#{vm['template']}", finish) if $statsd
$graphite.log($config[:graphite]['prefix'] + ".clone.#{vm['template']}", finish) if defined? $graphite $graphite.log($config[:graphite]['prefix'] + ".clone.#{vm['template']}", finish) if $graphite
rescue rescue
end end
end end
@ -300,7 +298,7 @@ module Vmpooler
$logger.log('s', "[-] [#{pool}] '#{vm}' destroyed in #{finish} seconds") $logger.log('s', "[-] [#{pool}] '#{vm}' destroyed in #{finish} seconds")
$graphite.log($config[:graphite]['prefix'] + ".destroy.#{pool}", finish) if defined? $graphite $graphite.log($config[:graphite]['prefix'] + ".destroy.#{pool}", finish) if $graphite
end end
end end
end end
@ -571,10 +569,10 @@ module Vmpooler
total = $redis.scard('vmpooler__pending__' + pool['name']) + ready total = $redis.scard('vmpooler__pending__' + pool['name']) + ready
begin begin
if defined? $statsd if $statsd
$statsd.increment($config[:statsd]['prefix'] + '.ready.' + pool['name'], $redis.scard('vmpooler__ready__' + pool['name'])) $statsd.increment($config[:statsd]['prefix'] + '.ready.' + pool['name'], $redis.scard('vmpooler__ready__' + pool['name']))
$statsd.increment($config[:statsd]['prefix'] + '.running.' + pool['name'], $redis.scard('vmpooler__running__' + pool['name'])) $statsd.increment($config[:statsd]['prefix'] + '.running.' + pool['name'], $redis.scard('vmpooler__running__' + pool['name']))
elsif defined? $graphite elsif $graphite
$graphite.log($config[:graphite]['prefix'] + '.ready.' + pool['name'], $redis.scard('vmpooler__ready__' + pool['name'])) $graphite.log($config[:graphite]['prefix'] + '.ready.' + pool['name'], $redis.scard('vmpooler__ready__' + pool['name']))
$graphite.log($config[:graphite]['prefix'] + '.running.' + pool['name'], $redis.scard('vmpooler__running__' + pool['name'])) $graphite.log($config[:graphite]['prefix'] + '.running.' + pool['name'], $redis.scard('vmpooler__running__' + pool['name']))
end end

View file

@ -2,11 +2,8 @@ require 'rubygems' unless defined?(Gem)
module Vmpooler module Vmpooler
class Statsd class Statsd
def initialize( def initialize(server = 'statsd', port = 8125)
s = 'statsd', @server = Statsd.new(server, port)
port = 8125
)
@server = Statsd.new s, port
end end
end end
end end

View file

@ -286,6 +286,17 @@ describe 'Pool Manager' do
expect(graphite).to receive(:log).with('vmpooler.running.pool1', 5) expect(graphite).to receive(:log).with('vmpooler.running.pool1', 5)
subject._check_pool(config[:pools][0]) subject._check_pool(config[:pools][0])
end end
it 'increments graphite when ready with 0 when pool empty and statsd disabled' do
allow(redis).to receive(:scard).with('vmpooler__ready__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__cloning__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__pending__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__running__pool1').and_return(5)
expect(graphite).to receive(:log).with('vmpooler.ready.pool1', 0)
expect(graphite).to receive(:log).with('vmpooler.running.pool1', 5)
subject._check_pool(config[:pools][0])
end
end end
context 'statsd' do context 'statsd' do
@ -307,6 +318,16 @@ describe 'Pool Manager' do
expect(statsd).to receive(:increment).with('vmpooler.running.pool1', 5) expect(statsd).to receive(:increment).with('vmpooler.running.pool1', 5)
subject._check_pool(config[:pools][0]) subject._check_pool(config[:pools][0])
end end
it 'increments statsd ready with 0 when pool empty' do
allow(redis).to receive(:scard).with('vmpooler__running__pool1').and_return(1)
allow(redis).to receive(:scard).with('vmpooler__ready__pool1').and_return(0)
allow(redis).to receive(:scard).with('vmpooler__pending__pool1').and_return(0)
allow(statsd).to receive(:increment).with('vmpooler.running.pool1', 1)
expect(statsd).to receive(:increment).with('vmpooler.ready.pool1', 0)
subject._check_pool(config[:pools][0])
end
end end
end end