claude-skills/

Anthropic公式スキル・プラグインの日本語ディレクトリ

last sync 1h ago
スキルOfficialmonitoring

🔧otel-migration

プラグイン
honeycomb

説明

OpenTelemetry(包括的な監視データ収集システム)を既存の未計測アプリケーションに後付けする際のガイドです。 **次のような場合に使用:** - 既存アプリを OpenTelemetry に移行したい - 既存プロジェクトに OpenTelemetry を追加したい - OpenTelemetry を既存コードベースに後付けしたい - コード内でコンテキスト(処理の流れに付随する情報)を引き継ぎたい - コンテキスト伝播(情報の引き継ぎ)について知りたい - Prometheus メトリクス(性能指標)を OpenTelemetry に橋渡ししたい - ログを OpenTelemetry に移行したい - 既存のログシステム(slog、logback など)と OpenTelemetry を連携させたい - 計測が正しく動作しているか検証したい - トレース(処理の追跡データ)が途切れている - 孤立した span(処理の一単位)が発生している - OpenTelemetry への移行計画を立てたい - 移行を段階的に進める方法を知りたい - 既存コードにトレース機能を追加したい - コンテキスト伝播に対応するよう既存コードをリファクタリングしたい - Fiber フレームワークのコンテキスト処理の注意点を知りたい - 既存のログシステムを OpenTelemetry と共存させたい - 既存の Prometheus 監視を壊さずに OpenTelemetry を追加したい - その他、既存アプリケーションへの OpenTelemetry 後付けに関する質問 **注記:** このスキルは既存システムへの統合向けです。新規構築時の計測(otel-instrumentation スキル参照)や Beeline からの移行(beeline-migration スキル参照)には対応していません。

原文を表示

Guide for retrofitting OpenTelemetry into an existing, uninstrumented application. Trigger phrases: "migrate existing app to OTel", "add OpenTelemetry to existing project", "retrofit OTel into my codebase", "thread context through my code", "context propagation", "bridge Prometheus metrics to OTel", "logging bridge", "migrate logging to OTel", "slog bridge", "logback bridge", "verify my instrumentation", "traces are disconnected", "orphaned spans", "migrate to OpenTelemetry", "OTel migration plan", "how do I sequence an OTel migration", "add tracing to existing code", "refactor for context propagation", "Fiber context gotcha", "keep existing logging working with OTel", "add OTel without breaking Prometheus", "bridge existing metrics", "coexist with existing monitoring", or any request about retrofitting OpenTelemetry into an existing application. This skill is for migrating existing codebases, NOT greenfield instrumentation (use otel-instrumentation) or Beeline-specific migration (use beeline-migration).

ユースケース

  • 既存アプリを OpenTelemetry に移行したい
  • 既存プロジェクトに計測機能を追加したい
  • コンテキスト伝播について知りたい
  • トレースが途切れている場合の対応
  • 既存ログシステムと連携させたい

本文(日本語訳)

既存アプリケーション向けOpenTelemetry移行ガイド

計測機能のないアプリケーションにOpenTelemetry(テレメトリーを自動収集するための仕組み)を組み込むための手順書です。段階的な移行アプローチ、コンテキスト伝播のリファクタリング、ログとメトリクスのブリッジ(データ連携)、検証について説明しています。これは新規プロジェクト向けのOTel導入(otel-instrumentationスキルを参照)やBeehive特有の移行(beeline-migrationスキルを参照)とは異なります。

このスキルを使用する場合

以下のいずれかに該当する既存アプリケーションを扱う場合にこのスキルを使用します。

  • OpenTelemetryの計測がなく、新たに追加する必要がある
  • 既存のログ、メトリクス、またはコンテキスト処理パターンがあり、OTelと共存させる必要がある
  • 関数のシグネチャを変更して、呼び出し階層全体でトレース情報を渡す必要がある
  • OTelミドルウェアで注意が必要なフレームワーク(Fiber、Gin、Expressなど)を使用している

新規プロジェクト向けのOTel導入は otel-instrumentation スキルを、Beehiveから OTelへの移行は beeline-migration スキルを、計測の必要性についての理解は observability-fundamentals スキルを参照してください。

移行の6つのフェーズ

移行は6つのフェーズを順番に進めます。各フェーズは独立したスケジュールで本番導入でき、検証も可能です。コンテキスト伝播(フェーズ3)が通常、全体作業量の約60%を占めます。

フェーズ1: SDK初期化とシャットダウン

