> For the complete documentation index, see [llms.txt](https://docs.lightningrails.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lightningrails.com/deploying-to-production/security/error-monitoring-with-sentry.md).

# Error Monitoring with Sentry

<figure><img src="/files/bV38IbAXxoZbUHFs9Pok" 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! 🐛🦟🕷️🪲


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lightningrails.com/deploying-to-production/security/error-monitoring-with-sentry.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
