From 36d22ad221e2c9e0598dafd573a4871003f2ecca Mon Sep 17 00:00:00 2001 From: Tymon Tobolski Date: Fri, 8 Mar 2013 15:25:16 +0100 Subject: [PATCH] New configuration options: "use_middleware" and "use_subscribers" --- lib/librato/rails.rb | 7 +- lib/librato/rails/configuration.rb | 4 +- lib/librato/rails/logging.rb | 4 +- lib/librato/rails/subscribers.rb | 104 +++++++++++++++-------------- test/support/integration_case.rb | 4 +- 5 files changed, 67 insertions(+), 56 deletions(-) diff --git a/lib/librato/rails.rb b/lib/librato/rails.rb index 2608a01..c54e805 100644 --- a/lib/librato/rails.rb +++ b/lib/librato/rails.rb @@ -33,10 +33,14 @@ module Rails mattr_accessor :token mattr_accessor :flush_interval mattr_accessor :source_pids + mattr_accessor :use_middleware + mattr_accessor :use_subscribers # config defaults self.flush_interval = 60 # seconds self.source_pids = false # append process id to the source? + self.use_middleware = true + self.use_subscribers = true # log_level (default :info) # source (default: your machine's hostname) @@ -100,7 +104,8 @@ def setup(app) log :info, "starting up with #{app_server}..." end @pid = $$ - app.middleware.use Librato::Rack::Middleware + app.middleware.use Librato::Rack::Middleware if self.use_middleware + enable_subscribers! if self.use_subscribers start_worker unless forking_server? end diff --git a/lib/librato/rails/configuration.rb b/lib/librato/rails/configuration.rb index 500788f..a863d23 100644 --- a/lib/librato/rails/configuration.rb +++ b/lib/librato/rails/configuration.rb @@ -1,7 +1,7 @@ module Librato module Rails module Configuration - CONFIG_SETTABLE = %w{user token flush_interval log_level prefix source source_pids} + CONFIG_SETTABLE = %w{user token flush_interval log_level prefix source source_pids use_middleware use_subscribers} mattr_accessor :config_file self.config_file = 'config/librato.yml' @@ -49,4 +49,4 @@ def configure_with_environment end end -end \ No newline at end of file +end diff --git a/lib/librato/rails/logging.rb b/lib/librato/rails/logging.rb index c470f3d..05bd009 100644 --- a/lib/librato/rails/logging.rb +++ b/lib/librato/rails/logging.rb @@ -68,7 +68,9 @@ def trace_settings :qualified_source => qualified_source, :log_level => log_level, :prefix => prefix, - :flush_interval => self.flush_interval + :flush_interval => self.flush_interval, + :use_middleware => self.use_middleware, + :use_subscribers => self.use_subscribers } log :info, 'Settings: ' + settings.pretty_inspect end diff --git a/lib/librato/rails/subscribers.rb b/lib/librato/rails/subscribers.rb index 49a912e..617492c 100644 --- a/lib/librato/rails/subscribers.rb +++ b/lib/librato/rails/subscribers.rb @@ -3,75 +3,77 @@ module Rails # controllers - ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args| + def self.enable_subscribers! - event = ActiveSupport::Notifications::Event.new(*args) - controller = event.payload[:controller] - action = event.payload[:action] + ActiveSupport::Notifications.subscribe /process_action.action_controller/ do |*args| - format = event.payload[:format] || "all" - format = "all" if format == "*/*" - status = event.payload[:status] - exception = event.payload[:exception] - # page_key = "request.#{controller}.#{action}_#{format}." + event = ActiveSupport::Notifications::Event.new(*args) + controller = event.payload[:controller] + action = event.payload[:action] - group "rails.request" do |r| + format = event.payload[:format] || "all" + format = "all" if format == "*/*" + status = event.payload[:status] + exception = event.payload[:exception] + # page_key = "request.#{controller}.#{action}_#{format}." - r.increment 'total' - r.timing 'time', event.duration + group "rails.request" do |r| - if exception - r.increment 'exceptions' - else - r.timing 'time.db', event.payload[:db_runtime] || 0 - r.timing 'time.view', event.payload[:view_runtime] || 0 - end + r.increment 'total' + r.timing 'time', event.duration - unless status.blank? - r.group 'status' do |s| - s.increment status - s.increment "#{status.to_s[0]}xx" - s.timing "#{status}.time", event.duration - s.timing "#{status.to_s[0]}xx.time", event.duration + if exception + r.increment 'exceptions' + else + r.timing 'time.db', event.payload[:db_runtime] || 0 + r.timing 'time.view', event.payload[:view_runtime] || 0 end - end - r.increment 'slow' if event.duration > 200.0 - end # end group + unless status.blank? + r.group 'status' do |s| + s.increment status + s.increment "#{status.to_s[0]}xx" + s.timing "#{status}.time", event.duration + s.timing "#{status.to_s[0]}xx.time", event.duration + end + end - end # end subscribe + r.increment 'slow' if event.duration > 200.0 + end # end group - # SQL + end # end subscribe - ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args| - payload = args.last + # SQL - group "rails.sql" do |s| - # puts (event.payload[:name] || 'nil') + ":" + event.payload[:sql] + "\n" - s.increment 'queries' + ActiveSupport::Notifications.subscribe 'sql.active_record' do |*args| + payload = args.last - sql = payload[:sql].strip - s.increment 'selects' if sql.starts_with?('SELECT') - s.increment 'inserts' if sql.starts_with?('INSERT') - s.increment 'updates' if sql.starts_with?('UPDATE') - s.increment 'deletes' if sql.starts_with?('DELETE') + group "rails.sql" do |s| + # puts (event.payload[:name] || 'nil') + ":" + event.payload[:sql] + "\n" + s.increment 'queries' + + sql = payload[:sql].strip + s.increment 'selects' if sql.starts_with?('SELECT') + s.increment 'inserts' if sql.starts_with?('INSERT') + s.increment 'updates' if sql.starts_with?('UPDATE') + s.increment 'deletes' if sql.starts_with?('DELETE') + end end - end - # ActionMailer + # ActionMailer - ActiveSupport::Notifications.subscribe 'deliver.action_mailer' do |*args| - # payload[:mailer] => 'UserMailer' - group "rails.mail" do |m| - m.increment 'sent' + ActiveSupport::Notifications.subscribe 'deliver.action_mailer' do |*args| + # payload[:mailer] => 'UserMailer' + group "rails.mail" do |m| + m.increment 'sent' + end end - end - ActiveSupport::Notifications.subscribe 'receive.action_mailer' do |*args| - group "rails.mail" do |m| - m.increment 'received' + ActiveSupport::Notifications.subscribe 'receive.action_mailer' do |*args| + group "rails.mail" do |m| + m.increment 'received' + end end end - end -end \ No newline at end of file +end diff --git a/test/support/integration_case.rb b/test/support/integration_case.rb index 0e15fc5..05569cc 100644 --- a/test/support/integration_case.rb +++ b/test/support/integration_case.rb @@ -1,7 +1,9 @@ class ActiveSupport::IntegrationCase < ActiveSupport::TestCase include Capybara::DSL include Rails.application.routes.url_helpers - + + Librato::Rails.enable_subscribers! + setup do # remove any accumulated metrics Librato::Rails.delete_all