Sentry完全ガイド
Sentry完全ガイド
Section titled “Sentry完全ガイド”Sentryの実践的な使い方を、実務で使える実装例とベストプラクティスとともに詳しく解説します。
1. Sentryとは
Section titled “1. Sentryとは”Sentryの特徴
Section titled “Sentryの特徴”Sentryは、エラー追跡とパフォーマンス監視を提供するプラットフォームです。
Sentryの特徴 ├─ エラー追跡 ├─ パフォーマンス監視 ├─ リリース追跡 ├─ ユーザーコンテキスト └─ 統合通知なぜSentryを選ぶのか
Section titled “なぜSentryを選ぶのか”Sentryを選ぶべき場合:
- エラー追跡が重要
- パフォーマンス監視が必要
- リリース追跡が必要
- ユーザーコンテキストが必要
2. Sentryのセットアップ
Section titled “2. Sentryのセットアップ”アカウントの作成
Section titled “アカウントの作成”- Sentry公式サイトでアカウントを作成
- プロジェクトを作成し、DSNを取得
Node.jsでのインストール
Section titled “Node.jsでのインストール”npm install @sentry/node @sentry/tracingconst Sentry = require('@sentry/node');const Tracing = require('@sentry/tracing');
Sentry.init({ dsn: 'your_dsn', environment: 'production', tracesSampleRate: 1.0, integrations: [ new Tracing.Integrations.Express({ app }), ],});
const express = require('express');const app = express();
app.use(Sentry.Handlers.requestHandler());app.use(Sentry.Handlers.tracingHandler());
app.get('/api/users', (req, res) => { // 処理 res.json({ users: [] });});
app.use(Sentry.Handlers.errorHandler());
app.listen(3000);Pythonでのインストール
Section titled “Pythonでのインストール”pip install sentry-sdkimport sentry_sdkfrom sentry_sdk.integrations.flask import FlaskIntegration
sentry_sdk.init( dsn='your_dsn', integrations=[FlaskIntegration()], traces_sample_rate=1.0, environment='production')
from flask import Flaskapp = Flask(__name__)
@app.route('/api/users')def get_users(): # 処理 return {'users': []}
if __name__ == '__main__': app.run()3. エラー追跡
Section titled “3. エラー追跡”手動でのエラー報告
Section titled “手動でのエラー報告”// Node.jsの例const Sentry = require('@sentry/node');
try { // 処理} catch (error) { Sentry.captureException(error, { tags: { section: 'order_processing', user_id: '12345' }, extra: { order_id: '67890', amount: 100.00 } }); throw error;}# Pythonの例import sentry_sdk
try: # 処理 passexcept Exception as e: sentry_sdk.capture_exception(e, { 'tags': { 'section': 'order_processing', 'user_id': '12345' }, 'extra': { 'order_id': '67890', 'amount': 100.00 } }) raiseメッセージの報告
Section titled “メッセージの報告”// メッセージの報告Sentry.captureMessage('Something went wrong', { level: 'warning', tags: { component: 'payment' }});4. パフォーマンス監視
Section titled “4. パフォーマンス監視”トランザクションの追跡
Section titled “トランザクションの追跡”// Node.jsの例const transaction = Sentry.startTransaction({ op: 'process_order', name: 'Process Order'});
const span = transaction.startChild({ op: 'database', description: 'Query orders'});
// データベースクエリspan.finish();
transaction.finish();# Pythonの例import sentry_sdk
with sentry_sdk.start_transaction(op='process_order', name='Process Order'): with sentry_sdk.start_span(op='database', description='Query orders'): # データベースクエリ passカスタムインストルメンテーション
Section titled “カスタムインストルメンテーション”// カスタムスパンの作成const span = Sentry.getCurrentHub().getScope().getSpan();const childSpan = span.startChild({ op: 'external_api', description: 'Call payment API'});
// 外部API呼び出しchildSpan.finish();5. ユーザーコンテキスト
Section titled “5. ユーザーコンテキスト”ユーザー情報の設定
Section titled “ユーザー情報の設定”// Node.jsの例Sentry.setUser({ id: '12345', email: 'user@example.com', username: 'alice', ip_address: '192.168.1.1'});# Pythonの例import sentry_sdk
sentry_sdk.set_user({ 'id': '12345', 'email': 'user@example.com', 'username': 'alice', 'ip_address': '192.168.1.1'})コンテキストの追加
Section titled “コンテキストの追加”// コンテキストの追加Sentry.setContext('order', { order_id: '12345', amount: 100.00, status: 'processing'});6. リリース追跡
Section titled “6. リリース追跡”リリースの設定
Section titled “リリースの設定”// Node.jsの例Sentry.init({ dsn: 'your_dsn', release: 'myapp@1.0.0', environment: 'production'});# Pythonの例sentry_sdk.init( dsn='your_dsn', release='myapp@1.0.0', environment='production')リリースのデプロイ
Section titled “リリースのデプロイ”# Sentry CLIでのデプロイ記録sentry-cli releases new 1.0.0sentry-cli releases set-commits 1.0.0 --autosentry-cli releases finalize 1.0.0sentry-cli releases deploys 1.0.0 new -e production7. パフォーマンス最適化
Section titled “7. パフォーマンス最適化”サンプリング
Section titled “サンプリング”// トランザクションのサンプリングSentry.init({ dsn: 'your_dsn', tracesSampleRate: 0.1, // 10%のサンプリング beforeSend(event, hint) { // イベントのフィルタリング if (event.exception) { const error = hint.originalException; if (error && error.statusCode === 404) { return null; // 404エラーは無視 } } return event; }});パフォーマンスバジェット
Section titled “パフォーマンスバジェット”// パフォーマンスバジェットの設定Sentry.init({ dsn: 'your_dsn', tracesSampleRate: 1.0, beforeSendTransaction(event) { // 遅いトランザクションのみ記録 if (event.transaction && event.transaction.duration > 1000) { return event; } return null; }});8. アラートの設定
Section titled “8. アラートの設定”アラートルールの作成
Section titled “アラートルールの作成”// Sentry APIでのアラート作成const axios = require('axios');
const alertRule = { name: 'High Error Rate', conditions: [ { id: 'sentry.rules.conditions.event_frequency.EventFrequencyCondition', interval: '1m', value: 10 } ], actions: [ { id: 'sentry.rules.actions.notify_event_service.NotifyEventServiceAction', service: 'slack', channel: '#alerts' } ]};
const response = await axios.post( `https://sentry.io/api/0/projects/${org}/${project}/alert-rules/`, alertRule, { headers: { 'Authorization': `Bearer ${api_token}` } });Slack統合
Section titled “Slack統合”// Slack通知の設定// SentryのWeb UIで設定// Settings → Integrations → SlackGitHub統合
Section titled “GitHub統合”// GitHub統合の設定// Settings → Integrations → GitHub// コミット情報が自動的にリンクされる10. 実践的なベストプラクティス
Section titled “10. 実践的なベストプラクティス”エラーの分類
Section titled “エラーの分類”// エラーの分類Sentry.captureException(error, { tags: { error_type: 'database', severity: 'high' }, fingerprint: ['database-error', '{{ default }}']});コンテキストの管理
Section titled “コンテキストの管理”// スコープの使用Sentry.withScope((scope) => { scope.setTag('section', 'payment'); scope.setLevel('warning'); Sentry.captureMessage('Payment processing slow');});パフォーマンスの最適化
Section titled “パフォーマンスの最適化”// 不要なデータの除外Sentry.init({ dsn: 'your_dsn', beforeSend(event, hint) { // 機密情報の除外 if (event.request) { delete event.request.cookies; delete event.request.headers['Authorization']; } return event; }});11. よくある問題と解決方法
Section titled “11. よくある問題と解決方法”問題1: エラーが記録されない
Section titled “問題1: エラーが記録されない”// 解決: DSNと環境設定の確認Sentry.init({ dsn: 'your_dsn', environment: 'production', debug: true // デバッグモードで確認});問題2: パフォーマンスの低下
Section titled “問題2: パフォーマンスの低下”// 解決: サンプリングの使用Sentry.init({ dsn: 'your_dsn', tracesSampleRate: 0.1 // 10%のサンプリング});問題3: コストの増加
Section titled “問題3: コストの増加”// 解決: イベントのフィルタリングSentry.init({ dsn: 'your_dsn', beforeSend(event, hint) { // 不要なイベントを除外 if (event.level === 'info') { return null; } return event; }});Sentry完全ガイドのポイント:
- エラー追跡: 詳細なエラー情報の収集
- パフォーマンス監視: トランザクションとスパンの追跡
- ユーザーコンテキスト: ユーザー情報の追加
- リリース追跡: リリースとデプロイの追跡
- アラート: 自動通知
- 統合: Slack、GitHubなどとの統合
- ベストプラクティス: サンプリング、フィルタリング
適切なSentryの使用により、効果的なエラー追跡とパフォーマンス監視システムを構築できます。