airbrake 9.4.3 → 13.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +5 -5
  2. data/lib/airbrake/capistrano/capistrano2.rb +3 -1
  3. data/lib/airbrake/capistrano/capistrano3.rb +3 -1
  4. data/lib/airbrake/capistrano.rb +2 -0
  5. data/lib/airbrake/delayed_job.rb +31 -15
  6. data/lib/airbrake/logger.rb +5 -1
  7. data/lib/airbrake/rack/context_filter.rb +13 -2
  8. data/lib/airbrake/rack/http_headers_filter.rb +7 -5
  9. data/lib/airbrake/rack/http_params_filter.rb +2 -0
  10. data/lib/airbrake/rack/instrumentable.rb +116 -4
  11. data/lib/airbrake/rack/middleware.rb +3 -1
  12. data/lib/airbrake/rack/request_body_filter.rb +2 -0
  13. data/lib/airbrake/rack/request_store.rb +2 -0
  14. data/lib/airbrake/rack/route_filter.rb +9 -11
  15. data/lib/airbrake/rack/session_filter.rb +2 -0
  16. data/lib/airbrake/rack/user.rb +9 -1
  17. data/lib/airbrake/rack/user_filter.rb +2 -0
  18. data/lib/airbrake/rack.rb +4 -0
  19. data/lib/airbrake/rails/action_cable/notify_callback.rb +2 -0
  20. data/lib/airbrake/rails/action_cable.rb +21 -17
  21. data/lib/airbrake/rails/action_controller.rb +9 -4
  22. data/lib/airbrake/rails/action_controller_notify_subscriber.rb +6 -2
  23. data/lib/airbrake/rails/action_controller_performance_breakdown_subscriber.rb +6 -1
  24. data/lib/airbrake/rails/action_controller_route_subscriber.rb +8 -15
  25. data/lib/airbrake/rails/active_job.rb +25 -8
  26. data/lib/airbrake/rails/active_record.rb +2 -0
  27. data/lib/airbrake/rails/active_record_subscriber.rb +7 -3
  28. data/lib/airbrake/rails/app.rb +60 -25
  29. data/lib/airbrake/rails/backtrace_cleaner.rb +13 -0
  30. data/lib/airbrake/rails/curb.rb +19 -22
  31. data/lib/airbrake/rails/event.rb +23 -6
  32. data/lib/airbrake/rails/excon_subscriber.rb +4 -0
  33. data/lib/airbrake/rails/http.rb +13 -7
  34. data/lib/airbrake/rails/http_client.rb +12 -6
  35. data/lib/airbrake/rails/net_http.rb +14 -6
  36. data/lib/airbrake/rails/railtie.rb +54 -0
  37. data/lib/airbrake/rails/railties/action_controller_tie.rb +90 -0
  38. data/lib/airbrake/rails/railties/active_record_tie.rb +74 -0
  39. data/lib/airbrake/rails/railties/middleware_tie.rb +62 -0
  40. data/lib/airbrake/rails/typhoeus.rb +11 -7
  41. data/lib/airbrake/rails.rb +14 -135
  42. data/lib/airbrake/rake/tasks.rb +15 -13
  43. data/lib/airbrake/rake.rb +49 -46
  44. data/lib/airbrake/resque.rb +33 -0
  45. data/lib/airbrake/shoryuken.rb +16 -4
  46. data/lib/airbrake/sidekiq/retryable_jobs_filter.rb +16 -8
  47. data/lib/airbrake/sidekiq.rb +22 -16
  48. data/lib/airbrake/sneakers.rb +39 -1
  49. data/lib/airbrake/version.rb +3 -1
  50. data/lib/airbrake.rb +2 -0
  51. data/lib/generators/airbrake_generator.rb +5 -6
  52. data/lib/generators/airbrake_initializer.rb.erb +67 -73
  53. metadata +53 -97
@@ -1,18 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Airbrake
2
4
  module Rails
