module PlainApm module Hooks # Rails 7 error notification mechanism class ErrorReporter include EventAttributes def install return unless defined?(Rails) && Rails.respond_to?(:error) # 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 uninstall return unless defined?(Rails) && Rails.respond_to?(:error) # There's no unsubscribe subscribers = Rails.error.instance_variable_get(:@subscribers) subscribers&.delete(self) end def collect(e, handled:, severity:, context: {}) source, event = attributes_from_exception(e, context) return if event.nil? event["source"] = source event["name"] = "error_reporter" ::PlainApm.agent.collect(event) end alias_method :report, :collect end end end