Skip to main content

A lightweight monitoring middleware for FastAPI applications with real-time dashboard

Project description

FastAPI Monitor

Python 3.8+ License: MIT Code style: black

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.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_monitor-0.1.0.tar.gz (38.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_monitor-0.1.0-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

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

Hashes for fastapi_monitor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b794165a78b4c22813d5643ddc9b0bef5c9732550ec64bcdf981d5f2d3927a46
MD5 890550fd73b16f905bdf1b0182c13b4e
BLAKE2b-256 b5a5bee459cb831d92ba8123ef763935d93b96bd86918bbc1d8543ef17a4b04e

See more details on using hashes here.

File details

Details for the file fastapi_monitor-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_monitor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 749606e374e0bb6c3b3dba8c0a9f74f1d80945089e52b3c04fb90086ba0b500d
MD5 a245e6e57608298b8a22ca6075db1142
BLAKE2b-256 d766d571955e7fde1b0f00fda2830debb7561f7b2e631460b8c9652912ec3dfc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page