3
5
  # Enables support for exceptions occurring in ActiveJob jobs.
4
6
  module ActiveJob
5
7
  extend ActiveSupport::Concern
6
8
 
7
- # @return [Array<Regexp>] the list of known adapters
8
- ADAPTERS = [/Resque/, /Sidekiq/, /DelayedJob/].freeze
9
-
10
9
  def self.notify_airbrake(exception, job)
11
- queue_adapter = job.class.queue_adapter.to_s
12
-
13
- # Do not notify twice if a queue_adapter is configured already.
14
- raise exception if ADAPTERS.any? { |a| a =~ queue_adapter }
15
-
16
10
  notice = Airbrake.build_notice(exception)
17
11
  notice[:context][:component] = 'active_job'
18
12
  notice[:context][:action] = job.class.name
@@ -23,10 +17,33 @@ module Airbrake
23
17
  raise exception
24
18
  end
25
19
 
20
+ def self.perform(job, block)
21
+ timing = Airbrake::Benchmark.measure do
22
+ block.call
23
+ end
24
+ rescue StandardError => exception
25
+ Airbrake.notify_queue(
26
+ queue: job.class.name,
27
+ error_count: 1,
28
+ timing: 0.01,
29
+ )
30
+ raise exception
31
+ else
32
+ Airbrake.notify_queue(
33
+ queue: job.class.name,
34
+ error_count: 0,
35
+ timing: timing,
36
+ )
37
+ end
38
+
26
39
  included do
27
40
  rescue_from(Exception) do |exception|
28
41
  Airbrake::Rails::ActiveJob.notify_airbrake(exception, self)
29
42
  end
43
+
44
+ around_perform do |job, block|
45
+ Airbrake::Rails::ActiveJob.perform(job, block)
46
+ end
30
47
  end
31
48
  end
32
49
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Airbrake
2
4
  module Rails
3
5
  # Rails <4.2 has a bug with regard to swallowing exceptions in the
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'airbrake/rails/event'
2
4
  require 'airbrake/rails/backtrace_cleaner'
3
5
 
@@ -8,6 +10,8 @@ module Airbrake
8
10
  # @since v8.1.0
9
11
  class ActiveRecordSubscriber
10
12
  def call(*args)
13
+ return unless Airbrake::Config.instance.query_stats
14
+
11
15
  routes = Airbrake::Rack::RequestStore[:routes]
12
16
  return if !routes || routes.none?
13
17
 
@@ -22,8 +26,8 @@ module Airbrake
22
26
  func: frame[:function],
23
27
  file: frame[:file],
24
28
  line: frame[:line],
25
- start_time: event.time,
26
- end_time: event.end
29
+ timing: event.duration,
30
+ time: event.time,
27
31
  )
28
32
  end
29
33
  end
@@ -33,7 +37,7 @@ module Airbrake
33
37
  def last_caller
34
38
  exception = StandardError.new
35
39
  exception.set_backtrace(
36
- Airbrake::Rails::BacktraceCleaner.clean(Kernel.caller)
40
+ Airbrake::Rails::BacktraceCleaner.clean(Kernel.caller),
37
41
  )
38
42
  Airbrake::Backtrace.parse(exception).first || {}
39
43
  end
@@ -1,43 +1,78 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Airbrake
2
4
  module Rails
3
- # App is a wrapper around Rails.application and Rails::Engine.
5
+ # App is a wrapper around Rails.application.
4
6
  #
5
7
  # @since v9.0.3
6
8
  # @api private
7
9
  class App
8
- Route = Struct.new(:path, :controller, :action)
10
+ Route = Struct.new(:path)
9
11
 
