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.1.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.1-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_monitor-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_monitor-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 20d9e973c2490d06ced7de555a09a9e5bdcab20e88a88708dca56ae0e2e11375
MD5 e311ab27419f9c4e541119b06f0373fb
BLAKE2b-256 f81070606d1ea2c55005162e1cb87958ed969abb1d03f3c4a7cd7402f21b2a39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_monitor-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6adfd7d20c8bf5a6c4fdbf1cc5a5dad57a74915e0d2a5475937d94ed9d21344f
MD5 99b92f85cf4c269b2264b464ab4eb033
BLAKE2b-256 e0892b2f0f4e5ed5a23be81a87de1f0ae2a479d306b480e4c48491ab4eb48b23

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