From 13aa40e5fec00c019afe450cfe3eb46d3c38ab08 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Mon, 21 Jun 2021 15:44:14 +0100 Subject: [PATCH] Fix possible NoMethodError in railtie This can happen if Bugsnag is added to the Gemfile with 'require: false'. In this case, 'require "bugsnag"' in a Rails initializer will run the 'config.before_initialize' before the rest of the Railtie has been executed. This results in the 'event_subscription' method being undefined as it was declared below the line that calls it By moving it to the top of the file this is no longer a problem --- lib/bugsnag/integrations/railtie.rb | 69 ++++++++++++++--------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/lib/bugsnag/integrations/railtie.rb b/lib/bugsnag/integrations/railtie.rb index c43f738b..c11fc216 100644 --- a/lib/bugsnag/integrations/railtie.rb +++ b/lib/bugsnag/integrations/railtie.rb @@ -9,11 +9,44 @@ module Bugsnag class Railtie < ::Rails::Railtie - FRAMEWORK_ATTRIBUTES = { :framework => "Rails" } + ## + # Subscribes to an ActiveSupport event, leaving a breadcrumb when it triggers + # + # @api private + # @param event [Hash] details of the event to subscribe to + def event_subscription(event) + ActiveSupport::Notifications.subscribe(event[:id]) do |*, event_id, data| + filtered_data = data.slice(*event[:allowed_data]) + filtered_data[:event_name] = event[:id] + filtered_data[:event_id] = event_id + + if event[:id] == "sql.active_record" + if data.key?(:binds) + binds = data[:binds].each_with_object({}) { |bind, output| output[bind.name] = '?' if defined?(bind.name) } + filtered_data[:binds] = JSON.dump(binds) unless binds.empty? + end + + # Rails < 6.1 included connection_id in the event data, but now + # includes the connection object instead + if data.key?(:connection) && !data.key?(:connection_id) + # the connection ID is the object_id of the connection object + filtered_data[:connection_id] = data[:connection].object_id + end + end + + Bugsnag.leave_breadcrumb( + event[:message], + filtered_data, + event[:type], + :auto + ) + end + end + rake_tasks do require "bugsnag/integrations/rake" load "bugsnag/tasks/bugsnag.rake" @@ -80,39 +113,5 @@ class Railtie < ::Rails::Railtie Bugsnag.configuration.warn("Unable to add Bugsnag::Rack middleware as the middleware stack is frozen") end end - - ## - # Subscribes to an ActiveSupport event, leaving a breadcrumb when it triggers - # - # @api private - # @param event [Hash] details of the event to subscribe to - def event_subscription(event) - ActiveSupport::Notifications.subscribe(event[:id]) do |*, event_id, data| - filtered_data = data.slice(*event[:allowed_data]) - filtered_data[:event_name] = event[:id] - filtered_data[:event_id] = event_id - - if event[:id] == "sql.active_record" - if data.key?(:binds) - binds = data[:binds].each_with_object({}) { |bind, output| output[bind.name] = '?' if defined?(bind.name) } - filtered_data[:binds] = JSON.dump(binds) unless binds.empty? - end - - # Rails < 6.1 included connection_id in the event data, but now - # includes the connection object instead - if data.key?(:connection) && !data.key?(:connection_id) - # the connection ID is the object_id of the connection object - filtered_data[:connection_id] = data[:connection].object_id - end - end - - Bugsnag.leave_breadcrumb( - event[:message], - filtered_data, - event[:type], - :auto - ) - end - end end end