Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rack/middleware: fix performance stat sending #901

Merged
merged 1 commit into from Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,9 @@ Airbrake Changelog

### master

* Fixed performance stats not being sent
([#901](https://github.com/airbrake/airbrake/pull/901))

### [v8.1.1][v8.1.1] (February 14, 2019)

* Fixed `uninitialized constant Airbrake::Rack::Middleware::ActiveRecord` for
Expand Down
15 changes: 12 additions & 3 deletions lib/airbrake/rack/middleware.rb
Expand Up @@ -34,6 +34,10 @@ def initialize(app, notifier_name = :default)
@notice_notifier = Airbrake.notifiers[:notice][notifier_name]
@performance_notifier = Airbrake.notifiers[:performance][notifier_name]

# This object is shared among hooks. ActionControllerRouteSubscriber
# writes it and other hooks read it.
@routes = {}

# Prevent adding same filters to the same notifier.
return if @@known_notifiers.include?(notifier_name)
@@known_notifiers << notifier_name
Expand All @@ -57,6 +61,9 @@ def call(env)
rescue Exception => ex
notify_airbrake(ex, env)
raise ex
ensure
# Clear routes for the next request.
@routes.clear
end
# rubocop:enable Lint/RescueException

Expand Down Expand Up @@ -101,13 +108,13 @@ def framework_exception(env)
def install_action_controller_hooks
ActiveSupport::Notifications.subscribe(
'start_processing.action_controller',
Airbrake::Rails::ActionControllerRouteSubscriber.new
Airbrake::Rails::ActionControllerRouteSubscriber.new(@routes)
)

ActiveSupport::Notifications.subscribe(
'process_action.action_controller',
Airbrake::Rails::ActionControllerNotifySubscriber.new(
@performance_notifier
@performance_notifier, @routes
)
)
end
Expand All @@ -120,7 +127,9 @@ def install_active_record_hooks
)
ActiveSupport::Notifications.subscribe(
'sql.active_record',
Airbrake::Rails::ActiveRecordSubscriber.new(@performance_notifier)
Airbrake::Rails::ActiveRecordSubscriber.new(
@performance_notifier, @routes
)
)
end
end
Expand Down
23 changes: 13 additions & 10 deletions lib/airbrake/rails/action_controller_notify_subscriber.rb
Expand Up @@ -5,25 +5,28 @@ module Rails
#
# @since v8.0.0
class ActionControllerNotifySubscriber
def initialize(notifier)
def initialize(notifier, routes)
@notifier = notifier
@routes = routes
end

def call(*args)
return unless (route = Thread.current[:airbrake_rails_route])
return if @routes.none?

event = ActiveSupport::Notifications::Event.new(*args)
payload = event.payload

@notifier.notify(
Airbrake::Request.new(
method: payload[:method],
route: route,
status_code: find_status_code(payload),
start_time: event.time,
end_time: Time.new
@routes.each do |route, method|
@notifier.notify(
Airbrake::Request.new(
method: method,
route: route,
status_code: find_status_code(payload),
start_time: event.time,
end_time: Time.new
)
)
)
end
end

private
Expand Down
6 changes: 3 additions & 3 deletions lib/airbrake/rails/action_controller_route_subscriber.rb
Expand Up @@ -5,7 +5,8 @@ module Rails
#
# @since v8.0.0
class ActionControllerRouteSubscriber
def initialize
def initialize(routes)
@routes = routes
@all_routes = nil
end

Expand All @@ -17,8 +18,7 @@ def call(*args)
event = ActiveSupport::Notifications::Event.new(*args)
payload = event.payload

Thread.current[:airbrake_rails_route] = find_route(payload[:params])
Thread.current[:airbrake_rails_method] = payload[:method]
@routes[find_route(payload[:params])] = payload[:method]
end

private
Expand Down
21 changes: 12 additions & 9 deletions lib/airbrake/rails/active_record_subscriber.rb
Expand Up @@ -4,21 +4,24 @@ module Rails
#
# @since v8.1.0
class ActiveRecordSubscriber
def initialize(notifier)
def initialize(notifier, routes)
@notifier = notifier
@routes = routes
end

def call(*args)
event = ActiveSupport::Notifications::Event.new(*args)
@notifier.notify(
Airbrake::Query.new(
route: Thread.current[:airbrake_rails_route],
method: Thread.current[:airbrake_rails_method],
query: event.payload[:sql],
start_time: event.time,
end_time: event.end
@routes.each do |route, method|
@notifier.notify(
Airbrake::Query.new(
route: route,
method: method,
query: event.payload[:sql],
start_time: event.time,
end_time: event.end
)
)
)
end
end
end
end
Expand Down