10
- def routes
11
- @routes ||= app_routes.merge(engine_routes).flat_map do |(engine_name, routes)|
12
- routes.map { |rails_route| build_route(engine_name, rails_route) }
13
- end
14
- end
12
+ # @param [] request
13
+ # @return [Airbrake::Rails::App::Route, nil]
14
+ # rubocop:disable Metrics/AbcSize
15
+ def self.recognize_route(request)
16
+ # Duplicate `request` because `recognize` *can* strip the request's
17
+ # `path_info`, which results in broken engine links (when the engine has
18
+ # an isolated namespace).
19
+ request_copy = request.dup
15
20
 
16
- private
21
+ # Save original script name because `router.recognize(request)` mutates
22
+ # it. It's a Rails bug. More info in:
23
+ # * https://github.com/airbrake/airbrake/issues/1072
24
+ # * https://github.com/rails/rails/issues/31152
25
+ original_script_name = request.env['SCRIPT_NAME']
17
26
 
18
- def app_routes
19
- # Engine name is nil because this is default (non-engine) routes.
20
- { nil => ::Rails.application.routes.routes.routes }
21
- end
27
+ # We must search every engine individually to find a concrete route. If
28
+ # we rely only on the `Rails.application.routes.router`, then the
29
+ # recognize call would return the root route, neglecting PATH_INFO
30
+ # completely. For example:
31
+ # * a request is made to `marketing#pricing`
32
+ # * `Rails.application` recognizes it as `marketing#/` (incorrect)
33
+ # * `Marketing::Engine` recognizes it as `marketing#/pricing` (correct)
34
+ engines.each do |engine|
35
+ engine.routes.router.recognize(request_copy) do |route, _params|
36
+ # Restore original script name. Remove this code when/if the Rails
37
+ # bug is fixed: https://github.com/airbrake/airbrake/issues/1072
38
+ request.env['SCRIPT_NAME'] = original_script_name
22
39
 
23
- def engine_routes
24
- ::Rails::Engine.subclasses.flat_map.with_object({}) do |engine, hash|
25
- next if (eng_routes = engine.routes.routes.routes).none?
40
+ # Skip "catch-all" routes such as:
41
+ # get '*path => 'pages#about'
42
+ #
43
+ # Ideally, we should be using `route.glob?` but in Rails 7+ this
44
+ # call would fail with a `NoMethodError`. This is because in
45
+ # Rails 7+ the AST for the route is not kept in memory anymore.
46
+ #
47
+ # See: https://github.com/rails/rails/pull/43006#discussion_r783895766
48
+ next if route.path.spec.any?(ActionDispatch::Journey::Nodes::Star)
26
49
 
27
- hash[engine.engine_name] = eng_routes
50
+ path =
51
+ if engine == ::Rails.application
52
+ route.path.spec.to_s
53
+ else
54
+ "#{engine.engine_name}##{route.path.spec}"
55
+ end
56
+
57
+ # Rails can recognize multiple routes for the given request. For
58
+ # example, if we visit /users/2/edit, then Rails sees these routes:
59
+ # * "/users/:id/edit(.:format)"
60
+ # * "/"
61
+ #
62
+ # We return the first route as, what it seems, the most optimal
63
+ # approach.
64
+ return Route.new(path)
65
+ end
28
66
  end
29
- end
30
67
 
31
- def build_route(engine_name, rails_route)
32
- engine_prefix = engine_name
33
- engine_prefix += '#' if engine_prefix
68
+ nil
69
+ end
70
+ # rubocop:enable Metrics/AbcSize
34
71
 
35
- Route.new(
36
- "#{engine_prefix}#{rails_route.path.spec}",
37
- rails_route.defaults[:controller],
38
- rails_route.defaults[:action]
39
- )
72
+ def self.engines
73
+ @engines ||= [*::Rails::Engine.subclasses, ::Rails.application]
40
74
  end
75
+ private_class_method :engines
41
76
  end
42
77
  end
43
78
  end
@@ -1,10 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Airbrake
2
4
  module Rails
3
5
  # BacktraceCleaner is a wrapper around Rails.backtrace_cleaner.
4
6
  class BacktraceCleaner
