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

Setting up Sentry

In your terminal, as per Heroku instructions run:

Now add these gems to your Gemfileand bundle:

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

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:

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!


Here are just a couple of extra things you may wish to configure.

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 :

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: