Permalink
Browse files

Allow basic metric recording to be disabled via config.disable_rack_m…

…etrics
  • Loading branch information...
1 parent 2f0307c commit b006685523d431f8d18088acd80112c38cc2755e @nextmat nextmat committed Mar 20, 2013
Showing with 77 additions and 2 deletions.
  1. +3 −1 lib/librato/rack.rb
  2. +2 −1 lib/librato/rack/configuration.rb
  3. +18 −0 test/apps/no_stats.ru
  4. +54 −0 test/integration/no_stats_test.rb
View
@@ -24,7 +24,7 @@ module Librato
#
# app = Rack::Builder.app do
# use Librato::Rack
- # run lambda { |env| [200, {"Content-Type" => 'text/html'}, ["Hello!"]]}
+ # run lambda { |env| [200, {"Content-Type" => 'text/html'}, ["Hello!"]] }
# end
#
class Rack
@@ -86,6 +86,7 @@ def record_header_metrics(env)
end
def record_request_metrics(status, duration)
+ return if config.disable_rack_metrics
tracker.group 'rack.request' do |group|
group.increment 'total'
group.timing 'time', duration
@@ -102,6 +103,7 @@ def record_request_metrics(status, duration)
end
def record_exception(exception)
+ return if config.disable_rack_metrics
tracker.increment 'rack.request.exceptions'
end
@@ -11,7 +11,8 @@ class Rack
#
class Configuration
attr_accessor :user, :token, :api_endpoint, :tracker, :source_pids,
- :log_level, :flush_interval, :log_target
+ :log_level, :flush_interval, :log_target,
+ :disable_rack_metrics
attr_reader :prefix, :source
def initialize
View
@@ -0,0 +1,18 @@
+require 'bundler/setup'
+require 'librato-rack'
+
+config = Librato::Rack::Configuration.new
+config.disable_rack_metrics = true
+
+use Librato::Rack, :config => config
+
+def application(env)
+ case env['PATH_INFO']
+ when '/exception'
+ raise 'exception raised!'
+ else
+ [200, {"Content-Type" => 'text/html'}, ["Hello!"]]
+ end
+end
+
+run method(:application)
@@ -0,0 +1,54 @@
+require 'test_helper'
+require 'rack/test'
+
+# Tests for universal tracking for all request paths
+#
+class NoStatsTest < MiniTest::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ Rack::Builder.parse_file('test/apps/no_stats.ru').first
+ end
+
+ def teardown
+ # clear metrics before each run
+ aggregate.delete_all
+ counters.delete_all
+ end
+
+ def test_no_standard_counters
+ get '/'
+ assert last_response.ok?
+
+ assert_equal nil, counters["rack.request.total"]
+ assert_equal nil, counters["rack.request.status.200"]
+ assert_equal nil, counters["rack.request.status.2xx"]
+ end
+
+ def test_no_standard_measures
+ get '/'
+ assert last_response.ok?
+
+ assert_equal nil, aggregate["rack.request.time"]
+ end
+
+ def test_dont_track_exceptions
+ begin
+ get '/exception'
+ rescue RuntimeError => e
+ raise unless e.message == 'exception raised!'
+ end
+ assert_equal nil, counters["rack.request.exceptions"]
+ end
+
+ private
+
+ def aggregate
+ Librato.tracker.collector.aggregate
+ end
+
+ def counters
+ Librato.tracker.collector.counters
+ end
+
+end

0 comments on commit b006685

Please sign in to comment.