(RE-7014) Add support for statsd

They way we were using graphite was incorrect for the type of data we were sending it.  statsd is the appropriate mechanism for our needs.
statsd and graphite are mutually exclusive and configuring statsd will take precendence over Graphite.  Example of configuration in vmpooler.yaml.example
This commit is contained in:
Rick Sherman 2016-05-10 12:47:01 -05:00
parent 5aaab7c5c2
commit 8d75865a5c
8 changed files with 143 additions and 7 deletions

View file

@ -7,6 +7,7 @@ module Vmpooler
require 'rbvmomi'
require 'redis'
require 'sinatra/base'
require "statsd-ruby"
require 'time'
require 'timeout'
require 'yaml'
@ -52,6 +53,13 @@ module Vmpooler
parsed_config[:graphite]['prefix'] ||= 'vmpooler'
end
# statsd is an addition and my not be present in YAML configuration
if parsed_config[:statsd]
if parsed_config[:statsd]['server']
parsed_config[:statsd]['prefix'] ||= 'vmpooler'
end
end
if parsed_config[:tagfilter]
parsed_config[:tagfilter].keys.each do |tag|
parsed_config[:tagfilter][tag] = Regexp.new(parsed_config[:tagfilter][tag])
@ -79,6 +87,14 @@ module Vmpooler
end
end
def self.new_statsd(server, port)
if server.nil? or server.empty? or server.length == 0
nil
else
Statsd.new server, port
end
end
def self.pools(conf)
conf[:pools]
end

View file

@ -1,12 +1,17 @@
module Vmpooler
class PoolManager
def initialize(config, logger, redis, graphite=nil)
def initialize(config, logger, redis, graphite=nil, statsd=nil)
$config = config
# Load logger library
$logger = logger
unless graphite.nil?
# statsd and graphite are mutex in the context of vmpooler
unless statsd.nil?
$statsd = statsd
end
unless graphite.nil? || !statsd.nil?
$graphite = graphite
end
@ -258,6 +263,7 @@ module Vmpooler
$redis.decr('vmpooler__tasks__clone')
begin
$statsd.timing($config[:statsd]['prefix'] + ".clone.#{vm['template']}", finish) if defined? $statsd
$graphite.log($config[:graphite]['prefix'] + ".clone.#{vm['template']}", finish) if defined? $graphite
rescue
end
@ -565,7 +571,10 @@ module Vmpooler
total = $redis.scard('vmpooler__pending__' + pool['name']) + ready
begin
if defined? $graphite
if defined? $statsd
$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']))
elsif defined? $graphite
$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']))
end

12
lib/vmpooler/statsd.rb Normal file
View file

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