安装和设置Errbit

首先为了收集Rails应用程序的应用程序错误,我搭建了一个Errbit服务器,并进行了Rails应用程序的设置。我打算在这里写下部署时的配置备忘录。

Errbit是什么?Errbit是一种用于收集应用程序错误的工具。通常的监控工具通过使用Ping等方式来监视服务器的存活或使用SNMP来监视服务器状态,但Errbit与它们不同。

Errbit所收集的信息是Web应用程序中发生的异常处理错误等。
通过收集异常处理,可以了解运营中出现的问题,从而能够高效地进行错误修复和重构。

Web应用程序的基础是Ruby(Rails),但似乎也可以使用PHP或Python来开发。

    https://github.com/errbit/errbit#use-errbit-with-applications-written-in-other-languages

Github Errbit 可以被翻译为 “Github捕获错误”

    https://github.com/errbit/errbit

我们将在Web应用程序中使用Airbrake(airbrake-ruby)。Airbrake是一个与Errbit相同的错误收集系统,具有API的兼容性,我们将使用Gem airbrake-ruby来向Errbit发送通知。

    https://github.com/airbrake/airbrake-ruby

环境我在 Amazon EC2 上构建了一个 Amazon Linux 实例。

Erribt服务器

    • MongoDB version v3.2.16

 

    • Ruby 2.4.1

 

    • Nginx 1.10.3

 

    Errbit 0.7.0-dev

网络应用服务器

    • Ruby 2.4.1

 

    • Rails 5.1.1

 

    Airbrake 6.2.1

请注意Airbrake这个用于Errbit的Gem在5版本之后接口发生了重大变化。如果要引入新的Errbit,则没有问题,但是需要注意Errbit服务器和Airbrake Gem的版本问题。

    https://github.com/airbrake/airbrake/blob/master/docs/Migration_guide_from_v4_to_v5.md

引入Errbit

安装MongoDB

只需要一个选项,用母语中文对以下内容进行改写:参考文献

    • Install MongoDB Community Edition on Amazon Linux — MongoDB Manual 3.4

 

    Amazon EC2 — MongoDB Ecosystem
# リポジトリ情報追加
$ echo "[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc" |
  sudo tee -a /etc/yum.repos.d/mongodb-org-3.2.repo

# インストール
$ sudo yum -y update && sudo yum install -y mongodb-org-server \
    mongodb-org-shell mongodb-org-tools

# バージョン確認
$ $ mongod -version
db version v3.2.16
git version: 056bf45128114e44c5358c7a8776fb582363e094
OpenSSL version: OpenSSL 1.0.0-fips 29 Mar 2010
allocator: tcmalloc
modules: none
build environment:
    distmod: amazon
    distarch: x86_64
    target_arch: x86_64

# EBSボリュームは使用しないので、ボリュームマウント関連はスキップ
# /etc/mongo.confなどもデフォルト値を使用

# リソース使用に関する設定(ulimit)を変更
$ echo '* soft nofile 64000
* hard nofile 64000
* soft nproc 64000
* hard nproc 64000' | sudo tee /etc/security/limits.d/90-mongodb.conf

# MongoDBの起動
$ sudo service mongod start
$ sudo chkconfig mongod on

# Mongo shellのテスト
$ mongo
MongoDB shell version: 3.2.16
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> exit

引用以下内容:安装Ruby以及相关软件包集合。

请提供更多的上下文或者详细信息,以便我能够为您准确地提供中文的同义句。

    Amazon LinuxにRubyの実行環境を構築してみる | Developers.IO
# 必要パッケージのインストール
$ sudo yum -y install git gcc gcc-c++ openssl-devel readline-devel zlib-devel

# rbenvをダウンロード
$ git clone git://github.com/sstephenson/rbenv.git .rbenv


# 環境変数などの設定
$ PATH="$HOME/.rbenv/bin:$PATH"
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile


# ruby-buildをダウンロード
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build


# rubyインストール
$ rbenv install 2.4.1
$ rbenv local 2.4.1
$ rbenv rehash


# bundlerインストール
$ gem install bundler
$ bundle -v


# git設定
$ git init
$ git config --local user.name '[githubに登録したユーザ名]'
$ git config --local user.email '[githubに登録したメールアドレス]'

安装Errbit

# githubからリポジトリをクローン
$ cd /var/rails
$ git clone https://github.com/errbit/errbit.git

$ cd errbit

# 必要なGemをインストール
$ bundle install --path vendor/bundle


$ bundle exec rake errbit:bootstrap
省略
-------------------------------
Creating an initial admin user:
-- email:    errbit@errbit.example.com
-- password: xxxxxxx
Be sure to note down these credentials now!


# pumaを動かしErrbit起動
$ bundle exec puma -C config/puma.default.rb

请记下此处显示的电子邮件/密码。
此时,应该能够查看Errbit的WebUI。
但是,这次我们将安装Nginx,并通过Nginx访问Puma。

其他与Errbit相关的设置。

# Errbitの設定
$ vi .env
ERRBIT_HOST=[Errbitサーバのホスト名]
ERRBIT_EMAIL_FROM='[Fromに設定するメールアドレス]'
ERRBIT_PROBLEM_DESTROY_AFTER_DAYS=180
EMAIL_DELIVERY_METHOD=:smtp
SMTP_SERVER='[SNMPサーバのアドレス]'
SMTP_PORT=25

