Class: Sentry::Sidekiq::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/sidekiq/error_handler.rb

Constant Summary collapse

WITH_SIDEKIQ_7 =
::Gem::Version.new(::Sidekiq::VERSION) >= ::Gem::Version.new("7.0")

Instance Method Summary collapse

Instance Method Details

#call(ex, context, sidekiq_config = nil) ⇒ Object

Parameters:

  • ex (Exception)

    the exception / error that occured

  • context (Hash or Array)

    Sidekiq error context

  • sidekiq_config (Sidekiq::Config, Hash) (defaults to: nil)

    Sidekiq configuration, Defaults to nil. Sidekiq will pass the config in starting Sidekiq 7.1.5, see github.com/sidekiq/sidekiq/pull/6051



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sentry/sidekiq/error_handler.rb', line 16

def call(ex, context, sidekiq_config = nil)
  return unless Sentry.initialized?

  context_filter = Sentry::Sidekiq::ContextFilter.new(context)

  scope = Sentry.get_current_scope
  scope.set_transaction_name(context_filter.transaction_name, source: :task) unless scope.transaction_name

  # If Sentry is configured to only report an error _after_ all retries have been exhausted,
  # and if the job is retryable, and have not exceeded the retry_limit,
  # return early.
  if Sentry.configuration.sidekiq.report_after_job_retries && retryable?(context)
    retry_count = context.dig(:job, "retry_count")
    if retry_count.nil? || retry_count < retry_limit(context, sidekiq_config) - 1
      return
    end
  end

  # See if we want to ignore jobs that have dead: false
  return if Sentry.configuration.sidekiq.report_only_dead_jobs && context.dig(:job, "dead") == false

  # Check if the retry count is below the attempt_threshold
  attempt_threshold = context.dig(:job, "attempt_threshold")
  if attempt_threshold && retryable?(context)
    attempt_threshold = attempt_threshold.to_i
    retry_count = context.dig(:job, "retry_count")
    # attempt 1 - retry_count is nil
    # attempt 2 - this is your first retry so retry_count is 0
    # attempt 3 - you have retried once, retry_count is 1
    attempt = retry_count.nil? ? 1 : retry_count.to_i + 2

    return if attempt < attempt_threshold
  end

  Sentry::Sidekiq.capture_exception(
    ex,
    contexts: { sidekiq: context_filter.filtered },
    hint: { background: false }
  )
ensure
  scope&.clear
end