Guides

Ruby on Rails

Report exceptions from controllers, Active Job, mailers and the Rails runner with the official sentry-rails gem. It works with Rails 5.2 and later.

1. Install the gems

Add them to your Gemfile and run bundle install:

Gemfile
gem "sentry-ruby"
gem "sentry-rails"

2. Point them at Uncaught

Create an initializer with your project's DSN, which you'll find on the project's setup page in Uncaught:

config/initializers/sentry.rb
Sentry.init do |config|
  config.dsn = ENV["SENTRY_DSN"] # https://<key>@uncaught.thistinct.tools/<project-id>
  config.breadcrumbs_logger = [ :active_support_logger, :http_logger ]
  config.send_default_pii = true # sends IP addresses, cookies and request data
  config.traces_sample_rate = 0.1 # time one in ten requests and jobs for Performance
  config.release = ENV["GIT_SHA"]
end

Unhandled exceptions are reported from then on. Uncaught groups them into issues and emails your team about new ones, regressions and spikes.

3. Check that it works

Send a test error from a console on the server:

bin/rails console
Sentry.capture_message("Hello from Rails")

It shows up on the project page within seconds. The project's setup page can also send a sample event for you.

Tell Uncaught who was affected

Set the user once per request, and Uncaught counts how many people each issue hit:

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action { Sentry.set_user(id: Current.user&.id, email: Current.user&.email_address) }
end

Tags you set show up on each issue, broken down by value:

Ruby
Sentry.set_tags(plan: current_account.plan, region: "eu")

Report errors you rescue

Ruby
begin
  PaymentGateway.charge!(order)
rescue PaymentGateway::Declined => error
  Sentry.capture_exception(error)
  redirect_to checkout_path, alert: "Your card was declined."
end

On Rails 7 and later, errors passed to Rails.error.report can be reported too:

config/initializers/sentry.rb
config.rails.register_error_subscriber = true

Environments and releases

The SDK uses Rails.env as the environment, so production, staging and development stay apart. Set config.release to your commit SHA or version so Uncaught can tell you which release an issue first appeared in, and reopen issues you resolved "in the next release" when they come back. To create releases and mark deploys from CI, see Releases and sentry-cli.

Keep noise out

config/initializers/sentry.rb
Sentry.init do |config|
  # ...
  config.enabled_environments = %w[ production staging ]
  config.excluded_exceptions += [ "ActionController::RoutingError" ]
end

Each project also has a rate limit in Uncaught, so a runaway loop can't bury your other projects.