class Sentry::Rails::ActiveJobExtensions::SentryReporter

def capture_exception(job, e)

def capture_exception(job, e)
  Sentry::Rails.capture_exception(
    e,
    extra: sentry_context(job),
    tags: {
      job_id: job.job_id,
      provider_job_id: job.provider_job_id
    }
  )
end

def detach_event_handlers

def detach_event_handlers
  subscribers.each do |subscriber|
    ActiveSupport::Notifications.unsubscribe(subscriber)
  end
  subscribers.clear
end

def finish_sentry_transaction(transaction, status)

def finish_sentry_transaction(transaction, status)
  return unless transaction
  transaction.set_http_status(status)
  transaction.finish
end

def handle_error_event(*args)

def handle_error_event(*args)
  event = ActiveSupport::Notifications::Event.new(*args)
  yield(event.payload[:job], event.payload[:error])
end

def record(job, &block)

def record(job, &block)
  Sentry.with_scope do |scope|
    begin
      scope.set_transaction_name(job.class.name, source: :task)
      transaction = Sentry.start_transaction(
        name: scope.transaction_name,
        source: scope.transaction_source,
        op: OP_NAME,
        origin: SPAN_ORIGIN
      )
      scope.set_span(transaction) if transaction
      yield.tap do
        finish_sentry_transaction(transaction, 200)
      end
    rescue Exception => e # rubocop:disable Lint/RescueException
      finish_sentry_transaction(transaction, 500)
      capture_exception(job, e)
      raise
    end
  end
end

def register_event_handlers

def register_event_handlers
  EVENT_HANDLERS.each do |name, handler|
    subscribers << ActiveSupport::Notifications.subscribe(name) do |*args|
      public_send(handler, *args)
    end
  end
end

def retry_handler(*args)

This handler does not capture error unless `active_job_report_on_retry_error` is true
def retry_handler(*args)
  handle_error_event(*args) do |job, error|
    return if !Sentry.initialized? || job.already_supported_by_sentry_integration?
    return unless Sentry.configuration.rails.active_job_report_on_retry_error
    capture_exception(job, error)
  end
end

def sentry_context(job)

def sentry_context(job)
  {
    active_job: job.class.name,
    arguments: sentry_serialize_arguments(job.arguments),
    scheduled_at: job.scheduled_at,
    job_id: job.job_id,
    provider_job_id: job.provider_job_id,
    locale: job.locale
  }
end

def sentry_serialize_arguments(argument)

def sentry_serialize_arguments(argument)
  case argument
  when Range
    if (argument.begin || argument.end).is_a?(ActiveSupport::TimeWithZone)
      argument.to_s
    else
      argument.map { |v| sentry_serialize_arguments(v) }
    end
  when Hash
    argument.transform_values { |v| sentry_serialize_arguments(v) }
  when Array, Enumerable
    argument.map { |v| sentry_serialize_arguments(v) }
  when ->(v) { v.respond_to?(:to_global_id) }
    argument.to_global_id.to_s rescue argument
  else
    argument
  end
end

def subscribers

def subscribers
  @__subscribers__ ||= Set.new
end