7
+ # @return [Regexp]
8
+ AIRBRAKE_FRAME_PATTERN = %r{/airbrake/lib/airbrake/}.freeze
9
+
5
10
  def self.clean(backtrace)
6
11
  ::Rails.backtrace_cleaner.clean(backtrace).first(1)
7
12
  end
8
13
  end
9
14
  end
10
15
  end
16
+
17
+ if defined?(Rails)
18
+ # Silence own frames to let the cleaner proceed to the next line (and probably
19
+ # find the correct call-site coming from the app code rather this library).
20
+ Rails.backtrace_cleaner.add_silencer do |line|
21
+ line =~ Airbrake::Rails::BacktraceCleaner::AIRBRAKE_FRAME_PATTERN
22
+ end
23
+ end
@@ -1,35 +1,32 @@
1
- module Curl
2
- # Monkey-patch to measure request timing.
3
- class Easy
4
- alias http_without_airbrake http
1
+ # frozen_string_literal: true
5
2
 
6
- def http(verb)
7
- Airbrake::Rack.capture_timing(:http) do
8
- http_without_airbrake(verb)
3
+ module Airbrake
4
+ module Rails
5
+ # Allows measuring request timing.
6
+ module CurlEasy
7
+ def http(verb)
8
+ Airbrake::Rack.capture_timing(:http) do
9
+ super(verb)
10
+ end
9
11
  end
10
- end
11
12
 
12
- alias perform_without_airbrake perform
13
-
14
- def perform(&block)
15
- Airbrake::Rack.capture_timing(:http) do
16
- perform_without_airbrake(&block)
13
+ def perform(&block)
14
+ Airbrake::Rack.capture_timing(:http) do
15
+ super(&block)
16
+ end
17
17
  end
18
18
  end
19
- end
20
- end
21
-
22
- module Curl
23
- # Monkey-patch to measure request timing.
24
- class Multi
25
- class << self
26
- alias http_without_airbrake http
27
19
 
20
+ # Allows measuring request timing.
21
+ module CurlMulti
28
22
  def http(urls_with_config, multi_options = {}, &block)
29
23
  Airbrake::Rack.capture_timing(:http) do
30
- http_without_airbrake(urls_with_config, multi_options, &block)
24
+ super(urls_with_config, multi_options, &block)
31
25
  end
32
26
  end
33
27
  end
34
28
  end
35
29
  end
30
+
31
+ Curl::Easy.prepend(Airbrake::Rails::CurlEasy)
32
+ Curl::Multi.singleton_class.prepend(Airbrake::Rails::CurlMulti)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Airbrake
2
4
  module Rails
3
5
  # Event is a wrapper around ActiveSupport::Notifications::Event.
@@ -6,7 +8,10 @@ module Airbrake
6
8
  # @api private
7
9
  class Event
8
10
  # @see https://github.com/rails/rails/issues/8987
9
- HTML_RESPONSE_WILDCARD = "*/*".freeze
11
+ HTML_RESPONSE_WILDCARD = "*/*"
12
+
13
+ # @return [Integer]
14
+ MILLISECOND = 1000
10
15
 
11
16
  include Airbrake::Loggable
12
17
 
@@ -40,11 +45,19 @@ module Airbrake
40
45
  end
41
46
 
42
47
  def time
43
- @event.time
44
- end
48
+ # On Rails 7+ `ActiveSupport::Notifications::Event#time` returns an
49
+ # instance of Float. It represents monotonic time in milliseconds.
50
+ # Airbrake Ruby expects that the provided time is in seconds. Hence,
51
+ # we need to convert it from milliseconds to seconds. In the
52
+ # versions below Rails 7, time is an instance of Time.
53
+ #
54
+ # Relevant commit:
55
+ # https://github.com/rails/rails/commit/81d0dc90becfe0b8e7f7f26beb66c25d84b8ec7f
56
+ #
57
+ # Ensure this conversion is applied exclusively for Rails 7.0
58
+ return @event.time / MILLISECOND if rails_7_0?
45
59
 
