Permalink
Browse files

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>
  • Loading branch information...
1 parent 8ce983e commit 8aefb867c496cad91d9fed8d6dbd733c9fe23b20 @codekitchen codekitchen committed Apr 4, 2013
Showing with 33 additions and 9 deletions.
  1. +1 −0 config/statsd.yml.example
  2. +13 −2 lib/canvas/statsd.rb
  3. +19 −7 spec/lib/canvas/statsd_spec.rb
@@ -2,3 +2,4 @@ development:
#host: my.statsd.host
#port: 1234
#namespace: canvas
+ #append_hostname: false
View
@@ -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 @@ def self.hostname
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 @@ def self.time(stat, sample_rate=1)
start = Time.now
result = yield
self.timing(stat, ((Time.now - start) * 1000).round, sample_rate)
+ result
end
def self.instance
@@ -65,13 +71,18 @@ def self.instance
@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
end
@statsd
end
+ def self.append_hostname?
+ @append_hostname
+ end
+
def self.reset_instance
remove_instance_variable(:@statsd) if defined?(@statsd)
end
@@ -21,26 +21,38 @@
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
Canvas::Statsd.stubs(:instance).returns(nil)
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

0 comments on commit 8aefb86

Please sign in to comment.