Sha256: 3611f425f303c3032ae3a9ded2b0fe516e86225e89a94f76ae7c18584cb7b5ff

Contents?: true

Size: 1.72 KB

Versions: 1

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" => "error_subscriber",
          "name" => "exception",
          "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

1 entries across 1 versions & 1 rubygems

Version Path
plain_apm-0.5.7 lib/plain_apm/hooks/error_reporter.rb