New Relic完全ガイド
New Relic完全ガイド
Section titled “New Relic完全ガイド”New Relicの実践的な使い方を、実務で使える実装例とベストプラクティスとともに詳しく解説します。
1. New Relicとは
Section titled “1. New Relicとは”New Relicの特徴
Section titled “New Relicの特徴”New Relicは、アプリケーションパフォーマンス監視(APM)とインフラ監視を提供するプラットフォームです。
New Relicの特徴 ├─ APM(Application Performance Monitoring) ├─ インフラ監視 ├─ ログ管理 ├─ ブラウザ監視 └─ モバイル監視なぜNew Relicを選ぶのか
Section titled “なぜNew Relicを選ぶのか”New Relicを選ぶべき場合:
- APM機能が必要
- ブラウザとモバイルの監視が必要
- 統合的な可観測性が必要
- エンタープライズ機能が必要
2. New Relicのセットアップ
Section titled “2. New Relicのセットアップ”アカウントの作成
Section titled “アカウントの作成”- New Relic公式サイトでアカウントを作成
- ライセンスキーを取得
Agentのインストール
Section titled “Agentのインストール”# Node.jsでのインストールnpm install newrelic
# Pythonでのインストールpip install newrelic3. APMの設定
Section titled “3. APMの設定”Node.jsの例
Section titled “Node.jsの例”'use strict';
exports.config = { app_name: ['My Application'], license_key: 'your_license_key', logging: { level: 'info' }, allow_all_headers: true, attributes: { exclude: [ 'request.headers.cookie', 'request.headers.authorization' ] }};require('newrelic');
const express = require('express');const app = express();
app.get('/api/users', (req, res) => { // 処理 res.json({ users: [] });});
app.listen(3000);Pythonの例
Section titled “Pythonの例”[newrelic]license_key = your_license_keyapp_name = My Applicationmonitor_mode = trueimport newrelic.agentnewrelic.agent.initialize('newrelic.ini')
from flask import Flaskapp = Flask(__name__)
@app.route('/api/users')def get_users(): # 処理 return {'users': []}
if __name__ == '__main__': app.run()4. カスタムメトリクス
Section titled “4. カスタムメトリクス”Node.jsの例
Section titled “Node.jsの例”const newrelic = require('newrelic');
// カスタムメトリクスの記録newrelic.recordMetric('Custom/ActiveUsers', 100);
// カスタムイベントの記録newrelic.recordCustomEvent('OrderCreated', { orderId: '12345', amount: 100.00, userId: 'user-123'});
// カスタムトレースnewrelic.startWebTransaction('/api/orders', function() { const transaction = newrelic.getTransaction();
newrelic.startSegment('database', true, function() { // データベースクエリ });
newrelic.startSegment('external_api', true, function() { // 外部API呼び出し });});Pythonの例
Section titled “Pythonの例”import newrelic.agent
# カスタムメトリクスの記録newrelic.agent.record_custom_metric('Custom/ActiveUsers', 100)
# カスタムイベントの記録newrelic.agent.record_custom_event('OrderCreated', { 'order_id': '12345', 'amount': 100.00, 'user_id': 'user-123'})
# カスタムトレース@newrelic.agent.function_trace()def process_order(order_id): with newrelic.agent.datastore_trace('MySQL', 'SELECT', 'orders'): # データベースクエリ pass
with newrelic.agent.external_trace('https://api.example.com', 'GET'): # 外部API呼び出し pass5. エラー追跡
Section titled “5. エラー追跡”Node.jsの例
Section titled “Node.jsの例”const newrelic = require('newrelic');
try { // 処理} catch (error) { newrelic.noticeError(error, { customAttribute: 'value' }); throw error;}Pythonの例
Section titled “Pythonの例”import newrelic.agent
try: # 処理 passexcept Exception as e: newrelic.agent.record_exception() raise6. ブラウザ監視
Section titled “6. ブラウザ監視”JavaScriptの設定
Section titled “JavaScriptの設定”<!-- HTMLに追加 --><script type="text/javascript"> window.NREUM||(NREUM={}); NREUM.init={ distributed_tracing:{enabled:true}, privacy:{cookies_enabled:true} }; NREUM.loader_config={accountID:"your_account_id",trustKey:"your_trust_key",agentID:"your_agent_id",licenseKey:"your_license_key",applicationID:"your_application_id"}; (function(){var a=document.createElement("script");a.src="https://js-agent.newrelic.com/nr-loader.min.js";a.type="text/javascript";document.getElementsByTagName("head")[0].appendChild(a);})();</script>7. ダッシュボードの作成
Section titled “7. ダッシュボードの作成”NRQL(New Relic Query Language)
Section titled “NRQL(New Relic Query Language)”-- リクエスト数の確認SELECT count(*) FROM Transaction WHERE appName = 'My Application' SINCE 1 hour ago
-- エラー率の確認SELECT percentage(count(*), WHERE error IS true) FROM Transaction WHERE appName = 'My Application' SINCE 1 hour ago
-- レスポンスタイムの確認SELECT average(duration) FROM Transaction WHERE appName = 'My Application' SINCE 1 hour ago
-- カスタムメトリクスの確認SELECT average(Custom/ActiveUsers) FROM Metric SINCE 1 hour ago8. アラートの設定
Section titled “8. アラートの設定”アラートポリシーの作成
Section titled “アラートポリシーの作成”// New Relic APIでのアラート作成const axios = require('axios');
const alertPolicy = { name: 'High Error Rate', incident_preference: 'PER_POLICY'};
const response = await axios.post( 'https://api.newrelic.com/v2/alerts_policies.json', { policy: alertPolicy }, { headers: { 'X-Api-Key': 'your_api_key' } });アラート条件の設定
Section titled “アラート条件の設定”const alertCondition = { type: 'apm_app_metric', name: 'Error Rate', enabled: true, metric: 'error_percentage', condition_scope: 'application', terms: [ { duration: 5, operator: 'above', priority: 'critical', threshold: 5, time_function: 'all' } ]};9. ログ管理
Section titled “9. ログ管理”const newrelic = require('newrelic');
// ログの送信newrelic.recordLogEvent({ message: 'User logged in', level: 'INFO', attributes: { user_id: '12345', ip_address: '192.168.1.1' }});-- ログの検索SELECT * FROM Log WHERE message LIKE '%error%' SINCE 1 hour ago
-- ログの集約SELECT count(*) FROM Log WHERE level = 'ERROR' FACET service SINCE 1 hour ago10. 実践的なベストプラクティス
Section titled “10. 実践的なベストプラクティス”トランザクション名の設定
Section titled “トランザクション名の設定”const newrelic = require('newrelic');
// トランザクション名の設定newrelic.setTransactionName('/api/orders/:id');カスタム属性の追加
Section titled “カスタム属性の追加”// リクエスト属性の追加newrelic.addCustomAttribute('user_id', '12345');newrelic.addCustomAttribute('order_id', '67890');サンプリング
Section titled “サンプリング”// サンプリングの設定newrelic.setTransactionSamplingRate(0.1); // 10%のサンプリング11. よくある問題と解決方法
Section titled “11. よくある問題と解決方法”問題1: パフォーマンスの低下
Section titled “問題1: パフォーマンスの低下”// 解決: サンプリングの使用newrelic.setTransactionSamplingRate(0.1);問題2: データの欠損
Section titled “問題2: データの欠損”// 解決: 適切な設定の確認// newrelic.jsでmonitor_modeがtrueになっているか確認問題3: コストの増加
Section titled “問題3: コストの増加”// 解決: サンプリングとデータ保持期間の調整// 不要なカスタム属性を削除New Relic完全ガイドのポイント:
- APM: アプリケーションパフォーマンス監視
- カスタムメトリクス: ビジネスメトリクスの追跡
- エラー追跡: エラーの詳細な分析
- ブラウザ監視: フロントエンドの監視
- NRQL: 強力なクエリ言語
- アラート: 自動通知
- ログ管理: 統合ログ管理
適切なNew Relicの使用により、包括的なAPMシステムを構築できます。