Sha256: 8fb9f2ae9e88958676981a91daa68b38223c12f9183c1b3a1f0058e4489c4940

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
plain_apm-0.6.7 lib/plain_apm/hooks/error_reporter.rb
plain_apm-0.6.6 lib/plain_apm/hooks/error_reporter.rb
plain_apm-0.6.5 lib/plain_apm/hooks/error_reporter.rb