46
- def end
47
- @event.end
60
+ @event.time
48
61
  end
49
62
 
50
63
  def groups
@@ -59,7 +72,7 @@ module Airbrake
59
72
 
60
73
  if @event.payload[:exception]
61
74
  status = ActionDispatch::ExceptionWrapper.status_code_for_exception(
62
- @event.payload[:exception].first
75
+ @event.payload[:exception].first,
63
76
  )
64
77
  status = 500 if status == 0
65
78
 
@@ -78,6 +91,10 @@ module Airbrake
78
91
  def duration
79
92
  @event.duration
80
93
  end
94
+
95
+ def rails_7_0?
96
+ ::Rails::VERSION::MAJOR == 7 && ::Rails::VERSION::MINOR == 0
97
+ end
81
98
  end
82
99
  end
83
100
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'airbrake/rails/event'
2
4
 
3
5
  module Airbrake
@@ -6,6 +8,8 @@ module Airbrake
6
8
  # @since v9.2.0
7
9
  class Excon
8
10
  def call(*args)
11
+ return unless Airbrake::Config.instance.performance_stats
12
+
9
13
  routes = Airbrake::Rack::RequestStore[:routes]
10
14
  return if !routes || routes.none?
11
15
 
@@ -1,12 +1,18 @@
1
- module HTTP
2
- # Monkey-patch to measure request timing.
3
- class Client
4
- alias perform_without_airbrake perform
1
+ # frozen_string_literal: true
5
2
 
6
- def perform(request, options)
7
- Airbrake::Rack.capture_timing(:http) do
8
- perform_without_airbrake(request, options)
3
+ module Airbrake
4
+ module Rails
5
+ # Monkey-patch to measure request timing.
6
+ # @api private
7
+ # @since v11.0.2
8
+ module HTTP
9
+ def perform(request, options)
10
+ Airbrake::Rack.capture_timing(:http) do
11
+ super(request, options)
12
+ end
9
13
  end
10
14
  end
11
15
  end
12
16
  end
17
+
18
+ HTTP::Client.prepend(Airbrake::Rails::HTTP)
@@ -1,10 +1,16 @@
1
- # Monkey-patch to measure request timing.
2
- class HTTPClient
3
- alias do_get_without_airbrake do_get_block
1
+ # frozen_string_literal: true
4
2
 
5
- def do_get_block(request, proxy, connection, &block)
6
- Airbrake::Rack.capture_timing(:http) do
7
- do_get_without_airbrake(request, proxy, connection, &block)
3
+ module Airbrake
4
+ module Rails
5
+ # Allows measuring request timing.
6
+ module HTTPClient
7
+ def do_get_block(request, proxy, connection, &block)
8
+ Airbrake::Rack.capture_timing(:http) do
9
+ super(request, proxy, connection, &block)
10
+ end
11
+ end
8
12
  end
9
13
  end
10
14
  end
15
+
16
+ HTTPClient.prepend(Airbrake::Rails::HTTPClient)
@@ -1,10 +1,18 @@
1
- # Monkey-patch Net::HTTP to benchmark it.
2
- Net::HTTP.class_eval do
3
- alias_method :request_without_airbrake, :request
1
+ # frozen_string_literal: true
4
2
 
5
- def request(request, *args, &block)
6
- Airbrake::Rack.capture_timing(:http) do
7
- request_without_airbrake(request, *args, &block)
3
+ module Airbrake
4
+ module Rails
5
+ # Monkey-patch Net::HTTP to benchmark it.
6
+ # @api private
7
+ # @since v10.0.2
8
+ module NetHttp
9
+ def request(request, *args, &block)
10
+ Airbrake::Rack.capture_timing(:http) do
11
+ super(request, *args, &block)
12
+ end
13
+ end
8
14
  end
9
15
  end
10
16
  end
