From 8aefb867c496cad91d9fed8d6dbd733c9fe23b20 Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Thu, 4 Apr 2013 10:11:17 -0600 Subject: [PATCH] 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 Reviewed-by: Cody Cutrer QA-Review: Clare Hetherington Product-Review: Brian Palmer --- config/statsd.yml.example | 1 + lib/canvas/statsd.rb | 15 +++++++++++++-- spec/lib/canvas/statsd_spec.rb | 26 +++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/config/statsd.yml.example b/config/statsd.yml.example index 0bcaa77956f..da9a7eb2001 100644 --- a/config/statsd.yml.example +++ b/config/statsd.yml.example @@ -2,3 +2,4 @@ development: #host: my.statsd.host #port: 1234 #namespace: canvas + #append_hostname: false diff --git a/lib/canvas/statsd.rb b/lib/canvas/statsd.rb index 96cc2459c5e..91d9bf13275 100644 --- a/lib/canvas/statsd.rb +++ b/lib/canvas/statsd.rb @@ -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 diff --git a/spec/lib/canvas/statsd_spec.rb b/spec/lib/canvas/statsd_spec.rb index b935d735450..13d793feb25 100644 --- a/spec/lib/canvas/statsd_spec.rb +++ b/spec/lib/canvas/statsd_spec.rb @@ -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