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:
heroku addons:create sentry
Now add these gems to your
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
# config.traces_sampler = lambda do |context|
To get the your unique value for
config.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
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.2), to capture just a portion of the errors your application generates.
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
Push your app to production, and visit the route to raise an error.
Within a few seconds, you’ll see the error appear 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:
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.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