๐Ÿ›Error Monitoring with Sentry

Receive errors directly on your email with Sentry free plan.

Sentry is a powerful error tracking tool that helps you monitor and fix crashes in real-time. Adding it to your Lightning Rails project gives you instant visibility into production issues with stack traces, request context, and more.

Installation

The Sentry SDK for Rails comes as two gems. Add them to your Gemfile:

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

Then install the gems:

bundle install

Configure SDK

Generate the initializer with:

rails generate sentry

This creates config/initializers/sentry.rb.

Now edit the generated file to include your configuration:

if Rails.env.production?
  Sentry.init do |config|
    config.dsn = 'https://8f1bc0b22a55bb483283e664f9fb421f@o4509708042043392.ingest.us.sentry.io/4509789067608064'
    config.breadcrumbs_logger = [:active_support_logger, :http_logger]
  
    # Add data like request headers and IP for users,
    # see https://docs.sentry.io/platforms/ruby/data-management/data-collected/ for more info
    config.send_default_pii = true
  end
end

๐Ÿ’ก Make sure to only enable this in production by wrapping it in if Rails.env.production? , we don't want to spam our email with error messages when developing ;)


Verify Itโ€™s Working

You can test the setup by triggering a sample error in the production console.

begin
  1 / 0
rescue ZeroDivisionError => exception
  Sentry.capture_exception(exception)
end

Sentry.capture_message("test message")

This should send both the exception and the message to your Sentry dashboard.


Optional: Trace Propagation (Frontend + Backend)

If you're using frontend Sentry, you can enable distributed tracing by leaving this in your layout:

<%= Sentry.get_trace_propagation_meta.html_safe %>

It should have been added to your header when you used the generator above. <head> tag in app/views/layouts/application.html.erb.

Happy Bug Tracking! ๐Ÿ›๐ŸฆŸ๐Ÿ•ท๏ธ๐Ÿชฒ

Last updated

Was this helpful?