Skip to content

Commit

Permalink
rails: load performance hooks only if performance_stats is enabled
Browse files Browse the repository at this point in the history
We load these hooks unnecessarily for those customers who don't use performance
stats. It may cause an unwanted overhead for them: we spend CPU time collecting
info but never send it. In this commit we check if performance_stats is enabled
and include the hooks only if it is.
  • Loading branch information
kyrylo committed Mar 28, 2019
1 parent 136e5e7 commit a5a8e16
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
53 changes: 36 additions & 17 deletions lib/airbrake/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ module Rails
# occurring in the application automatically.
class Railtie < ::Rails::Railtie
initializer('airbrake.middleware') do |app|
require 'airbrake/rails/action_controller_route_subscriber'
require 'airbrake/rails/action_controller_notify_subscriber'
require 'airbrake/rails/action_controller_performance_breakdown_subscriber'

ActiveSupport::Notifications.subscribe(
'process_action.action_controller',
Airbrake::Rails::ActionControllerPerformanceBreakdownSubscriber.new
)

require 'airbrake/rails/active_record_subscriber' if defined?(ActiveRecord)

# Since Rails 3.2 the ActionDispatch::DebugExceptions middleware is
# responsible for logging exceptions and showing a debugging page in
# case the request is local. We want to insert our middleware after
Expand Down Expand Up @@ -60,10 +49,28 @@ class Railtie < ::Rails::Railtie
require 'airbrake/rails/action_controller'
include Airbrake::Rails::ActionController

# Cache route information for the duration of the request.
require 'airbrake/rails/action_controller_route_subscriber'
# Send route stats.
require 'airbrake/rails/action_controller_notify_subscriber'
if Airbrake::Config.instance.performance_stats
# Cache route information for the duration of the request.
require 'airbrake/rails/action_controller_route_subscriber'
ActiveSupport::Notifications.subscribe(
'start_processing.action_controller',
Airbrake::Rails::ActionControllerRouteSubscriber.new
)

# Send route stats.
require 'airbrake/rails/action_controller_notify_subscriber'
ActiveSupport::Notifications.subscribe(
'process_action.action_controller',
Airbrake::Rails::ActionControllerNotifySubscriber.new
)

# Send performance breakdown: where a request spends its time.
require 'airbrake/rails/action_controller_performance_breakdown_subscriber'
ActiveSupport::Notifications.subscribe(
'process_action.action_controller',
Airbrake::Rails::ActionControllerPerformanceBreakdownSubscriber.new
)
end
end
end

Expand All @@ -74,8 +81,20 @@ class Railtie < ::Rails::Railtie
require 'airbrake/rails/active_record'
include Airbrake::Rails::ActiveRecord

# Send SQL queries.
require 'airbrake/rails/active_record_subscriber'
if defined?(ActiveRecord) && Airbrake::Config.instance.performance_stats
# Send SQL queries.
require 'airbrake/rails/active_record_subscriber'
ActiveSupport::Notifications.subscribe(
'sql.active_record', Airbrake::Rails::ActiveRecordSubscriber.new
)

# Filter out parameters from SQL body.
Airbrake.add_performance_filter(
Airbrake::Filters::SqlFilter.new(
::ActiveRecord::Base.connection_config[:adapter]
)
)
end
end
end

Expand Down
5 changes: 0 additions & 5 deletions lib/airbrake/rails/action_controller_notify_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,3 @@ def find_status_code(payload)
end
end
end

ActiveSupport::Notifications.subscribe(
'process_action.action_controller',
Airbrake::Rails::ActionControllerNotifySubscriber.new
)
5 changes: 0 additions & 5 deletions lib/airbrake/rails/action_controller_route_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,3 @@ def find_all_routes
end
end
end

ActiveSupport::Notifications.subscribe(
'start_processing.action_controller',
Airbrake::Rails::ActionControllerRouteSubscriber.new
)
10 changes: 0 additions & 10 deletions lib/airbrake/rails/active_record_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@ def last_caller
end
end
end

Airbrake.add_performance_filter(
Airbrake::Filters::SqlFilter.new(
ActiveRecord::Base.connection_config[:adapter]
)
)

ActiveSupport::Notifications.subscribe(
'sql.active_record', Airbrake::Rails::ActiveRecordSubscriber.new
)

0 comments on commit a5a8e16

Please sign in to comment.