nerdgeschoss / shimmer

Shimmer is a collection of Rails extensions that bring advanced UI features into your app and make your life easier as a developer.
https://nerdgeschoss.de
MIT License
5 stars 1 forks source link

Error Tracking #71

Open jmarsh24 opened 1 year ago

jmarsh24 commented 1 year ago
jmarsh24 commented 1 year ago

Integrating Sentry with Our Applications

Backend (Rails Application)

1. Add Sentry Gems to Your Gemfile

Add the sentry-ruby and sentry-rails gems to your Gemfile.

# Gemfile
gem 'sentry-ruby'
gem 'sentry-rails'

2. Initialize Sentry in Rails Application

Create an initializer config/initializers/sentry.rb with the following configuration:

# config/initializers/sentry.rb

Sentry.init do |config|
  config.dsn = Config.sentry_dsn!
  config.breadcrumbs_logger = [:active_support_logger, :http_logger]
  config.environment = Rails.env
  config.release = Config.release_version
  config.sanitize_fields = %w(password credit_card)
  config.enabled_environments = %w(production staging)
end

Replace 'https://YOUR_SENTRY_DSN' with your actual Sentry DSN.

Frontend (Typescript)

1. Add Sentry Browser to Your Project

Run the following command to add @sentry/browser to your project:

yarn add @sentry/browser

2. Initialize Sentry in Your Typescript

Create a separate file or include this in your main Typescript file to initiate Sentry:

// javascript/lib/error-tracking.ts

import * as Sentry from '@sentry/browser';

Sentry.init({
  dsn: import.meta.env.SENTRY_DSN,
  environment: import.meta.env.SENTRY_ENVIRONMENT || import.meta.env.RAILS_ENV,
});

Add rails credentials:edit to the rails encrypted credentials folder. (Be sure that you have the master.key copied from heroku RAILS_MASTER_KEY).

SENTRY_DSN=https://YOUR_SENTRY_DSN

Replace 'https://YOUR_SENTRY_DSN' with your actual Sentry DSN.

Integrating the Rails Request ID

1. Capture Request ID in the Controller

# app/controllers/application_controller.rb

before_action :set_sentry_context

def set_sentry_context
  Sentry.set_context("Request ID", { request_id: request.uuid })
end

2. Displaying Request ID on 500 Error Pages

Configure Rails to Show Custom Error Pages:

Add the route at the top of the config/routes.rb:

# config/routes.rb

match "/404", to: "errors#not_found", via: :all
match "/500", to: "errors#internal_server_error", via: :all

Create an ErrorsController.

# app/controllers/errors_controller.rb

class ErrorsController < ApplicationController
  skip_after_action :verify_authorized, only: [:not_found, :internal_server_error]
  skip_before_action :assign_user, only: [:not_found, :internal_server_error]
  skip_before_action :check_locale
  before_action :assign_exception
  before_action :capture_exception

  def not_found
    render status: :not_found
  end

  def internal_server_error
    render status: :internal_server_error
  end

  private

  def assign_exception
    @exception = request.env["action_dispatch.exception"]
  end

  def capture_exception
    Rails.error.report(@exception, handled: true)
  end
end

Display the Request ID on the Error Page

<!-- app/views/errors/internal_server_error.html.slim -->
= request.uuid

Responding to Errors in Sentry

Standard Operating Procedure (SOP)

Follow these initial steps when an error is reported:

  1. Acknowledge the error in Sentry.
  2. Examine the error context and stack trace in Sentry.
  3. Assess the severity and impact of the error.

Documentation for Each Error

Team Communication and Coordination

jmarsh24 commented 1 year ago

@JensRavens, @danieldiekmeier, and @Adeynack I have created a simple guide based on anymator to add error tracking with sentry. Is there anything you would like to add above?

Where should this documentation live?

We could also consider to hook up projects/organization GitHub with sentry so it automatically creates issues for us so we don't have to do it manually. I don't have permissions on any project or the organization to see these settings though so @JensRavens or @ckroter would have to do it.

https://docs.sentry.io/product/integrations/source-code-mgmt/github/ https://resources.github.com/actions/integrating-with-sentry/

danieldiekmeier commented 1 year ago

I think we don't have to add the Request ID to Sentry manually, Sentry seems to support this out of the box: https://github.com/getsentry/sentry-ruby/pull/1120

Adeynack commented 12 months ago

Where should this documentation live?

I still cannot offer a clear answer to that. @JensRavens what's our stance there at the moment? What about a markdown file? Should it be here in shimmer, or should it be in development-environment? Or in the Handbook?