sentry-setup-tracing について
sentry-setup-tracingは、Sentryのパフォーマンス監視を設定するスキルです。複数の言語でトランザクション、レイテンシ、カスタムスパンを追跡する方法を示し、実装前に最新ドキュメントを確認するよう案内します。
主な機能
- 01複数言語向けSentryトレーシング設定例
- 02サンプリング率と動的サンプリングの設定
- 03カスタムスパンの作成
- 04分散トレース伝播先の設定
- 05検証とトラブルシューティング手順
活用シーン
- 01API応答時間やページ読み込みの追跡
- 02アプリケーション処理へのカスタムスパン追加
- 03トラフィックに応じたトレースサンプリングの調整
- 04Sentryのパフォーマンスデータ表示の確認
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ getsentry/ sentry-agent-skills --skill sentry-setup-tracingインストール後、Claude Code で/sentry-setup-tracingを入力して読み込まれたか確認します。
詳細
Sentry トレーシングの設定
Sentry のパフォーマンスモニタリングを設定して、トランザクションとスパンを追跡します。
このスキルを使うタイミング
- ユーザーが「トレーシングを有効にする」または「パフォーマンスモニタリングを追加する」と尋ねた場合
- API の応答時間、ページの読み込み時間、またはレイテンシを追跡したい場合
tracesSampleRateやカスタムスパンについて尋ねた場合
重要: 以下の SDK バージョン、API 名、コードサンプルは例です。API や最小バージョンが変更されている可能性があるため、実装前に必ず docs.sentry.io で確認してください。
クイックリファレンス
| プラットフォーム | 最小 SDK バージョン | 有効化 | カスタムスパン |
|---|---|---|---|
| JS/ブラウザー | 9.0.0+ | tracesSampleRate + browserTracingIntegration() | Sentry.startSpan() |
| Next.js | 9.0.0+ | 各ランタイム設定ファイルの tracesSampleRate | Sentry.startSpan() |
| Node.js | 9.0.0+ | tracesSampleRate | Sentry.startSpan() |
| Python | 0.11.2+ | traces_sample_rate | @sentry_sdk.trace または start_span() |
| Ruby | 5.0.0+ | traces_sample_rate | Sentry.with_child_span() |
JavaScript の設定
トレーシングを有効にする
Sentry.init({
dsn: "YOUR_DSN",
tracesSampleRate: 1.0, // 1.0 = 100%, lower for production
integrations: [Sentry.browserTracingIntegration()], // Browser/React only
tracePropagationTargets: ["localhost", /^https:\/\/api\./],
});
カスタムスパン
// Async operation
const result = await Sentry.startSpan(
{ name: "fetch-user", op: "http.client" },
async () => {
return await fetch("/api/user").then(r => r.json());
}
);
// Nested spans
await Sentry.startSpan({ name: "checkout", op: "transaction" }, async () => {
await Sentry.startSpan({ name: "validate", op: "validation" }, validateCart);
await Sentry.startSpan({ name: "payment", op: "payment" }, processPayment);
});
動的サンプリング
tracesSampler: ({ name, inheritOrSampleWith }) => {
if (name.includes("healthcheck")) return 0;
if (name.includes("checkout")) return 1.0;
return inheritOrSampleWith(0.1); // Respects parent sampling decision, falls back to 0.1
},
Python の設定
トレーシングを有効にする
sentry_sdk.init(
dsn="YOUR_DSN",
traces_sample_rate=1.0,
)
カスタムスパン
# Decorator
@sentry_sdk.trace
def expensive_function():
return do_work()
# Context manager
with sentry_sdk.start_span(name="process-order", op="task") as span:
span.set_data("order.id", order_id)
process(order_id)
動的サンプリング
from sentry_sdk.types import SamplingContext
def traces_sampler(sampling_context: SamplingContext) -> float:
name = sampling_context.get("transaction_context", {}).get("name", "")
parent_sampled = sampling_context.get("parent_sampled")
if "healthcheck" in name: return 0
if "checkout" in name: return 1.0
if parent_sampled is not None: return float(parent_sampled) # Respect parent decision
return 0.1
sentry_sdk.init(dsn="YOUR_DSN", traces_sampler=traces_sampler)
Ruby の設定
Sentry.init do |config|
config.dsn = "YOUR_DSN"
config.traces_sample_rate = 1.0
end
一般的な操作タイプ
op の値 | 用途 |
|---|---|
http.client | 送信 HTTP |
http.server | 受信 HTTP |
db / db.query | データベース |
cache | キャッシュ操作 |
queue.task | バックグラウンドジョブ |
function | 関数呼び出し |
サンプリングの推奨値
| トラフィック | レート |
|---|---|
| 開発環境 | 1.0 |
| 低(<1K req/min) | 0.5 - 1.0 |
| 中(1K-10K) | 0.1 - 0.5 |
| 高(>10K) | 0.01 - 0.1 |
分散トレーシング
API にトレースヘッダーを送信するには、tracePropagationTargets を設定します。
tracePropagationTargets: ["localhost", "https://api.yourapp.com"],
Next.js 14 App Router の場合は、ルートレイアウトに追加します(Next.js 15 以降では不要です)。
export function generateMetadata(): Metadata {
return { other: { ...Sentry.getTraceData() } };
}
検証
トレーシングを有効にした後、トレース対象の操作(例:HTTP リクエスト)を実行し、Sentry の Performance ダッシュボードでトランザクションを確認します。カスタムスパンは、親トランザクションの下にネストして表示されます。
トラブルシューティング
| 問題 | 解決策 |
|---|---|
| トランザクションが表示されない | tracesSampleRate > 0 を確認し、DSN を検証します |
| ブラウザーのトレースが見つからない | browserTracingIntegration() を追加します |
| 分散トレースが接続されない | tracePropagationTargets と CORS ヘッダーを確認します |
| トランザクションが多すぎる | サンプルレートを下げるか、tracesSampler を使ってフィルターします |
コメント
Markdown 対応