LoginSignup
1
0

More than 3 years have passed since last update.

RailsロガーからStackdriverに切り替える

Last updated at Posted at 2019-12-19

はじめに

  • 当該記事はたたき台のため、追記予定

システム情報

  • Ruby 2.6.5
  • Rails 5.2.3
  • bundler 1.17.2
  • puma
  • rbenv local環境下
Terminal
$ rbenv versions
  system
  2.4.1
* 2.6.5 (set by /Users/%user%/%repository%/.ruby-version)
$ bundle exec rails -v
Rails 5.2.3

Stackdriverに切り替える

これまでの構成

  • Rails.loggerのformatterを config gem で環境毎に設定(或いは settings.yml でも良い)
config/settings/development.yml
…途中省略…
# logging setting
# fatal   システムクラッシュ(システムエラー)
# error   アプリケーションエラー
# warn    警告
# info    プログラム通知
# debug   開発者向けの情報
logger:
  formatter: 'proc {|severity, datetime, progname, msg| "severity:[#{severity.rjust(5, " ")}]\tdatetime:#{datetime.strftime("%Y/%m/%d %H:%m:%S")} pid:#{$$}\tmessage:#{msg}\n"}'
production.rb|development.rb
config.log_formatter = eval Settings.logger.formatter
Log
severity:[ INFO]    datetime:2019/12/19 00:00:00 pid:20026  message:Started GET "/api_v2/users?first_name=ほげお&gender=male&zip_code=100-0001" for ::1 at 2019-12-19 00:00:00 +0900
severity:[DEBUG]    datetime:2019/12/19 00:00:00 pid:20026  message:   (8.5ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
severity:[DEBUG]    datetime:2019/12/19 00:00:00 pid:20026  message:  ↳ vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
severity:[DEBUG]    datetime:2019/12/19 00:00:00 pid:20026  message:   (8.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
severity:[DEBUG]    datetime:2019/12/19 00:00:00 pid:20026  message:  ↳ vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
severity:[ INFO]    datetime:2019/12/19 00:00:01 pid:20026  message:Processing by ApiV2::UsersController#index as */*

これに logrotateの組み合わせ でも全く構わないとは思うのですが、今回はこれをStackdriverに切り替えたいと思います。

Stackdriverとは

スイートに含まれるパッケージ・ライブラリ

Stackdriver Debugger
  • コードの動作を調査し、デバッグを行える
  • gem を組み込んだら、Cloud Console に移動し、コードをアップロードします(または、Stackdriver Debugger でリポジトリ内のコードを指定します)。すると、実行中のアプリケーションの変数値とスタックトレースを含むスナップショットを取得
  • 実行中のアプリケーションに任意のログ文を追加することも可能
Stackdriver Error Reporting
  • 例外を検出して報告する Google Cloud ツール
  • アプリケーションのクラッシュを捉え、それを論理的にグループ化して通知してくれる
    • UI(エラー管理インターフェース)に整理してわかりやすく表示
    • UI には、エラーのスタックトレースや、各クラッシュのログおよび分散トレースへのリンクが表示される
    • グループ化されたバグ データベースから発生したバグを関連づけることができる
Stackdriver Trace
  • アプリケーションのパフォーマンス モニタリングと分散トレーシングを行う
  • リクエストがデータベースに到達し、ビューがレンダリングされ、アプリケーション ロジックの実行に要した時間が自動的に表示される
  • リクエストがマイクロサービス アーキテクチャでどのように処理されるかを表示
    • レイテンシの時系列の傾向に関する詳細レポートを提供
    • 最新リリースへの更新後、アプリケーションに遅延はないかチェックできる
Stackdriver Logging

導入手順

  1. GCPプロジェクトの請求先設定がされていることを確認※設定されていないと使用できない模様

2. GCPプロジェクト上にStackdriver Logging APIをインストールする

3. Gemを追加する

Terminal
vi Gemfile
Gemfile
# logging
gem 'stackdriver-ruby', require: 'stackdriver'
gem 'stackdriver'

4. Bundle install (gem installでも可)

Terminal
$ bundle _1.17.2_ install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 13.0.0
…途中省略…
Fetching debug_inspector 0.0.3
Installing debug_inspector 0.0.3 with native extensions
Fetching binding_of_caller 0.8.0
Installing binding_of_caller 0.8.0 with native extensions
…途中省略…
Using listen 3.1.5
Fetching multi_json 1.14.1
Installing multi_json 1.14.1
Using mysql2 0.5.2
…途中省略…
Fetching google-cloud-env 1.3.0
Installing google-cloud-env 1.3.0
Fetching google-cloud-core 1.4.1
Installing google-cloud-core 1.4.1
Fetching google-protobuf 3.11.2 (universal-darwin)
Installing google-protobuf 3.11.2 (universal-darwin)
Fetching googleapis-common-protos-types 1.0.4
Installing googleapis-common-protos-types 1.0.4
Fetching grpc 1.25.0 (universal-darwin)
Installing grpc 1.25.0 (universal-darwin)
Fetching googleapis-common-protos 1.3.9
Installing googleapis-common-protos 1.3.9
Fetching jwt 2.2.1
Installing jwt 2.2.1
Fetching memoist 0.16.2
Installing memoist 0.16.2
…途中省略…
Using spring-watcher-listen 2.0.1
Fetching stackdriver-ruby 0.0.1
Installing stackdriver-ruby 0.0.1
…途中省略…
Fetching os 1.0.1
Installing os 1.0.1
Fetching signet 0.12.0
Installing signet 0.12.0
Fetching googleauth 0.10.0
Installing googleauth 0.10.0
Fetching rly 0.2.3
Installing rly 0.2.3
Fetching google-gax 1.8.1
Installing google-gax 1.8.1
Fetching stackdriver-core 1.4.0
Installing stackdriver-core 1.4.0
Fetching google-cloud-logging 1.9.1
Installing google-cloud-logging 1.9.1
Fetching google-cloud-debugger 0.35.1
Installing google-cloud-debugger 0.35.1 with native extensions
Fetching google-cloud-error_reporting 0.34.0
Installing google-cloud-error_reporting 0.34.0
Fetching google-cloud-trace 0.36.1
Installing google-cloud-trace 0.36.1
…途中省略…
Using uglifier 4.2.0
Using web-console 3.7.0
Bundle complete! 31 Gemfile dependencies, 135 gems now installed.
Bundled gems are installed into `./vendor/bundle`

5. Railsコンフィグに追加する

application.rb
# Stackdriver logging settings
require "google/cloud/logging"
Google::Cloud.configure do |config|
  # Stackdriver Logging specific parameters
  config.logging.project_id = "YOUR-PROJECT-ID"
  config.logging.keyfile    = "/path/to/service-account.json"
end

6. 起動確認

Terminal
$ bundle exec rails s -p 8080
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.5-p114), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:8080
Use Ctrl-C to stop

使い方は追って記載します

1
0
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
1
0