Lab — Python REST API Monitoring Service¶
Lab Overview¶
Purpose: Check a list of HTTP endpoints for status code and latency; emit a summary suitable for alerting.
Scenario: Product APIs degrade regionally. You need a small monitor that can run in CI with fixtures and live against staging.
Expected outcome: CLI reads targets YAML; --fixture synthesises results; live mode uses timeouts and optional concurrency.
This is a lab, not a tutorial
Apply REST APIs and Concurrency — threads, asyncio, and futures.
Business Scenario¶
Status page data is manual. Engineering wants automated probes every five minutes with a JSON artefact for Grafana or Slack.
Learning Objectives¶
- Load targets from YAML
- Enforce connect/read timeouts
- Use ThreadPoolExecutor or asyncio for concurrent probes
- Fixture mode for offline CI
Prerequisites¶
Knowledge¶
Software¶
httpx. Estimated cost: £0.
Environment¶
mkdir -p ~/rebash-lab-python-monitor/{fixtures,out}
cd ~/rebash-lab-python-monitor
python3 -m venv .venv && source .venv/bin/activate
pip install 'httpx>=0.27,<1' 'PyYAML>=6.0,<7'
Initial State¶
cat > targets.yaml << 'EOF'
targets:
- name: httpbin-get
url: https://httpbin.org/status/200
expect: 200
- name: httpbin-404
url: https://httpbin.org/status/404
expect: 200
EOF
cat > fixtures/results.json << 'EOF'
[
{"name": "httpbin-get", "ok": true, "status": 200, "latency_ms": 120},
{"name": "httpbin-404", "ok": false, "status": 404, "latency_ms": 110}
]
EOF
Task¶
monitor_apis.py --targets targets.yaml --fixture fixtures/results.json writes out/report.json and exits 1 if any ok is false. Live mode without --fixture probes URLs concurrently with timeout 5s.
Validation¶
- Fixture mode offline
- Failed expect → non-zero exit
- Timeouts configured in live code path
Troubleshooting¶
| Symptom | Fix |
|---|---|
| Live probes hang | Missing timeout |
| SSL errors | Document corporate proxies; use fixtures |
Cleanup¶
Production Discussion¶
Graduate to blackbox exporters / Synthetics. Keep this service for custom business probes and learning concurrency patterns.
Related¶
- Next: Secrets Scanner