/
statsd.rb
62 lines (51 loc) · 1.22 KB
/
statsd.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'capistrano/notifier'
require 'socket'
class Capistrano::Notifier::StatsD < Capistrano::Notifier::Base
DEFAULTS = { :host => "127.0.0.1", :port => "8125", :with => :counter }
def self.load_into(configuration)
configuration.load do
namespace :deploy do
namespace :notify do
desc 'Notify StatsD of deploy.'
task :statsd do
run Capistrano::Notifier::StatsD.new(configuration).perform
end
end
end
after 'deploy:restart', 'deploy:notify:statsd'
end
end
def perform
"echo #{packet.gsub('|', '\\|')} | nc -w 1 -u #{host} #{port}"
end
private
def host
options[:host]
end
def options
if cap.respond_to? :notifier_statsd_options
cap.notifier_statsd_options.reverse_merge DEFAULTS
else
DEFAULTS
end
end
def packet
if stage
"#{application}.#{stage}.deploy:#{with}"
else
"#{application}.deploy:#{with}"
end
end
def port
options[:port]
end
def with
case options[:with]
when :counter then "1|c"
when :gauge then "1|g"
end
end
end
if Capistrano::Configuration.instance
Capistrano::Notifier::StatsD.load_into(Capistrano::Configuration.instance)
end