Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Tracking #71

Open
jmarsh24 opened this issue Aug 16, 2023 · 4 comments
Open

Error Tracking #71

jmarsh24 opened this issue Aug 16, 2023 · 4 comments

Comments

@jmarsh24
Copy link
Member

  • document how error tracking should work in our apps (e.g. where to put sentry with which settings)
  • make sure the rails request id is submitted to sentry and shown on 500 pages so we can look up an error if it happens
  • figure out what to do when an error appears on sentry (and document it!)
@jmarsh24
Copy link
Member Author

jmarsh24 commented Aug 16, 2023

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

  • Create a ticket in your issue tracking system, linking to the error in Sentry.
  • Consider rolling back a recent deployment if necessary.

Team Communication and Coordination

  • Alert the team in the nerdgeschoss slack channel and tag the relavent people.

@jmarsh24
Copy link
Member Author

jmarsh24 commented Aug 16, 2023

@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
Copy link
Contributor

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

@Adeynack
Copy link
Contributor

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants