How to use Sentry on Heroku to track errors on your ruby on rails application

Firstly, why use Sentry, couldn’t we just wait for users

Setting up Sentry

In your terminal, as per Heroku instructions run:

heroku addons:create sentry

Now add these gems to your Gemfileand bundle:

gem 'sentry-ruby'
gem 'sentry-rails'

Create a new initializer in /config/initializers/sentry.rb and inside, place the following:

Sentry.init do |config|
config.dsn = 'https://examplePublicKey@o0.ingest.sentry.io/0'
config.breadcrumbs_logger = [:active_support_logger, :http_logger]

# To activate performance monitoring, set one of these options.
# We recommend adjusting the value in production:
config.traces_sample_rate = 1
# or
# config.traces_sampler = lambda do |context|
# true
# end
end

To get the your unique value forconfig.dsn, head to this sentry url - it will provide you with yours so you can replace the sample value above with it. Alternatively, run heroku config to list the environment variables available in your production application, and look for the SENTRY_DSN value.

The configuration code above captures all errors, rather than just a sample. This is fine for smaller applications, but if your application already has a lot of logs/users/errors, adjust this sample rate down (to, say 0.5or 0.2), to capture just a portion of the errors your application generates.

Going live

Before going live, let’s ensure we can produce an error on demand by creating a route and controller action that will raise an error. Define a route and add the following controller action:

# app/controllers/home_controller.rbdef error
# This controller action generates an error for testing purposes
raise "error"
end

Push your app to production, and visit the route to raise an error.

Monitoring errors

Within a few seconds, you’ll see the error appear in the Sentry dashboard:

An error displayed in the Sentry dashboard

From here, you can drill down into the error to view its specific details from the comfort of the Sentry web UI:

Some of the info displayed about each error

That’s really all there is to setting up Sentry on Heroku to track errors in your ruby on rails application!

Extras

Here are a few extra things you may wish to note.

Firstly, you may not want Sentry running on local host, so define the environments you want Sentry to track by adding this line to config/initializers/sentry.rb :

config.enabled_environments = ['production']

You could, of course, add more environments, e.g. ['staging', 'production'] etc.

Secondly, we can give Sentry the ability to track releases by running this once:

heroku labs:enable runtime-dyno-metadata

Lastly, take note of how to access your app’s Sentry dashboard in the future. To access your app’s Sentry dashboard, go to https://dashboard.heroku.com/apps/, click on your app, and under the ‘Overview’ tab, find the Sentry add on and click on it. The reason to take note of this is because you don’t sign into the Sentry website with email/password or via Google login like you may on many other websites, since you created the account via Heroku.

Happy error monitoring!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store