Sentry Error Tracker with Rails

Sentry make lives of rails developers heaven. Sentry gives notification when an error comes in your app with all relevant data to reproduce the error. The integration to rails app is also quite easy just a few lines.

Sentry Error Tracker with Rails

The key to success in life is learning from your mistakes. So as a web developer, errors are bound to come. The compile errors are easy. Problem comes with runtime errors as backend developer some time data comes to app different, especially when users are many and using your app at any time of world around the world. You will be unable to continuously see logs in the server all 24 hours.

Benefits of Error Tracker

Errors are good, but resolving those errors need data, a lot of data. Ideally, what we want is that when error comes, we need the following data so that we can reproduce and debug that data:

  • User device details, browser details, operating system?
  • Environment in which error occurred?
  • What url was hit when the error occurred?
  • What inputs were provided with the url?
  • At which line of code error occurred?
  • What was before and after error logs?
  • Is this error repeating? If yes, then how many times?
  • And lastly, What was the error?

If we get all above data, it will be straightforward to reproduce and debug the bug easily.

Sentry

For Ruby on Rails, I like Sentry Error Tracker the most. It provides help with all the above features and more. Sentry is free for around 50k errors a month to check it out. Sentry can send email, slack or any other web-hook notification when an error comes to your app with all the data above in sentry error details.


Create an account on sentry at https://sentry.io/welcome/. Then create a new application in sentry for your app, and it will give the instructions to install it. When selecting the type of application, select Ruby on Rails if the app is on Ruby on Rails. As Sentry provides support to many frontend and backend technologies.

Install Sentry:

When application in sentry has been created. Then add this to your project’s Gemfile:

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

and then initialise the SDK within your config/initializers/sentry.rb:

Sentry.init do |config|
  config.dsn = 'https://xxxxxxxxx'
  config.breadcrumbs_logger = [:active_support_logger]
  config.enabled_environments = %w[production staging]
  config.excluded_exceptions += ['ActionController::RoutingError', 'ActiveRecord::RecordNotFound']

  config.release = 'xxxx@0.1b'

  # To activate performance monitoring, set one of these options.
  # We recommend adjusting the value in production:
  config.traces_sample_rate = 1
  config.send_default_pii = true
end

Above is mine with custom settings.

Replace dns with the one given when created the project in sentry.


Release tells the project release that caused this error in case of release system of the app.

Now you can see when error comes like this:

Error Example in Sentry
Error Example in Sentry

Extras in Sentry

We can invoke an error with data as well with sentry. We don’t need to wait 500 errors to have error. But Sentry can work like a special logger for future improvements. So, we need to add this code of sentry capture to capture that data as error or issue:

Sentry.capture_message 'Api#api_response',
                             level: :info,
                             extra: { type: 'User', status: response.status_code, body: response.parsed_response }

I am calling this code whenever API response I am hitting is not giving 200 but resolving that issue in rescue but at the same time I will get error to handle that API better next time.


Happy Coding!