LoginSignup
8
8

More than 5 years have passed since last update.

Rails アプリから Fluent 経由で Bugsnag に送信する

Last updated at Posted at 2015-04-23

「とりあえず、やってみました」という記録です。

  • Fluent(d) 、呼び方すらよく分かってません
  • Bugsnag、最近トライアル始めた程度の知見です
  • gem、誰かに使ってもらう事を考えたリリースになってません
  • 開発・実行環境
    • OSX 10.10.3
    • Rails 4.2.1
    • td-agent-2.1.4-0.dmg

TL;DR

  1. koshigoe/logging-bugsnagTwP/logging を拡張
  2. koshigoe/bugsnag-delivery-fluentbugsnag/bugsnag-ruby を拡張
    • deliver を拡張し Fluent に URL とリクエストボディを渡す
  3. koshigoe/fluent-plugin-bugsnag で Fluentd から Bugsnag に送信する

Fluent

fluent-plugin-bugsnag

Fluent から Bugsnag へ送信するプラグイン koshigoe/fluent-plugin-bugsnag をインストールする。

$ sudo td-agent-gem install fluent-plugin-bugsnag

td-agent.conf

koshigoe/bugsnag-delivery-fluent の利用を想定しているので、タグ bugsnag.deliver を対象に bugsnag プラグインを適用する設定を追加。

<source>
  type forward
</source>

<match bugsnag.deliver>
  type bugsnag

  # bugsnag_proxy_host localhost
  # bugsnag_proxy_port 8888
  # bugsnag_proxy_user user
  # bugsnag_proxy_password password
  # bugsnag_timeout 10
</match>

設定が用意できたら td-agent を起動。

$ td-agent -c td-agent.conf

アプリケーション

Rails アプリを想定。

Gemfile

gem 'logging-rails'
gem 'logging-bugsnag'
gem 'bugsnag-delivery-fluent'
$ bundle install

config/logging.rb

TwP/logging-rails を使うので、ジェネレータで設定ファイルを作成する。

$ rails generate logging:install

作成した config/logging.rb に以下を追記して bugsnag appender を使える様にしておく。

Logging.appenders.bugsnag('bugsnag', level: :error) if config.log_to.include? 'bugsnag'

config.log_to

config/environments/production.rb などで config.log_to'bugsnag' を追加すれば logger に組み込まれる。

config.log_to = %w[file bugsnag]

config/initializers/bugsnag.rb

アプリケーションから直接 Bugsnag に送信せず Fluentd に送信を任せるために、 delivery_method:fluent とする。

Bugsnag.configure do |config|
  config.api_key = ENV['BUGSNAG_API_KEY']
  config.logger = ActiveSupport::Logger.new(Rails.root.join('log/bugsnag.log'), 10, 100.megabytes)
  config.delivery_method = :fluent
  # config.fluent_tag_prefix = 'bugsnag'
  # config.fluent_host = 'localhost'
  # config.fluent_port = 24224
end

利用開始

ここまでで、捕捉してない例外と error 以上のログ出力が Fluent に送られ、Fluent で適宜 Bugsnag に送信される様になる。

$ rails c
> Rails.logger.error('TEST TEST TEST')
> Bugsnag.notify(RuntimeError.new('TETETETE TEST'))
8
8
1

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
8
8