DEV Community

Vigilmon
Vigilmon

Posted on

Monitoring Your FastAPI Application with Vigilmon: Health Checks & Uptime Alerts

Monitoring Your FastAPI Application with Vigilmon: Health Checks & Uptime Alerts

FastAPI is one of the fastest-growing Python web frameworks, popular for building APIs and microservices. When your FastAPI service goes down, external monitoring catches it before your users do. This guide walks through adding health checks and setting up Vigilmon.

Adding a Health Check to FastAPI

Basic Health Endpoint

from fastapi import FastAPI
from datetime import datetime

app = FastAPI()

@app.get("/health")
async def health_check():
    return {"status": "ok", "timestamp": datetime.utcnow().isoformat()}
Enter fullscreen mode Exit fullscreen mode

Deploy this and you have a URL Vigilmon can monitor.

Health Check with Database Connectivity

from fastapi import FastAPI, HTTPException
from sqlalchemy import text
from .database import engine

app = FastAPI()

@app.get("/health")
async def health_check():
    try:
        async with engine.connect() as conn:
            await conn.execute(text("SELECT 1"))
        return {"status": "ok", "db": "connected"}
    except Exception as e:
        raise HTTPException(status_code=503, detail={"status": "error", "db": str(e)})
Enter fullscreen mode Exit fullscreen mode

Comprehensive Health Check with Multiple Dependencies

from fastapi import FastAPI
from fastapi.responses import JSONResponse
import asyncio
import redis.asyncio as redis

app = FastAPI()

@app.get("/health")
async def health_check():
    health = {"status": "ok", "services": {}}
    http_status = 200

    # Check database
    try:
        async with engine.connect() as conn:
            await conn.execute(text("SELECT 1"))
        health["services"]["database"] = "ok"
    except Exception:
        health["services"]["database"] = "error"
        health["status"] = "degraded"
        http_status = 503

    # Check Redis
    try:
        r = redis.from_url("redis://localhost")
        await r.ping()
        await r.aclose()
        health["services"]["redis"] = "ok"
    except Exception:
        health["services"]["redis"] = "error"
        health["status"] = "degraded"

    return JSONResponse(content=health, status_code=http_status)
Enter fullscreen mode Exit fullscreen mode

Setting Up Vigilmon for FastAPI

  1. Sign up at vigilmon.online
  2. Click Add Monitor
  3. URL: https://your-fastapi-service.com/health
  4. Check interval: 1 minute for production
  5. Alert channels: email and/or webhook
  6. Save

Vigilmon checks from multiple geographic regions simultaneously. It only alerts when 2+ regions agree the endpoint is down — eliminating false alerts from transient network issues.

Docker Compose Health Check Integration

Pair your Vigilmon monitoring with Docker health checks for full coverage:

# docker-compose.yml
services:
  api:
    build: .
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    ports:
      - "8000:8000"
Enter fullscreen mode Exit fullscreen mode

Docker health checks catch internal container failures. Vigilmon catches external reachability failures. Both are necessary.

Kubernetes Probe Configuration

spec:
  containers:
  - name: fastapi-app
    image: your-fastapi:latest
    livenessProbe:
      httpGet:
        path: /health
        port: 8000
      initialDelaySeconds: 30
      periodSeconds: 10
    readinessProbe:
      httpGet:
        path: /health
        port: 8000
      initialDelaySeconds: 5
      periodSeconds: 5
Enter fullscreen mode Exit fullscreen mode

Then add the public endpoint to Vigilmon for external monitoring.

What to Monitor

For a FastAPI microservice architecture:

Service Monitor URL Alert Threshold
Main API /health 2 failures
Auth service /auth/health 1 failure (critical)
Worker service /worker/alive 2 failures
Docs (optional) /docs 5 failures

Webhook Alerts

Vigilmon supports webhook alerts. For Slack notifications:

  1. Create a Slack incoming webhook
  2. In Vigilmon, add Alert Channel → Webhook
  3. Paste the Slack webhook URL

Vigilmon sends a POST when your FastAPI service goes down or recovers.

Conclusion

Adding a /health endpoint to FastAPI takes 5 lines of code. Monitoring it with Vigilmon takes 2 minutes. Together they give you multi-region uptime monitoring with false-alert prevention — ensuring you know before your users do.

Start free at vigilmon.online — 5 monitors, no credit card, live in 2 minutes.

Top comments (0)