class Sentry::Rails::ActiveJobExtensions::SentryReporter

def finish_sentry_transaction(transaction, status)

def finish_sentry_transaction(transaction, status)
  return unless transaction
  transaction.set_http_status(status)
  transaction.finish
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 =
        if job.is_a?(::Sentry::SendEventJob)
          nil
        else
          Sentry.start_transaction(name: scope.transaction_name, source: scope.transaction_source, op: OP_NAME)
        end
      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)
      Sentry::Rails.capture_exception(
        e,
        extra: sentry_context(job),
        tags: {
          job_id: job.job_id,
          provider_job_id: job.provider_job_id
        }
      )
      raise
    end
  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 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