Connect Sidekiq to Bugsink

The Sidekiq integration reports exceptions from background jobs with the job class, queue, ID, and worker-side Ruby stack.

This guide adds the integration to an existing Sidekiq worker, queues a job which fails, and checks the result in Bugsink. To follow along, create a Bugsink project and copy its DSN from the project setup page.

Install the SDK

Add the open-source Sentry Ruby and Sidekiq gems to the worker’s bundle. Bugsink accepts the Sentry event protocol, so these SDKs can send the worker’s errors directly to your Bugsink project:

bundle add sentry-ruby sentry-sidekiq

Configure your DSN

Initialize the SDK when the worker starts. Requiring sentry-sidekiq installs the Sidekiq middleware, while reading the DSN from the environment keeps the project credential out of source control:

# frozen_string_literal: true

require "sentry-sidekiq"

Sentry.init do |config|
  config.dsn = ENV.fetch("SENTRY_DSN")
end

Load that file from the same entry point which configures Sidekiq and loads your jobs. This example also keeps its existing Redis configuration there:

# frozen_string_literal: true

require "sidekiq"
require_relative "error_reporting"

redis = {url: ENV.fetch("REDIS_URL", "redis://127.0.0.1:6380/0")}
Sidekiq.configure_client { |config| config.redis = redis }
Sidekiq.configure_server { |config| config.redis = redis }

require_relative "app/jobs/inventory_reservation_job"

Set SENTRY_DSN to the DSN shown by your Bugsink project when you start the worker:

export SENTRY_DSN=https://key@bugsink.example/1

Queue a job that fails

A job does not need a Bugsink call. When perform raises and Sidekiq records the execution as failed, the integration reports that exception from the worker process. This inventory job raises ProductNotFound for an unknown product:

class InventoryReservationJob
  include Sidekiq::Job

  sidekiq_options queue: "inventory", retry: false

  INVENTORY = {
    "coffee" => {name: "Coffee beans", stock: 12},
    "tea" => {name: "Breakfast tea", stock: 8}
  }.freeze

  def perform(sku, quantity)
    product = INVENTORY[sku]
    raise ProductNotFound, "Product #{sku} is not in inventory" unless product

    raise ArgumentError, "Only #{product[:stock]} units of #{sku} are available" if quantity > product[:stock]

    puts "Reserved #{quantity} x #{product[:name]}"
  end
end

Start the worker with its normal queue and Redis configuration:

bundle exec sidekiq --require ./config.rb --queue inventory

Queue the job from your application or a separate producer. It is important to enqueue the job rather than call perform directly, because the Sidekiq worker lifecycle is what supplies the job context:

sku = ARGV.fetch(0, "coffee")
quantity = Integer(ARGV.fetch(1, "1"))
jid = InventoryReservationJob.perform_async(sku, quantity)
puts "Queued InventoryReservationJob #{jid}"

For a setup check, queue a product which does not exist:

bundle exec ruby enqueue.rb sidekiq-missing 1

Sidekiq records the failed execution and applies the job’s normal retry policy. The example disables retries so the deliberate error runs only once.

See the failed job in Bugsink

Open your Bugsink project and select the new ProductNotFound issue. The event points to the worker code and identifies the Sidekiq job which failed.

Bugsink preview

That’s it: failed Sidekiq jobs will now be reported to Bugsink.

The integration also adds queue and jid tags and a Sidekiq context. You can use the queue tag to find errors from one worker pool, while the job ID links a Bugsink event to the matching Sidekiq log entry.

Retries

Retries determine how often a recurring failure is reported. By default, the integration reports each failed execution before Sidekiq schedules its retry. Set report_after_job_retries if you want one report only after the job has used all its retries:

Sentry.init do |config|
  config.dsn = ENV.fetch("SENTRY_DSN")
  config.sidekiq.report_after_job_retries = true
end

Limitations

Job arguments are not included in the Sidekiq context with the SDK’s default data settings. The job class, queue, ID, exception, and application stack are still available without sending the values passed to the job.

For handled exceptions, Ruby logging, messages, tags, structured context, and other SDK options, continue with the Ruby guide.