Integrate Sentry to Rails application

Rasna
by Rasna 

From Sentry - Monitoring and Tracking Application Error blog, we got insights on sentry and how Gurzu has been using sentry within the company.

In this article, we will be focusing on the integration of sentry on rails applications. Our developers have been using sentry in order to get real-time insight into production deployments with info to reproduce and fix crashes. It not only notifies us of exceptions or errors that our users run into while using applications but also organizes them on a web dashboard that helps us to monitor/ track issues and records trails of the cause of the errors. Exceptions reported by the sentry include stack traces, device info, version, and another relevant context automatically, which helps developers debug the issues and fix the crashes.

How to add Sentry in rails application

Step 0: Sign up for a Sentry account and create a project

We’ll need to make sure that we’ve created a Sentry project for real-time updates on errors. 

For this, we need to create an account on sentry. Since we have a self-hosted sentry, we created an account on it otherwise we can also Sign up for Sentry (it’s free) and create a project on the dashboard. From this, we can get a DSN which we will need later.

Step 1: Installation

In the rails project, add sentry-ruby and sentry-rails to Gemfile:

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

Step 2: Configuration

To configure sentry, we need to add the following configuration on config/initializers/sentry.rb:

Sentry.init do |config|
  config.dsn = 'YOUR DSN HERE'
  config.breadcrumbs_logger = [:active_support_logger, :http_logger]

  # To activate performance monitoring, we can set any one of these options.
  config.traces_sample_rate = 0.5
  # or
  config.traces_sampler = lambda do |context|
    true
  end
end


Testing Sentry

We can test whether everything is working or not by running below snippets which is an intentional error. As soon as we run, we will get a notification of an error that verifies that our configuration is a success.

Sentry.capture_message("test message")

We can view and resolve the recorded error, by logging into the Sentry account and on the project where the error’s title will open a page where we can see detailed information about the error and can mark it as resolved when fixed.