Skip to content
Back to skills

Fastapi Observability

ASecurity

Observability for FastAPI backends and workers - /healthz and /readyz, Prometheus metrics (HTTP, dependency timings via guards, job/event/queue lag, security and audit gauges), event-loop lag monitor, unified JSON log envelope shipped with Alloy to Loki, guarded /metrics, dashboards, alerts and runbooks. Use whenever the user asks about monitoring, metrics, health endpoints, "how do I know it's working in production", slow requests, queue backlog, Grafana/Prometheus/Loki, log pipeline, alerts...

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 27, 2026
developmentpythongofastapidockerapibackendsecurity

Works with

  • cli
  • api

Security analysis

A100/100

Scanned November 27, 2026

npx -y skills add amralhorimy/fastapi-pro-skills --skill fastapi-observability --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Fastapi Observability?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Fastapi Observability
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/amralhorimy-fastapi-observability/badge)](https://www.skillsdirectory.com/skills/amralhorimy-fastapi-observability)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: fastapi-observability
description: Observability for FastAPI backends and workers - /healthz and /readyz, Prometheus metrics (HTTP, dependency timings via guards, job/event/queue lag, security and audit gauges), event-loop lag monitor, unified JSON log envelope shipped with Alloy to Loki, guarded /metrics, dashboards, alerts and runbooks. Use whenever the user asks about monitoring, metrics, health endpoints, "how do I know it's working in production", slow requests, queue backlog, Grafana/Prometheus/Loki, log pipeline, alerts or tracing across API and workers.
---

# Observability

Logs say what happened, metrics say how much/how fast, health says whether to send traffic. Design them in at foundation time.

## Health
- `GET /healthz` - process alive, no dependency calls (restart signal).
- `GET /readyz` - dependencies reachable *now* (Mongo ping, Redis ping, storage) concurrently with 2 s timeouts; 503 + per-dependency
  status otherwise. Compose/Nginx wait on it. Keep details internal; never print secrets or versions publicly.

## Metrics (`platform/metrics`, prometheus_client, one registry class)
| Group | Metrics |
|---|---|
| HTTP | `http_requests_total{method,route,status}`, `http_request_seconds`, in-flight gauge - label by **route template**, never raw path or ids |
| Errors | `errors_total{code}` fed by the central handler |
| Dependencies | `dependency_calls_total{dependency,outcome}` + `dependency_seconds` - recorded **inside the guard decorator** (`ok / unavailable / error`), so every adapter is measured with zero per-call code |
| Runtime | event-loop lag histogram (a monitor task sleeps a fixed interval and measures overshoot), WebSocket connections gauge |
| Jobs | enqueued, done{status}, retries, `job_seconds{type}` |
| Events | emitted, **dropped** (publish failed), consumed{group}, `event_seconds{group}` |
| Queues | `queue_length{stream}`, `queue_dead{stream}`, `queue_pending{group}`, `queue_lag{group}` - sampled periodically |
| Security/audit | security events + alerts counters, audit entries counter, `audit_chain_ok`, last-verified timestamp, last seq |
| Domain | small business counters (e.g. feed lookups by source) |

Rules: low-cardinality labels only; workers expose their own metrics port (or API samples queue gauges from Redis);
`/metrics` requires `METRICS_AUTH_TOKEN` in production and answers **404** to a wrong token.

## Logs
Unified envelope, channels (`access/audit/security/app`), redaction in the formatter, correlation id and actor on every line
(`fastapi-errors-logging`). Ship stdout with **Grafana Alloy -> Loki**; metrics via **Prometheus**; config under `deploy/monitoring/`
(`prometheus.yml`, `loki.yml`, `alloy/`), started with `docker-compose.monitoring.yml`. Include `verify_logs_pipeline.sh`
that emits a known line and confirms it arrives in Loki - a pipeline you never tested is not a pipeline.

## Alerts (symptoms, not causes)
Error ratio (5xx) and p95/p99 per key route; `readyz` failing; `queue_lag`/oldest-pending growing; `queue_dead` > 0;
`events_dropped` rising; event-loop lag p99 high; no worker heartbeat; `audit_chain_ok == 0` (page); security alert bursts.
Each alert links to a runbook entry: meaning -> first 3 checks -> commands -> fix -> escalation.

## Ops commands live in docs
Per module doc: `XINFO GROUPS`, pending/dead counts, stream length, safe `--scan` (never `KEYS`), recount/repair scripts
(`python -m <pkg>.entrypoints.recount`), how to reset a user's state. Operators should never need to read the code.

## Tracing
Start with correlation ids. Add OpenTelemetry when latency attribution across API -> stream -> worker is needed; carry
`traceparent` inside job/event payloads and link it to the log `request_id`.

## Checklist
- [ ] `/healthz` and `/readyz`; stopping Redis makes readiness fail and recover on restart
- [ ] Every adapter class guarded -> dependency metrics exist without extra code
- [ ] Queue length/pending/lag/dead sampled; alert rules written with runbook links
- [ ] Log pipeline verified end to end; redaction tested
- [ ] `/metrics` protected; labels low-cardinality

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.