RailsにNew Relic APMを導入する時に監視対象のホストを限定する方法を紹介します。
New Relic APMの導入方法と設定
- https://docs.newrelic.com/docs/agents/ruby-agent/installation/install-new-relic-ruby-agent
- https://docs.newrelic.com/docs/agents/ruby-agent/configuration/ruby-agent-configuration
なんで監視するホストを限定したいのか
New Relic APM のProはホスト課金です。
契約台数の都合上、特定のサーバーだけに限定したいときがあるからです。
設定手順
production環境の特定ホストのみ監視させたい場合の例
-
config/custom_new_relic_configuration.rbを読み込む設定を追加します
config/application.rbrequire_relative './custom_new_relic_configuration'
-
特定ホストか判定するメソッドを作成します。
config/custom_new_relic_configuration.rbrequire 'socket' class CustomNewRelicConfiguration ENABLED_NEWRELIC_HOST = 'host.example.com' def self.enable_on_current_host? ENABLED_NEWRELIC_HOST == Socket.gethostname end end
-
デフォルトでは監視させないようにして、productionだけ作成したメソッドで判定させます。
config/newrelic.ymlcommon: &default_settings monitor_mode: false production: <<: *default_settings monitor_mode: <%= CustomNewRelicConfiguration.enable_on_current_host? %>