Lightweight FastAPI middleware for centralized API monitoring
Project description
tigzig-api-monitor
Lightweight FastAPI middleware for centralized API monitoring. Automatically captures request/response metrics and sends them to a central monitoring service.
Installation
pip install tigzig-api-monitor
Quick Start
from fastapi import FastAPI
from tigzig_api_monitor import APIMonitorMiddleware
app = FastAPI()
# Add the monitoring middleware
app.add_middleware(
APIMonitorMiddleware,
app_name="YOUR_APP_NAME", # Required: identifies your app in logs
)
Configuration
Set these environment variables:
| Variable | Required | Description |
|---|---|---|
API_MONITOR_URL |
Yes | URL of the monitoring service (e.g., https://logger.tigzig.com/log) |
API_MONITOR_KEY |
Yes | API key for authentication |
Features
- Non-blocking: Logs are sent asynchronously, never slowing down your API
- Automatic capture: Request method, endpoint, status code, response time
- Privacy-safe: IP addresses are hashed, not stored
- Lightweight: Minimal dependencies (just
httpxandstarlette) - Fire-and-forget: Logging failures don't affect your API
- Noise filtering: Automatically skips health checks, bots, and vulnerability scanners
What Gets Logged
Each request automatically captures:
- App name (configured)
- Endpoint path (no query params for privacy)
- HTTP method (GET, POST, etc.)
- Response status code
- Response time in milliseconds
- Client IP (sent to service, hashed before storage)
- User-Agent header
- Origin header
- Referer path (no query params)
Example
import os
from fastapi import FastAPI
from tigzig_api_monitor import APIMonitorMiddleware
# Set environment variables (or use .env file)
os.environ["API_MONITOR_URL"] = "https://logger.tigzig.com/log"
os.environ["API_MONITOR_KEY"] = "your-api-key"
app = FastAPI()
# Add monitoring - that's it!
app.add_middleware(APIMonitorMiddleware, app_name="MY_BACKEND")
@app.get("/")
def read_root():
return {"message": "Hello World"}
# Every request to this API will now be monitored automatically
Whitelist Mode (v1.3.0+) - RECOMMENDED
The best way to filter noise is to only log YOUR endpoints. Use include_prefixes to specify which paths to log:
# Only log paths starting with these prefixes
app.add_middleware(
APIMonitorMiddleware,
app_name="MY_BACKEND",
include_prefixes=("/excel/", "/stock/", "/api/", "/mcp/"),
)
This is much cleaner than trying to exclude infinite junk paths. You know your endpoints - just whitelist them.
Blacklist Mode (v1.1.0+)
If you prefer blacklist mode, the middleware automatically skips logging for:
- Health check endpoints (
/,/health,/healthz,/ready) - Static files (
/favicon.ico,/robots.txt,/sitemap.xml) - Vulnerability scanner probes (
/.env,/wp-admin,/vendor/phpunit/..., etc.) - Common bot paths (
/_next/,/js/,/css/, etc.)
Customize Blacklist
# Add additional paths to exclude
app.add_middleware(
APIMonitorMiddleware,
app_name="MY_BACKEND",
exclude_paths={"/internal", "/metrics", "/debug"},
exclude_prefixes=("/admin/", "/private/"),
)
# Log everything (disable all filtering)
app.add_middleware(
APIMonitorMiddleware,
app_name="MY_BACKEND",
include_noise=True,
)
License
MIT
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 tigzig_api_monitor-1.3.0.tar.gz.
File metadata
- Download URL: tigzig_api_monitor-1.3.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4d60f409fce750ee7ba82063298ac8a518c01a8566eca6f848f71ab969d5e2f
|
|
| MD5 |
219949b18d810cf44c8a033556f207e8
|
|
| BLAKE2b-256 |
e3da689c6fe355eb751b2751cc542e9432c5e43b9cbbdd76a87903fe81dc36de
|
File details
Details for the file tigzig_api_monitor-1.3.0-py3-none-any.whl.
File metadata
- Download URL: tigzig_api_monitor-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60ad0cd9da80096cc446652099ee1db0f8e4fb8c3b3fbe6a68b626b8427d601f
|
|
| MD5 |
c7b263c4ddd9eb37b3e7d1187862bfab
|
|
| BLAKE2b-256 |
76f417588862f6fa9f5c8e99b1afd376fe1b277058dd89c99250d051be915c23
|