optionally don't append hostname to statsd keys

test plan: add "append_hostname: false" to your statsd config and
generate statsd metrics by visiting canvas pages. no hostname should be
appended to the key, so all servers in the environment will write to the
same metric.

Change-Id: I4662601ae04661aee2a75b39eeb58c7902870a38
Reviewed-on: https://gerrit.instructure.com/19342
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: Clare Hetherington <clare@instructure.com>
Product-Review: Brian Palmer <brianp@instructure.com>
This commit is contained in:
Brian Palmer 2013-04-04 10:11:17 -06:00
parent 8ce983ecd7
commit 8aefb867c4
3 changed files with 33 additions and 9 deletions

View File

@ -2,3 +2,4 @@ development:
#host: my.statsd.host
#port: 1234
#namespace: canvas
#append_hostname: false

View File

@ -29,7 +29,7 @@
# At least a host needs to be defined for the environment, all other config is optional
#
# If a namespace is defined in statsd.yml, it'll be prepended to the stat name.
# The hostname of the server will be appended to the stat name.
# The hostname of the server will be appended to the stat name, unless `append_hostname: false` is specified in the config.
# So if the namespace is "canvas" and the hostname is "app01", the final stat name of "my_stat" would be "stats.canvas.my_stat.app01"
# (assuming the default statsd/graphite configuration)
#
@ -43,7 +43,12 @@ module Canvas::Statsd
class_eval <<-RUBY, __FILE__, __LINE__+1
def self.#{method}(stat, *args)
if self.instance
self.instance.#{method}("\#{stat}.\#{hostname}", *args)
if self.append_hostname?
stat_name = "\#{stat}.\#{hostname}"
else
stat_name = stat.to_s
end
self.instance.#{method}(stat_name, *args)
else
nil
end
@ -55,6 +60,7 @@ module Canvas::Statsd
start = Time.now
result = yield
self.timing(stat, ((Time.now - start) * 1000).round, sample_rate)
result
end
def self.instance
@ -65,6 +71,7 @@ module Canvas::Statsd
@statsd = ::Statsd.new(statsd_settings[:host])
@statsd.port = statsd_settings[:port] if statsd_settings[:port]
@statsd.namespace = statsd_settings[:namespace] if statsd_settings[:namespace]
@append_hostname = !statsd_settings.key?(:append_hostname) || !!statsd_settings[:append_hostname]
else
@statsd = nil
end
@ -72,6 +79,10 @@ module Canvas::Statsd
@statsd
end
def self.append_hostname?
@append_hostname
end
def self.reset_instance
remove_instance_variable(:@statsd) if defined?(@statsd)
end

View File

@ -21,16 +21,30 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
describe "Canvas::Statsd" do
METHODS = %w(increment decrement count gauge timing)
it "should append the hostname to stat names" do
it "should append the hostname to stat names by default" do
Canvas::Statsd.stubs(:hostname).returns("testhost")
statsd = mock()
Canvas::Statsd.stubs(:instance).returns(statsd)
Canvas::Statsd.stubs(:append_hostname?).returns(true)
METHODS.each do |method|
statsd.expects(method).with("test.name.testhost")
Canvas::Statsd.send(method, "test.name")
statsd.expects(method).with("test.name.testhost", "test")
Canvas::Statsd.send(method, "test.name", "test")
end
statsd.expects("timing").with("test.name.testhost", anything, anything)
Canvas::Statsd.time("test.name") { }
Canvas::Statsd.time("test.name") { "test" }.should == "test"
end
it "should omit hostname if specified in config" do
Canvas::Statsd.expects(:hostname).never
statsd = mock()
Canvas::Statsd.stubs(:instance).returns(statsd)
Canvas::Statsd.stubs(:append_hostname?).returns(false)
METHODS.each do |method|
statsd.expects(method).with("test.name", "test")
Canvas::Statsd.send(method, "test.name", "test")
end
statsd.expects("timing").with("test.name", anything, anything)
Canvas::Statsd.time("test.name") { "test" }.should == "test"
end
it "should ignore all calls if statsd isn't enabled" do
@ -38,9 +52,7 @@ describe "Canvas::Statsd" do
METHODS.each do |method|
Canvas::Statsd.send(method, "test.name").should be_nil
end
called = false
Canvas::Statsd.time("test.name") { called = true }.should be_nil
called.should == true
Canvas::Statsd.time("test.name") { "test" }.should == "test"
end
it "should configure a statsd instance" do