CentOS 6.5にErrbitを構築してみたの続きです。
今回は、RailsアプリケーションでErrbitへエラー情報を送信するための設定方法を紹介します。
Gemfile
Gemfileにairbrakeのgemを追加して、bundle install
# Gemfile
gem 'airbrake' # 追加
errbit.rb
config/initializers/errbit.rb
にErrbitへの設定を記載します。
# config/initializers/errbit.rb
Airbrake.configure do |config|
config.api_key = 'xxxxxxxxxxxxxxxxxxxxxxx'
config.host = 'http://errbit.example.com'
config.port = 80
config.secure = config.port == 443
config.use_system_ssl_cert_chain = true
end
config.api_key
はErrbit(GUI)でアプリを登録する際に、表示されるAPIキーを指定します- デフォルトでは、Airbrake自身のca-bundle.crtを参照してしまうため、自己証明書を使う場合は、
config.use_system_ssl_cert_chain = true
を設定する必要があります
テスト
テスト用コマンドがあるので実行します。
$ bundle exec rake airbrake:test RAILS_ENV=production
Configuration:
api_key: "xxxxxxxxxxxxxxxxxxxxxxx"
backtrace_filters: [#<Proc:0x007fdef033fe10@/app/sanmple_rails
development_environments: []
development_lookup: true
environment_name: "production"
host: "errbit.example.com"
http_open_timeout: 2
http_read_timeout: 5
ignore: ["ActiveRecord::RecordNotFound", "ActionController::Rou
ignore_by_filters: []
ignore_user_agent: []
notifier_name: "Airbrake Notifier"
notifier_url: "https://github.com/airbrake/airbrake"
notifier_version: "4.1.0"
params_filters: ["password", "password_confirmation"]
project_root: #<Pathname:/app/sample>
port: 80
protocol: "http"
proxy_host: nil
proxy_pass: nil
proxy_port: nil
proxy_user: nil
secure: false
use_system_ssl_cert_chain: false
framework: "Rails: 4.2.0"
user_information: "Airbrake Error {{error_id}}"
rescue_rake_exceptions: nil
rake_environment_filters: []
test_mode: nil
Setting up the Controller.
Processing request.
Started GET "/verify" for at 2015-03-19 06:08:24 +0000
Raising 'AirbrakeTestingException' to simulate application failure.
** [Airbrake] Success: Net::HTTPOK
** [Airbrake] Environment Info: [Ruby: 2.1.2] [Rails: 4.2.0] [Env: production]
** [Airbrake] Response from Airbrake:
UUID: 550a67d8696369095c000000
URL: http://errbit.example.com/locate/550a67d8696369095c000000
Testing airbrake via "rake airbrake:test". If you can see this, it works.
上のように表示されていればOKです。 Errbit(GUI)にアクセスして送信されたエラーを確認してみてください。
蛇足
APIキーの編集
ステージング環境、本番環境など複数環境でErrbitを構築し、それぞれに同じアプリを登録することがあると思います。その場合、同じアプリでもAPIキーが異なってしまうため、同じAPIキーを使いたい場合は、以下のようにMongoDBに格納されているデータを変更します。
$ mongo
Foo:PRIMARY> use errbit
switched to db errbit
# 現在の設定を確認
Foo:PRIMARY> db.apps.find({"name": "foo"});
{ "_id" : "551a0ed661646d072c010000", "notify_on_errs" : true, "email_at_notices" : [ 1, 10, 100 ], "resolve_errs_on_deploy" : false, "notify_all_users" : false, "notify_on_deploys" : true, "name" : "foo", "repository_branch" : "", "github_repo" : "", "bitbucket_repo" : "", "asset_host" : "", "current_app_version" : "", "api_key" : "<old_api_key>", "updated_at" : ISODate("2015-03-31T03:04:54.439Z"), "created_at" : ISODate("2015-03-31T03:04:54.439Z"), "watchers" : [ { "_id" : ObjectId("551a0ed661646d072c020000"), "user_id" : null, "email" : "ohsawa@sansan.com" } ], "notification_service" : { "_id" : ObjectId("551a0ed661646d072c030000"), "notify_at_notices" : [ 1, 10, 100 ], "_type" : "NotificationServices::WebhookService", "api_token" : "" } }
# api_keyを更新
Foo:PRIMARY> db.apps.update({"name": "foo"}, {$set: {"api_key": "<new_api_key>"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
# 更新されたか確認
Foo:PRIMARY> db.apps.find({"name": "foo"});
{ "_id" : "551a0ed661646d072c010000", "notify_on_errs" : true, "email_at_notices" : [ 1, 10, 100 ], "resolve_errs_on_deploy" : false, "notify_all_users" : false, "notify_on_deploys" : true, "name" : "foo", "repository_branch" : "", "github_repo" : "", "bitbucket_repo" : "", "asset_host" : "", "current_app_version" : "", "api_key" : "<new_api_key>", "updated_at" : ISODate("2015-03-31T03:04:54.439Z"), "created_at" : ISODate("2015-03-31T03:04:54.439Z"), "watchers" : [ { "_id" : ObjectId("551a0ed661646d072c020000"), "user_id" : null, "email" : "ohsawa@sansan.com" } ], "notification_service" : { "_id" : ObjectId("551a0ed661646d072c030000"), "notify_at_notices" : [ 1, 10, 100 ], "_type" : "NotificationServices::WebhookService", "api_token" : "" } }