TracerProvider、MeterProvider、LoggerProviderをOTLPエクスポーター(データ送信機)とともに設定します。初期化はアプリケーションの起動時点で行い、シャットダウンはシグナルハンドラ(終了信号の処理)で実行します。

重要なポイント:

  • SDK初期化は、スパン(トレース単位)を作成する可能性があるアプリケーションコードより前に実行する必要があります。起動スクリプトの最初のステップで、設定読み込みやストレージ初期化より前に行いましょう
  • シャットダウンの順序が重要です。トレース → メトリクス → ログの順に処理してください。10~30秒のタイムアウトを設定しましょう
  • 初期化に失敗してもアプリケーションは動作し続けるべきです。エラーをログに記録し、テレメトリーなしで続行します
  • 言語別のSDK設定については、${CLAUDE_PLUGIN_ROOT}/skills/otel-instrumentation/references/sdk-setup-by-language.md を参照してください

フェーズ2: HTTPミドルウェア(自動計測)

OTelミドルウェアをHTTPフレームワークに追加します。これにより、ハンドラーのコード変更なしにすべての受け取りリクエストに対して自動的にスパンが作成されます。最も効率的なステップです。

重要な注意:

異なるフレームワークではOTelで拡張されたコンテキストの取得方法が異なります。これは隠れたトレース停止の最も一般的な原因です。${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/framework-middleware.md でフレームワーク固有の詳細を確認してください。

フレームワーク OTelコンテキストの取得方法 よくある間違い
Go net/http r.Context() なし(標準)
Go Fiber v2 c.UserContext() c.Context() を使用(OTelスパンなしのfasthtpコンテキストが返る)
Go Gin c.Request.Context() c を直接使用
Go Echo c.Request().Context() なし
Python Flask 自動(スレッドローカル変数) 計測ライブラリ使用時はなし
Python Django 自動(スレッドローカル変数) 計測ライブラリ使用時はなし
Node.js Express 自動(AsyncLocalStorage) 計測ライブラリ使用時はなし
Java Spring 自動(スレッドローカル変数) スレッドプール間でのコンテキスト喪失
.NET ASP.NET Core 自動(AsyncLocal) なし
Ruby Rails 自動(スレッドローカル変数) 計測ライブラリ使用時はなし

フェーズ3: コンテキスト伝播のリファクタリング

HTTPハンドラー(または開始ポイント)から入出力操作まで、呼び出し階層全体にトレース情報を渡します。このフェーズが最も難しく、通常、移行作業全体の約60%を占めます。

このフェーズの難易度は言語によって大きく異なります。

  • Go: 最も難しい。呼び出し階層のすべての関数に context.Context パラメーターを追加する必要があります
  • Java: 中程度。スレッドローカルコンテキストはスレッド内では自動的に伝播しますが、スレッドプール、CompletableFuture、リアクティブストリーム間では破損します
  • Python: 比較的簡単。contextvars はスレッド内で自動的に伝播します。スレッドプールとマルチプロセッシングが痛い部分です
  • Node.js: 比較的簡単。AsyncLocalStorage はasync/awaitを通じて自動的に伝播します。古いコールバック方式のコードが痛い部分です
  • .NET: 最も簡単。Activity はAsync/awaitを通じて AsyncLocal<T> で自動的に伝播します
  • Ruby: 比較的簡単。スレッドローカルコンテキストは自動的に伝播します。スレッドを手動作成する際に問題が生じます

言語別のパターンとコード例については、${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/context-propagation-patterns.md を参照してください。

フェーズ4: カスタムスパン

自動計測では対応しないビジネスロジック操作にスパンを追加します。スパン作成の仕組みについては otel-instrumentation スキルに従います。移行固有のガイダンス:

  1. 入出力の境界から開始 — データベース呼び出し、外部HTTP呼び出し、キャッシュ操作
  2. その後ビジネスロジックを追加 — 時間がかかる理由を説明する操作
  3. 属性(詳細情報)を大量に記録 — 調査時の分析を役立てるため、できるだけ詳しく記録しましょう
  4. スパンに結果を記録 — 成功失敗のステータス、エラー数、実行時間を属性として記録

属性の名付けルールとスパン作成パターンについては、${CLAUDE_PLUGIN_ROOT}/skills/otel-instrumentation/references/custom-instrumentation.md を参照してください。

フェーズ5: ログの移行

既存のログライブラリをOTelに置き換えるか、ブリッジさせてログとトレースを関連付けます。

重要なポイント:

  • 通常、ログは標準出力(ローカルデバッグ用)とOTel(トレース相関用)の両方に送信するべきです。これにはマルチハンドラー(複数の出力先)パターンが必要です
  • OTelログブリッジは構造化ログ(キーと値のペア)で機能します。既存ログがprintf形式の文字列フォーマットを使用している場合は、先に構造化形式に変換してください
  • printf形式から構造化ログへの変換は退屈ですが機械的な作業です。自動リファクタリングの候補として適しています

言語別のログブリッジとマルチハンドラーパターンについては、${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/bridge-libraries.md を参照してください。

フェーズ6: メトリクスブリッジ

既存のPrometheus(監視メトリクス)またはその他のメトリクスライブラリがある場合、ブリッジでOTelに接続してください。書き直す必要はありません。

重要なポイント:

  • Prometheusブリッジは既存のレジストリーから読み取り、OTelメトリクスを生成します。既存の prometheus.NewCounterVec(...) 呼び出しは変更不要です
  • 既存のスクレイパー(データ収集ツール)がある場合、Prometheusの /metrics エンドポイントは維持してください。ブリッジはスクレイピングに加えてOTLPエクスポートを追加します
  • 最終的にPrometheus依存を削除したい場合は、別途の移行計画を立ててください。ブリッジは時間を稼ぐのに役立ちます

言語別のメトリクスブリッジについては、${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/bridge-libraries.md を参照してください。

検証

各フェーズの完了後、計測が正しく、完全であることを確認します。${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/verification-checklist.md で全チェックリストとクエリパターンを確認してください。

Honeycombアカウントなしでローカルで検証するには、付属のコレクタースクリプトを使用してスパンをデバッグ出力とNDJSON(改行区切りのJSON)として取得できます。使用方法は ${CLAUDE_PLUGIN_ROOT}/skills/otel-instrumentation/references/local-collector-debug-test.md を、完全なスクリプトは ${CLAUDE_PLUGIN_ROOT}/scripts/start-collector.sh を参照してください。

Honeycomb固有の検証クエリについては、query-patterns スキルも参照してください。

よくある落とし穴

一般的な間違いとその回避方法については、${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/migration-pitfalls.md を参照してください。

実例から学ぶ目安

参考として、実際のGatusプロジェクト(約30,000行のGoコード、Fiber v2、SQLite/Postgres、Prometheus)の移行:

  • 変更ファイル数: 約45個、変更行数: +840行/-645行
  • 作業内訳: コンテキスト伝播約60%、カスタムスパン約15%、ログ移行約15%、その他約10%
  • 遭遇したバグ: Fiberの c.Context()c.UserContext() の混淆、printf形式のslogフォーマット文字列、スパンの End() 呼び出し忘れ、ゴルーチン(Go言語の軽量スレッド)のコンテキスト再利用
原文(English)を表示

OpenTelemetry Migration for Existing Applications

Guide for retrofitting OpenTelemetry into an existing, uninstrumented application. This covers the phased migration approach, context propagation refactoring, logging and metrics bridges, and verification. This is distinct from greenfield OTel setup (see otel-instrumentation skill) and Beeline-specific migration (see beeline-migration skill).

When to Use This Skill

Use this skill when the user has an existing application that:

  • Has no OpenTelemetry instrumentation and needs to add it
  • Has existing logging, metrics, or context patterns that must coexist with OTel
  • Needs to refactor function signatures to thread trace context through the call stack
  • Uses a framework with OTel middleware gotchas (e.g., Fiber, Gin, Express)

For greenfield OTel setup, use the otel-instrumentation skill instead. For Beeline-to-OTel migration, use the beeline-migration skill instead. For understanding why to instrument, see the observability-fundamentals skill.

Migration Phases

The migration follows six phases in order. Each phase is independently deployable and verifiable. Context propagation (Phase 3) is typically ~60% of the effort.

Phase 1: SDK Initialization and Shutdown

Set up TracerProvider, MeterProvider, and LoggerProvider with OTLP exporters. Wire initialization early in the application's entry point and shutdown in signal handlers.

Key guidance:

  • SDK init must happen before any application code that might create spans — one of the first things in your entry point, before config loading or storage initialization
  • Shutdown ordering matters: flush traces, then metrics, then logs. Use a timeout (10-30s)
  • If init fails, the application should still work — log the error and continue without telemetry
  • For language-specific SDK setup, consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-instrumentation/references/sdk-setup-by-language.md

Phase 2: HTTP Middleware (Auto-Instrumentation)

Add OTel middleware to your HTTP framework. This gives you automatic spans for every inbound request with zero code changes to handlers. This is the highest-ROI step.

Critical: Different frameworks expose the OTel-enriched context differently. This is the #1 source of silent trace breaks. Consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/framework-middleware.md for framework-specific details.

Framework How to get OTel context Common mistake
Go net/http r.Context() N/A (standard)
Go Fiber v2 c.UserContext() Using c.Context() (returns fasthttp context without OTel span)
Go Gin c.Request.Context() Using c directly
Go Echo c.Request().Context() N/A
Python Flask Automatic (thread-local) N/A with instrumentation library
Python Django Automatic (thread-local) N/A with instrumentation library
Node.js Express Automatic (AsyncLocalStorage) N/A with instrumentation library
Java Spring Automatic (thread-local) Thread pool context loss
.NET ASP.NET Core Automatic (AsyncLocal) N/A
Ruby Rails Automatic (thread-local) N/A with instrumentation library

Phase 3: Context Propagation Refactoring

Thread trace context through your call chain from HTTP handlers (or entry points) down to I/O operations. This is the hardest phase — typically ~60% of migration effort.

The difficulty of this phase varies dramatically by language:

  • Go: Hardest. Requires adding context.Context parameter to every function in the call chain.
  • Java: Moderate. Thread-local context propagates automatically within a thread, but breaks across thread pools, CompletableFuture, and reactive streams.
  • Python: Easier. contextvars propagates automatically within a thread. Pain points are thread pools and multiprocessing.
  • Node.js: Easier. AsyncLocalStorage propagates through async/await automatically. Pain points are old callback-based code.
  • .NET: Easiest. Activity propagates through async/await via AsyncLocal<T> automatically.
  • Ruby: Easier. Thread-local context propagates automatically. Pain with manual thread creation.

For language-specific patterns and code examples, consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/context-propagation-patterns.md.

Phase 4: Custom Spans

Add spans to business logic operations that auto-instrumentation doesn't cover. Defer to the otel-instrumentation skill for span creation mechanics. Migration-specific guidance:

  1. Start with I/O boundaries — database calls, external HTTP calls, cache operations
  2. Then add business logic — operations that explain why time is spent
  3. Add attributes liberally — every piece of context makes BubbleUp useful during investigations
  4. Record outcomes on spans — result status, error count, duration as attributes

For attribute naming and span creation patterns, consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-instrumentation/references/custom-instrumentation.md.

Phase 5: Logging Migration

Replace or bridge your existing logging library into OTel so logs correlate with traces.

Key guidance:

  • You almost certainly want logs going to both stderr (local debugging) AND OTel (trace correlation). This requires a multi-handler/fan-out pattern.
  • OTel log bridges work with structured logging (key-value pairs). If your existing logging uses printf-style format strings, convert to structured format first.
  • Converting from printf-style to structured logging is tedious but mechanical — a good candidate for automated refactoring.

For language-specific logging bridges and the multi-handler pattern, consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/bridge-libraries.md.

Phase 6: Metrics Bridge

If you already have Prometheus metrics (or another metrics library), bridge them to OTel rather than rewriting.

Key guidance:

  • Prometheus bridge reads from the existing registry and produces OTel metrics — existing prometheus.NewCounterVec(...) calls continue unchanged
  • Keep the Prometheus /metrics endpoint if you have existing scrapers. The bridge adds OTLP export in addition to scraping.
  • If you want to eventually remove the Prometheus dependency, plan a separate migration later. The bridge buys you time.

For language-specific metrics bridges, consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/bridge-libraries.md.

Verification

After each phase, verify that instrumentation is correct and complete. Consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/verification-checklist.md for the full checklist and query patterns.

To verify locally without a Honeycomb account, use the bundled collector script to capture spans as debug output and NDJSON. Consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-instrumentation/references/local-collector-debug-test.md for usage, and ${CLAUDE_PLUGIN_ROOT}/scripts/start-collector.sh for the full script.

For Honeycomb-specific verification queries, also consult the query-patterns skill.

Common Pitfalls

For a catalog of common mistakes and how to avoid them, consult ${CLAUDE_PLUGIN_ROOT}/skills/otel-migration/references/migration-pitfalls.md.

Real-World Calibration

For reference, a real migration of Gatus (~30k LOC Go, Fiber v2, SQLite/Postgres, Prometheus):

  • Files changed: ~45, Lines: +840/-645
  • Effort breakdown: Context propagation ~60%, Custom spans ~15%, Logging migration ~15%, Everything else ~10%
  • Bugs encountered: Fiber c.Context() vs c.UserContext(), printf-style slog format strings, missing span.End() calls, goroutine context reuse

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。