bugsnag-ruby: Allow to notify about sidekiq error only after several insuccessful retries

We are sending emails with sidekiq workers that are monitored for exceptions by bugsnag.

There are a lot of one time exceptions like IO::EAGAINWaitReadable. The 5 seconds downtime of the mail server (e.g it is redeployed) results in hundreds of such errors.

Can we setup bugsnag-sidekiq to notify about exceptions only after several unsuccessful retries?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 34 (11 by maintainers)

Most upvoted comments

It would be great if the Bugsnag documentation could provide clear, simple information on how its Sidekiq-integration works.

I finally got it working with something like this (hopefully it can help someone else):

# config/initializers/bugsnag.rb

class SidekiqBugsnagMiddleware

  # With Bugsnag callbacks are treated as request state and are registered on the current job only.
  # Because of this we cannot use the regular `Bugsnag.configure` block below. Instead we use a
  # Sidekiq middleware (which run before each job) and setup the callback before each job runs.

  BUGSNAG_CALLBACK = lambda do |bug_report|
    if sidekiq_data = bug_report.meta_data[:sidekiq]

      # this value is nil for the first job attempt
      retry_count = sidekiq_data[:msg]['retry_count']

      if !retry_count || retry_count && retry_count < 5
        bug_report.ignore!
      end
    end
  end

  def initialize(options = nil)
  end

  def call(worker, msg, queue)
    # Guard against changes in the behaviour described above. Otherwise we'd risk adding to
    # the notify callback queue for every job invocation, and that would quickly spiral out of control.
    unless Bugsnag.before_notify_callbacks.include?(BUGSNAG_CALLBACK)
      Bugsnag.before_notify_callbacks << BUGSNAG_CALLBACK
    end

    yield
  end
end

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add SidekiqBugsnagMiddleware
  end
end