Guides

Ruby

For Sinatra, Hanami, Roda, plain Rack apps, background workers and scripts, use the official sentry-ruby gem. On Rails, follow the Rails guide instead.

1. Install the gem

Gemfile
gem "sentry-ruby"

2. Initialize it at boot

Run this once, before your app handles any work:

Ruby
require "sentry-ruby"

Sentry.init do |config|
  config.dsn = ENV["SENTRY_DSN"] # https://<key>@uncaught.thistinct.tools/<project-id>
  config.environment = ENV.fetch("APP_ENV", "production")
  config.release = ENV["GIT_SHA"]
  config.traces_sample_rate = 0.1 # time one in ten requests for Performance
end

3. Catch exceptions

For Rack apps (Sinatra, Hanami, Roda), add the middleware in config.ru:

config.ru
use Sentry::Rack::CaptureExceptions
run MyApp

Anywhere else, like a worker or a script, report what you rescue:

Ruby
begin
  sync_inventory
rescue => error
  Sentry.capture_exception(error)
  raise
end

Sidekiq, Resque and Delayed Job have their own gems that report job failures for you:

Gemfile
gem "sentry-sidekiq" # or sentry-resque, sentry-delayed_job

Add context

Ruby
Sentry.set_user(id: user.id, email: user.email)
Sentry.set_tags(queue: "imports", tenant: tenant.slug)
Sentry.add_breadcrumb(Sentry::Breadcrumb.new(category: "import", message: "Read #{rows.size} rows"))