Skip to content

Website Uptime Checker

In plain English

Regularly checks websites and APIs in the background, verifies they respond properly, and tracks response speed trends cleanly.

An asynchronous FastAPI service and SLA probing engine verifying HTTP status codes, response latency ceilings, and JSON payload contracts in SQLite WAL.

1. Why I built it

Built to explore proper FastAPI lifespan client pooling, SQLite concurrency locks, and dependency-injected offline HTTP testing.

Problem being solvedPer-request `AsyncClient` instantiation leaks sockets, concurrent handlers race unguarded SQLite connections, and live-network tests fail non-deterministically.

2. How it works

Implemented `TargetConfig`, `ProbeOutcome`, `HealthStore`, `_percentile()`, and `execute_probe()` in `src/api_monitor/engine.py` paired with `src/api_monitor/api.py`.

Simple HTTP 200 pings miss endpoints breaching latency budgets or returning incomplete JSON payloads. HTTP SLA Monitor checks three explicit contract conditions per probe: expected status code, `max_latency_ms` ceiling, and optional required JSON payload keys (`required_json_key`).

FastAPI `lifespan` manages a shared `httpx.AsyncClient` connection pool and deterministic DB teardown. `HealthStore` uses SQLite WAL mode with `busy_timeout` and `threading.RLock()`, performs `O(1)` indexed target lookups (`get_target`), enforces positive limit checks, and resets stale `last_status` codes when network exceptions strike on retries.

Offline tests pass injected `httpx.MockTransport` handlers to exercise `503 -> 200` retry recovery, latency violations, and missing-key failures deterministically.

3. Output & Source Specimen

TargetConfig & SLA Rollup Shape

01GET /targets/{name}/summary ->
02 target_name: str, total_checks: int, availability_pct: float
03 p50_latency_ms: float, p95_latency_ms: float, p99_latency_ms: float
04 last_checked_at: ISO-8601 | None, last_healthy: bool | None
05 last_failure_reason: str | None
Exact dictionary fields returned by HealthStore.compute_target_summary()Run checks: pytest -v tests/test_monitor.py

4. Design Decisions & Challenges

Key Design Decisions

  • 01.Moved `httpx.AsyncClient` and database initialisation into FastAPI `lifespan` for clean connection pooling and teardown.
  • 02.Guarded `HealthStore` queries with `threading.RLock()` and WAL journalling, adding direct `get_target()` lookups.
  • 03.Injected optional `httpx.AsyncClient` instances so `pytest-asyncio` tests run against `MockTransport` offline.

Edge Cases Handled

  • Clearing `last_status = None` across network timeout/connection exceptions so final failure reasons never retain an old HTTP status code.

5. Testing & Limits

Automated Checks

Verified with 8 pytest / pytest-asyncio checks in `tests/test_monitor.py` testing healthy checks, timeout/503 retries, non-JSON HTTP status preservation, HEAD checks, concurrent batching, `GET /targets/{name}`, `GET /targets/summary`, and deletion.

Current Limits & Next Steps

  • Checks currently trigger via `POST /targets/{name}/probe`; running a background `asyncio.Task` inside `lifespan` would enable scheduled autonomous polling.

6. Key Takeaways

What building Website Uptime Checker reinforced

Lifespan client sharing paired with explicit RLock boundaries prevents SQLite locking errors under async load while MockTransport makes retry loops trivial to test.

  • — All 8 async pytest tests and Ruff checks pass cleanly.
  • — Source hosted at github.com/16lv/http-sla-monitor.

Recent Activity

Latest repository updates

All changes for 16lv/http-sla-monitor

Website Uptime & SLA Monitor (v1.4.0)

Published FastAPI monitoring service and CLI runner verifying HTTP status codes, JSON response keys, and p50/p95/p99 latencies into SQLite WAL.

2026-11-18