A lightweight monitoring middleware for FastAPI applications with real-time dashboard
Project description
FastAPI Monitor
A lightweight monitoring middleware for FastAPI applications with real-time dashboard and comprehensive metrics collection.
✨ Features
- 🚀 Zero-configuration setup - Add monitoring in 3 lines of code
- 📊 Real-time dashboard with auto-refresh capabilities
- 💾 SQLite storage - No external dependencies required
- 📈 Comprehensive metrics - Request/response times, status codes, error tracking
- 🔒 Security-first - SQL injection proof with parameterized queries
- 📱 Responsive UI - Works seamlessly on desktop and mobile
- ⚡ High performance - Minimal overhead on your API
🚀 Quick Start
Installation
pip install fastapi-monitor
Basic Usage
from fastapi import FastAPI
from fastapi_monitor import MonitorMiddleware, create_dashboard_app
app = FastAPI()
# Add monitoring middleware
app.add_middleware(MonitorMiddleware, db_path="monitor.db")
# Your API routes
@app.get("/")
async def root():
return {"message": "Hello World"}
# Mount the monitoring dashboard
dashboard_app = create_dashboard_app("monitor.db")
app.mount("/monitor", dashboard_app)
That's it! Visit http://localhost:8000/monitor to see your API metrics.
📊 Dashboard Features
- Real-time statistics - Total requests, average response time, error rates
- Request timeline - Visual representation of API traffic over time
- Status code distribution - HTTP status code breakdown with color coding
- Error tracking - Detailed error logs with stack traces
- Request details - Full request/response inspection
- Performance metrics - Response time analysis and trends
⚙️ Configuration
Advanced Middleware Setup
app.add_middleware(
MonitorMiddleware,
db_path="custom_monitor.db",
exclude_paths=["/health", "/metrics", "/docs"]
)
Dashboard with Basic Authentication
# Protected dashboard
dashboard_app = create_dashboard_app(
db_path="monitor.db",
username="admin",
password="your-secure-password"
)
app.mount("/monitor", dashboard_app)
Custom Dashboard Mount
# Without authentication
dashboard_app = create_dashboard_app("custom_monitor.db")
app.mount("/admin/monitoring", dashboard_app)
📈 Metrics Collected
| Metric | Description |
|---|---|
| Request Time | Timestamp of each request |
| Response Time | Processing time in milliseconds |
| HTTP Method | GET, POST, PUT, DELETE, etc. |
| Request Path | API endpoint accessed |
| Status Code | HTTP response status |
| Request Size | Size of request body in bytes |
| Response Size | Size of response body in bytes |
| Client IP | Source IP address |
| User Agent | Client browser/application info |
| Error Details | Stack traces for failed requests |
🛠️ Development
Setup Development Environment
git clone https://github.com/yourusername/fastapi-monitor.git
cd fastapi-monitor
pip install -e ".[dev]"
Running Tests
pytest
Code Formatting
black .
isort .
📝 Example Application
Check out the complete example in the examples/ directory:
cd examples
python example_app.py
Then visit:
- API: http://localhost:8000
- Dashboard: http://localhost:8000/monitor
🤝 Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
⭐ If this project helped you, please consider giving it a star!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_monitor-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_monitor-0.1.0.tar.gz
- Upload date:
- Size: 38.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b794165a78b4c22813d5643ddc9b0bef5c9732550ec64bcdf981d5f2d3927a46
|
|
| MD5 |
890550fd73b16f905bdf1b0182c13b4e
|
|
| BLAKE2b-256 |
b5a5bee459cb831d92ba8123ef763935d93b96bd86918bbc1d8543ef17a4b04e
|
File details
Details for the file fastapi_monitor-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_monitor-0.1.0-py3-none-any.whl
- Upload date:
- Size: 39.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
749606e374e0bb6c3b3dba8c0a9f74f1d80945089e52b3c04fb90086ba0b500d
|
|
| MD5 |
a245e6e57608298b8a22ca6075db1142
|
|
| BLAKE2b-256 |
d766d571955e7fde1b0f00fda2830debb7561f7b2e631460b8c9652912ec3dfc
|