module PlainApm module Hooks # Rails 7 error notification mechanism class ErrorReporter IGNORED_EXCEPTIONS = [ "Sidekiq::JobRetry::Skip", # Sidekiq uses exceptions for control flow. "ActionController::RoutingError" # Rails unmapped route, raised before the request hits the app. ].freeze def install begin require "active_support/error_reporter" require "active_support/lazy_load_hooks" rescue LoadError return end # Install the hook when the app is up. This might miss errors that # happen before that, but that's OK. ::ActiveSupport.on_load(:after_initialize, yield: self, run_once: true) do ::Rails.error.subscribe(self) end end def collect(e, handled:, severity:, context: {}) return if IGNORED_EXCEPTIONS.include?(e.class.name) event = { "source" => "exception", "name" => "error_reporter", "class" => e.class.name, "message" => e.message, "backtrace" => e.backtrace, "handled" => handled, "severity" => severity, "context" => context } if context[:job] && context[:job].is_a?(ActiveJob::Base) event.merge!({ "job_class" => context[:job].class.name, "queue_name" => context[:job].queue_name }) end if e.cause event.merge!({ "cause_class" => e.cause.class.name, "cause_message" => e.cause.message, "cause_backtrace" => e.cause.backtrace }) end PlainApm::Agent.collect(event) end alias_method :report, :collect end end end