Zero-config metrics and observability for FastAPI apps targeting indie devs, startups, and no-code platforms. No Prometheus/Grafana infrastructure required.
- 5-minute setup: One import, one line of code
- No infrastructure: SQLite/memory storage, no containers
- Business metrics: Track revenue, users, features - not just HTTP metrics
- Query API: JSON endpoints for custom dashboards
- Cost tracking: Auto-detect OpenAI/Anthropic API costs
- No-code ready: Retool/Bubble can consume the API directly
- Indie developers building MVPs
- Early-stage startups (pre-Series A)
- No-code platform backends (Bubble, Retool, Make.com)
- Developers prototyping without DevOps resources
fastapi-metrics/
├── fastapi_metrics/
│ ├── __init__.py
│ ├── core.py # Main Metrics class
│ ├── middleware.py # Request tracking middleware
│ ├── storage/
│ │ ├── base.py # Storage interface
│ │ ├── memory.py # In-memory storage
│ │ ├── sqlite.py # SQLite storage
│ │ └── redis.py # Redis storage (optional)
│ ├── collectors/
│ │ ├── http.py # HTTP request metrics
│ │ ├── system.py # CPU, memory, disk
│ │ ├── business.py # Custom business metrics
│ │ └── cost.py # LLM API cost tracking
│ ├── health/
│ │ ├── checks.py # Health check implementations
│ │ └── endpoints.py # Health endpoints
│ ├── query.py # Metrics query engine
│ └── exporters/
│ ├── json.py # JSON export
│ ├── prometheus.py # Prometheus format
│ └── csv.py # CSV export
├── tests/
├── examples/
├── docs/
└── pyproject.toml
- ✅ Main
Metricsclass initialization - ✅ FastAPI app integration
- ✅ Request tracking middleware
- ✅ Response time calculation
- ✅ Status code tracking
- ✅ Abstract storage interface
- ✅ In-memory storage (dict-based)
- ✅ SQLite storage with tables:
http_requests(timestamp, endpoint, method, status, latency_ms)custom_metrics(timestamp, metric_name, value, labels)system_metrics(timestamp, cpu, memory, disk)
- ✅ Requests per endpoint
- ✅ Requests per status code
- ✅ Requests per method
- ✅ Latency percentiles (p50, p95, p99)
- ✅ Error rate calculation
- ✅ Active requests counter
- ✅
GET /metrics- Current snapshot (JSON) - ✅
GET /metrics/query- Time-series queries- Query params:
from,to,metric,group_by,endpoint
- Query params:
- ✅
GET /metrics/endpoints- Per-endpoint stats - ✅
GET /metrics/export?format=csv|prometheus
- ✅ Health check base class
- ✅ Built-in checks:
- ✅ Database connectivity
- ✅ Redis connectivity
- ✅ Disk space
- ✅ Memory usage
- ✅ Custom check support
- ✅ Health endpoints:
- ✅
GET /health- Simple status - ✅
GET /health/live- Liveness probe - ✅
GET /health/ready- Readiness probe with checks
- ✅
- ✅ CPU usage tracking
- ✅ Memory usage tracking
- ✅ Disk usage tracking
- ✅ Uptime tracking
- ✅ System metrics endpoint
- ✅Make Redis storage first-class (not optional)
- ✅ Add proper K8s health checks (Phase 2 should be Phase 1)
- ✅ Add deployment examples for multi-instance setups
- ✅ Position as "lightweight alternative to Prometheus that scales"
- ✅
metrics.track(name, value, **labels)API - ✅ Counter metrics
- ✅ Gauge metrics
- ✅ Histogram metrics
- ✅ Label/tag support for segmentation
- ✅ Auto-detect OpenAI API calls
- ✅ Auto-detect Anthropic API calls
- ✅ Token usage tracking
- ✅ Cost calculation (using current pricing)
- ✅ Cost by model/endpoint
- ✅
GET /metrics/costsendpoint
- Configurable retention period
- Automatic data cleanup job
- Aggregation for old data (hourly → daily)
- Threshold-based alerts
- Webhook notifications
- Email notifications (optional)
- Prometheus format
- CSV export
- JSON export with timestamps
- README with quick start
- Full API documentation
- Configuration guide
- Storage backend comparison
- Kubernetes deployment guide
- Basic FastAPI app
- With database health checks
- Custom business metrics
- LLM cost tracking
- Retool integration example
- No-code tool integration guide
- Unit tests (80%+ coverage)
- Integration tests
- Performance benchmarks
from fastapi import FastAPI
from fastapi_metrics import Metrics
app = FastAPI()
# Minimal setup
metrics = Metrics(
app,
storage="sqlite://metrics.db", # or "memory://"
retention_hours=24,
)
# Track custom metrics anywhere
@app.post("/payment")
def payment(amount: float, user_id: int):
metrics.track("revenue", amount, user_id=user_id)
metrics.track("payment_count", 1)
return {"status": "ok"}GET /metrics
GET /metrics/query?metric=revenue&from=24h&group_by=hour
GET /metrics/endpoints
GET /metrics/costs
GET /health/ready
- Default: SQLite (single file, no setup)
- Optional: Redis (for distributed systems)
- Fallback: In-memory (testing/development)
- Store raw events initially
- Aggregate on query (Phase 1)
- Pre-aggregate for performance (Phase 4)
- Async middleware (non-blocking)
- Background workers for aggregation
- Configurable batch writes
- Query result caching
- < 5 lines of code to set up
- < 1ms overhead per request
- Works with SQLite (no external deps)
- Query API returns in < 100ms
- Retool/Bubble integration works out-of-box
- ❌ Replace Prometheus/Grafana for large scale
- ❌ Distributed tracing
- ❌ Log aggregation
- ❌ APM-level profiling
- ❌ Built-in UI (JSON API only)
[dependencies]
fastapi = ">=0.100.0"
pydantic = ">=2.0.0"
aiosqlite = ">=0.19.0" # Async SQLite
psutil = ">=5.9.0" # System metrics
httpx = ">=0.24.0" # For health checks (optional)- Phase 1: Core functionality
- Phase 2: Health checks
- Phase 3: Business metrics
- Phase 4: Advanced features
- Phase 5: Documentation
- Phase 6: Additional Features
pip install fastapi-metricsgit clone https://github.com/yourusername/fastapi-metrics
cd fastapi-metrics
poetry install
poetry run pytest- 2025-12-10: Initial spec created
- Target: Indie devs who want metrics without infrastructure
- Focus: Simplicity over enterprise features
- SQLite as default storage for zero-config experience

