# Error Monitoring with Sentry

<figure><img src="https://2221554152-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPCf6vtFuZEPHXJd7JfIe%2Fuploads%2F5bptUDWxQfWbmSxBfBz1%2FScreenshot%202025-08-05%20at%2010.39.52.png?alt=media&#x26;token=909beb0b-b2cc-471d-887d-a18284735d44" alt=""><figcaption></figcaption></figure>

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`:

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

Then install the gems:

```bash
bundle install
```

***

### Configure SDK

Generate the initializer with:

```bash
rails generate sentry
```

This creates `config/initializers/sentry.rb`.

Now edit the generated file to include your configuration:

```ruby
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 ;)&#x20;

***

### Verify It’s Working

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

```ruby
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:

```erb
<%= 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! 🐛🦟🕷️🪲
