FastAPI
FastAPI アプリ向けのステータスページと稼働監視
FastAPI エンドポイントと非同期ワーカーを監視。JSON-path アサーションは FastAPI のヘルススキーマに自然に合致。ホステッドまたはヘッドレス。
FastAPI のステータスページを公開する 3 つの方法
必要な制御レベルを選択 — ノーコード、ローコード、フルヘッドレス。
ホステッド
FastAPI エンドポイントを追加し status.yourdomain.com に CNAME を向けるだけ。5 分で完成。
埋め込みバッジ
SVG の稼働率・ステータスバッジを README やランディングに埋め込み。5 分ごとに自動更新。
この health エンドポイントを FastAPI アプリに貼り付けます
コピペして PulseAPI を URL に向けるだけ。健康なら 200、劣化なら 503 を返します。
app/health.py
from fastapi import APIRouter, Response
from sqlalchemy import text
from .db import engine
from .redis import redis
router = APIRouter()
@router.get("/health")
async def health(response: Response) -> dict:
checks: dict[str, str] = {}
try:
async with engine.connect() as conn:
await conn.execute(text("SELECT 1"))
checks["database"] = "ok"
except Exception:
checks["database"] = "fail"
try:
pong = await redis.ping()
checks["cache"] = "ok" if pong else "fail"
except Exception:
checks["cache"] = "fail"
healthy = all(v == "ok" for v in checks.values())
response.status_code = 200 if healthy else 503
return {"status": "healthy" if healthy else "degraded", **checks}ルーターをメインアプリにマウント。部分障害時に 503 を返せば劣化判定。JSON-path は返却スキーマにそのまま動作。
FastAPI アプリで壊れるもの — それを検知する方法
非同期デッドロック
`await` 忘れや async 内の同期呼び出しがイベントループを止めます。レスポンスタイム急増で即検知。
Pydantic v1→v2 移行バグ
モデル検証変更が静かにエンドポイントを壊します。JSON body アサーションがスキーマ後退を捕捉。
上流の障害
依存サービスが落ちると 502。PulseAPI がユーザーより先に検知。
ヘッドレス視点
当社のステータスページが気に入らない? FastAPI で自作してください。
当社 API はホステッド UI と同じデータを返します。完全な OpenAPI 仕様書は api.pulseapi.tech/docs.
当社ステータスデータを独自の /status にプロキシする FastAPI エンドポイント。
import httpx
from fastapi import APIRouter
router = APIRouter()
@router.get("/status")
async def status():
async with httpx.AsyncClient(timeout=5) as client:
r = await client.get("https://api.pulseapi.tech/status/acme")
r.raise_for_status()
return r.json()FastAPI 監視 — FAQ
FastAPI、Starlette、Quart で動きますか?
はい。いずれも標準 HTTP で同じ方法で監視できます。
バックグラウンドワーカー (Dramatiq、Arq、Celery) を監視?
はい。タスクをラウンドトリップするヘルスエンドポイントを公開。失敗やタイムアウトで 503。
Pydantic v1 と v2 のレスポンス対応は?
PulseAPI は最終 JSON に対してアサート。アサート対象フィールドが残っていれば OK。
FastAPI 内でステータスページをレンダリング?
はい。API を取得して Jinja2 で描画、または JSON をフロントエンドに返却。構造は OpenAPI 仕様書に。