LoginSignup
0
1

More than 3 years have passed since last update.

SentryでRailsアプリのエラーをトラッキング

Last updated at Posted at 2020-09-18

契約

以下より契約
https://sentry.io/signup/

契約時にプロジェクトを作成することになり、プロジェクト作成後の画面にセットアップ方法が書かれているので、手順通りに進めていけば問題無いはず。ただ念のため自分ようにまとめなおします。

基本的な使い方

Gemfile
gem "sentry-raven"
$ bundle install
config/application.rb
module AppName
  class Application < Rails::Application
    # 省略
    Raven.configure do |config|
      config.dsn = "https://#{ENV['SENTRY_KEY']}@#{ENV['SENTRY_SECRET']}.sentry.io/#{ENV['SENTRY_ID']}"
    end
  end
end

Sentryのサイトで表示されるセットアップ方法には、ID等が全て直書きされているので、環境変数にしまいました。

パラメーターやsession情報を拾えるようにする

app/controllers/application.rb
class ApplicationController < ActionController::Base
  before_action :set_raven_context

  private

  def set_raven_context
    Raven.user_context(id: session[:current_user_id]) # or anything else in session
    Raven.extra_context(params: params.to_unsafe_h, url: request.url)
  end
end

パスワードを平文で記録しない

$ touch config/initializers/sentry.rb
config/initializers/sentry.rb
Raven.configure do |config|
  config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s)
end

本番環境でのエラーだけを参照する

デフォルトだと、開発環境のエラーも全て一覧で表示されてしまうので、設定を修正します。

Settings > Projects > プロジェクト名 > Environments > production以外の環境をhide
 2020-09-24 14.42.26.png

(2020/10/07追記)
上記の方法だと開発環境のエラーもメール等で通知されてしまいます。
単純に開発環境のSENTRY用のAPIキーを削除すれば、開発環境のエラーは捕捉されなくなります。

参考

その他

UIがめっちゃSlack笑
 2020-09-18 16.23.44.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1