17
+
18
+ Net::HTTP.prepend(Airbrake::Rails::NetHttp)
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Airbrake
4
+ module Rails
5
+ # This railtie works for any Rails application that supports railties (Rails
6
+ # 3.2+ apps). It makes Airbrake Ruby work with Rails and report errors
7
+ # occurring in the application automatically.
8
+ class Railtie < ::Rails::Railtie
9
+ initializer('airbrake.middleware') do |app|
10
+ require 'airbrake/rails/railties/middleware_tie'
11
+ Railties::MiddlewareTie.new(app).call
12
+ end
13
+
14
+ rake_tasks do
15
+ # Report exceptions occurring in Rake tasks.
16
+ require 'airbrake/rake'
17
+
18
+ # Defines tasks such as `airbrake:test` & `airbrake:deploy`.
19
+ require 'airbrake/rake/tasks'
20
+ end
21
+
22
+ initializer('airbrake.action_controller') do
23
+ require 'airbrake/rails/railties/action_controller_tie'
24
+ Railties::ActionControllerTie.new.call
25
+ end
26
+
27
+ initializer('airbrake.active_record') do
28
+ require 'airbrake/rails/railties/active_record_tie'
29
+ Railties::ActiveRecordTie.new.call
30
+ end
31
+
32
+ initializer('airbrake.active_job') do
33
+ ActiveSupport.on_load(:active_job, run_once: true) do
34
+ # Reports exceptions occurring in ActiveJob jobs.
35
+ require 'airbrake/rails/active_job'
36
+ include Airbrake::Rails::ActiveJob
37
+ end
38
+ end
39
+
40
+ initializer('airbrake.action_cable') do
41
+ ActiveSupport.on_load(:action_cable, run_once: true) do
42
+ # Reports exceptions occurring in ActionCable connections.
43
+ require 'airbrake/rails/action_cable'
44
+ end
45
+ end
46
+
47
+ runner do
48
+ at_exit do
49
+ Airbrake.notify_sync($ERROR_INFO) if $ERROR_INFO
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'airbrake/rails/action_controller'
4
+ require 'airbrake/rails/action_controller_route_subscriber'
5
+ require 'airbrake/rails/action_controller_notify_subscriber'
6
+ require 'airbrake/rails/action_controller_performance_breakdown_subscriber'
7
+
8
+ module Airbrake
9
+ module Rails
10
+ module Railties
11
+ # Ties Airbrake APM (routes) and HTTP clients with Rails.
12
+ #
13
+ # @api private
14
+ # @since v13.0.1
15
+ class ActionControllerTie
16
+ def initialize
17
+ @route_subscriber = Airbrake::Rails::ActionControllerRouteSubscriber.new
18
+ @notify_subscriber = Airbrake::Rails::ActionControllerNotifySubscriber.new
19
+ @performance_breakdown_subscriber =
20
+ Airbrake::Rails::ActionControllerPerformanceBreakdownSubscriber.new
21
+ end
22
+
23
+ def call
24
+ ActiveSupport.on_load(:action_controller, run_once: true, yield: self) do
25
+ # Patches ActionController with methods that allow us to retrieve
26
+ # interesting request data. Appends that information to notices.
27
+ ::ActionController::Base.include(Airbrake::Rails::ActionController)
28
+
29
+ tie_routes_apm
30
+ tie_http_integrations
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def tie_routes_apm
37
+ [
38
+ # Cache route information for the duration of the request.
39
+ ['start_processing.action_controller', @route_subscriber],
40
+
41
+ # Send route stats.
42
+ ['process_action.action_controller', @notify_subscriber],
43
+
44
+ # Send performance breakdown: where a request spends its time.
45
+ ['process_action.action_controller', @performance_breakdown_subscriber],
46
+ ].each do |(event, callback)|
47
+ ActiveSupport::Notifications.subscribe(event, callback)
48
+ end
49
+ end
50
+
51
+ def tie_http_integrations
52
+ tie_net_http
53
+ tie_curl
54
+ tie_http
55
+ tie_http_client
56
+ tie_typhoeus
57
+ tie_excon
58
+ end
59
+
60
+ def tie_net_http
61
+ require 'airbrake/rails/net_http' if defined?(Net) && defined?(Net::HTTP)
62
+ end
63
+
64
+ def tie_curl
65
+ require 'airbrake/rails/curb' if defined?(Curl) && defined?(Curl::CURB_VERSION)
66
+ end
67
+
68
+ def tie_http
69
+ require 'airbrake/rails/http' if defined?(HTTP) && defined?(HTTP::Client)
70
+ end
71
+
72
+ def tie_http_client
73
+ require 'airbrake/rails/http_client' if defined?(HTTPClient)
74
+ end
75
+
76
+ def tie_typhoeus
77
+ require 'airbrake/rails/typhoeus' if defined?(Typhoeus)
78
+ end
79
+
80
+ def tie_excon
81
+ return unless defined?(Excon)
82
+
83
+ require 'airbrake/rails/excon_subscriber'
84
+ ActiveSupport::Notifications.subscribe(/excon/, Airbrake::Rails::Excon.new)
85
+ ::Excon.defaults[:instrumentor] = ActiveSupport::Notifications
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'airbrake/rails/active_record'
4
+ require 'airbrake/rails/active_record_subscriber'
5
+
6
+ module Airbrake
7
+ module Rails
8
+ module Railties
9
+ # Ties Airbrake APM (queries) with Rails.
10
+ #
11
+ # @api private
12
+ # @since v13.0.1
13
+ class ActiveRecordTie
14
+ def initialize
15
+ @active_record_subscriber = Airbrake::Rails::ActiveRecordSubscriber.new
16
+ end
17
+
18
+ def call
19
+ ActiveSupport.on_load(:active_record, run_once: true, yield: self) do
20
+ tie_activerecord_callback_fix
21
+ tie_activerecord_apm
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def tie_activerecord_callback_fix
28
+ # Reports exceptions occurring in some bugged ActiveRecord callbacks.
29
+ # Applicable only to the versions of Rails lower than 4.2.
30
+ return unless defined?(::Rails)
31
+ return if Gem::Version.new(::Rails.version) > Gem::Version.new('4.2')
32
+
33
+ ActiveRecord::Base.include(Airbrake::Rails::ActiveRecord)
34
+ end
35
+
36
+ def tie_activerecord_apm
37
+ # Some Rails apps don't use ActiveRecord.
38
+ return unless defined?(::ActiveRecord)
39
+
40
+ # However, some dependencies might still require it, so we need an
41
+ # extra check. Apps that don't need ActiveRecord will likely have no
42
+ # AR configurations defined. We will skip APM integration in that
43
+ # case. See: https://github.com/airbrake/airbrake/issues/1222
44
+ configurations = ::ActiveRecord::Base.configurations
45
+ return unless configurations.any?
46
+
47
+ # Send SQL queries.
48
+ ActiveSupport::Notifications.subscribe(
49
+ 'sql.active_record',
50
+ @active_record_subscriber,
51
+ )
52
+
53
+ # Filter out parameters from SQL body.
54
+ sql_filter = Airbrake::Filters::SqlFilter.new(
55
+ detect_activerecord_adapter(configurations),
56
+ )
57
+ Airbrake.add_performance_filter(sql_filter)
58
+ end
59
+
60
+ # Rails 6+ introduces the `configs_for` API instead of the deprecated
61
+ # `#[]`, so we need an updated call.
62
+ def detect_activerecord_adapter(configurations)
63
+ unless configurations.respond_to?(:configs_for)
64
+ return configurations[::Rails.env]['adapter']
65
+ end
66
+
67
+ cfg = configurations.configs_for(env_name: ::Rails.env).first
68
+ # Rails 7+ API : Rails 6 API.
69
+ cfg.respond_to?(:adapter) ? cfg.adapter : cfg.config['adapter']
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end