# pumaの設定
$ cp config/puma.default.rb config/puma.rb
$ vi config/puma.rb

#コメントアウト
#port ENV['PORT'] || 8080
中略

# 追加
app_dir = File.expand_path("../..", __FILE__)
daemonize true

# pid/stateファイルの格納場所を指定
pidfile "#{app_dir}/tmp/pids/puma.pid"
state_path "#{app_dir}/tmp/pids/puma.state"

# 設定のテスト、デーモン化での起動、再起動、停止
$ bundle exec pumactl start
$ bundle exec pumactl halt
$ bundle exec pumactl restart
$ vi config/puma.rb

# nginx経由でsocket接続するため、sockファイル場所を指定
bind "unix://#{app_dir}/tmp/sockets/puma.sock"

安装和配置Nginx。

# インストールとバージョン確認
$ sudo yum -y install nginx

$ nginx -v
nginx version: nginx/1.10.3

$ sudo vi /etc/nginx/conf.d/puma.conf
server {
    #root         /usr/share/nginx/html;
    root         /var/rails/errbit/public;
    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded_Proto $scheme;
            proxy_redirect off;
            proxy_pass http://unix:/var/rails/errbit/tmp/sockets/puma.sock;
    }
}

$ sudo service nginx restart
$ sudo chkconfig nginx on

在启动Nginx之后,访问Errbit的URL,并进行应用程序的设置。

    • Appタブをクリックし、画面上部にある「add a new app」ボタンを押す

 

    • 入力フォームが表示されるので必要な項目を入力

 

    その後、Webアプリケーション導入時に必要な設定が表示される

我们将记录所显示的设置。根据这个记录的设置,我们将继续进行Airbrake方面的配置。

Gem Airbrake的设置当Errbit服务器准备好之后,将Airbrake安装到目标Web应用程序中。
从Errbit服务器注销后,对Web应用程序(Rails)应用以下配置。

#  Airbrakeのインストール
$ vi Gemfile

gem 'airbrake-ruby', '~> 2.6'

$ bundle install --path vendor/bundle

# Airbrakeの設定
$ vi config/initializers/errbit.rb

# Require the airbrake gem in your App.
# ---------------------------------------------
#
# Ruby - In your Gemfile
# gem 'airbrake', '~> 5.0'
#
# Then add the following to config/initializers/errbit.rb
# -------------------------------------------------------
Airbrake.configure do |config|
  config.host = [ErrbitサーバのURL]
  config.project_id = 1 # required, but any positive integer works
  config.project_key = [Errbitで取得したKeyを利用]

  # Uncomment for Rails apps
  config.environment = Rails.env
  config.ignore_environments = %w(development test)
end

如果正确设置了Rails.env,config.environment将成为在Errbit上显示的环境名称,并自动识别staging、production等。

设置完成后,使用以下命令进行测试。

bundle exec rake airbrake:test

我应该通过此命令通知Errbit有关测试错误。
请访问WebUI进行确认。

对通知错误内容进行筛选

$ vi config/initializers/errbit.rb

# 下記を追記
# The library supports nested exceptions, so one notice can carry several
# exceptions.
# Accesses a notice's payload, which can be read or filtered. Payload includes:
# - :errors
# - :context
# - :environment
# - :session
# - :params
# https://github.com/airbrake/airbrake-ruby#airbrakeadd_filter

Airbrake.add_filter do |notice|
   # モデルSampleClassのRecordNotFoundを通知から除外
   notice.ignore! if notice[:errors].any? do |error|
     error[:type] == 'ActiveRecord::RecordNotFound'
     error[:message] =~ /Couldn't find SampleClass with/
   end
end

在Errbit中,您可以添加不需要通知的异常。使用上述设置。

    • Airbrakeから送られるエラータイプがActiveRecord::RecordNotFound

エラー内容に Couldn’t find SampleClass withが含まれている

如果满足上述条件,则忽略通知。
即使是设置文件,由于采用了Ruby的DSL,您可以像编写Ruby代码一样编写条件。

如果在应用程序运行过程中发现不必要的通知,请添加错误。
通知的内容几乎与Errbit的WebUI中的错误通知项目相同。
如果想要实际查看内部内容,请使用Pry/Pry-byebug来查看通知的内容。

按照中国母语的要求,对以下内容进行释义: Pry-byebug

    https://github.com/deivid-rodriguez/pry-byebug:embed

我希望在网页应用程序中能够通知任意错误。有时候可能希望筛选错误,但也可能希望在Errbit中记录任意错误。若要记录,请使用Airbrake.notify()函数。

以下的代码是使用rails控制台进行测试的。

$ bundle exec rails console

[1] pry(main)> require 'airbrake-ruby'
[2] pry(main)> Airbrake.notify('App crashed!')

在已设定状态下,执行上述命令将将通知发送到Errbit。可以在Errbit的WebUI上查看详细信息。

如果想要更精细的实现,就进行以下操作。

    https://github.com/airbrake/airbrake-ruby

请确认。

最后導入初期的時候,收到了很多不必要的錯誤通知,但是通過使用篩選器我們能夠減少通知。
雖然還沒有測試所有的錯誤通知,但是它似乎很方便查看錯誤詳細信息。

bannerAds