Metadata-Version: 2.4
Name: tigzig-api-monitor
Version: 1.2.0
Summary: Lightweight FastAPI middleware for centralized API monitoring
Project-URL: Homepage, https://github.com/amararun/TIGZIG_LOGGER_SERVICE
Project-URL: Repository, https://github.com/amararun/TIGZIG_LOGGER_SERVICE
Project-URL: Issues, https://github.com/amararun/TIGZIG_LOGGER_SERVICE/issues
Author-email: Amar Harolikar <amar@harolikar.com>
License: MIT
Keywords: analytics,api,fastapi,logging,middleware,monitoring
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: System :: Logging
Classifier: Topic :: System :: Monitoring
Requires-Python: >=3.8
Requires-Dist: httpx>=0.24.0
Requires-Dist: starlette>=0.27.0
Description-Content-Type: text/markdown

# tigzig-api-monitor

Lightweight FastAPI middleware for centralized API monitoring. Automatically captures request/response metrics and sends them to a central monitoring service.

## Installation

```bash
pip install tigzig-api-monitor
```

## Quick Start

```python
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 `httpx` and `starlette`)
- **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

```python
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
```

## Noise Filtering (v1.1.0+)

By default, 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 Filtering

```python
# 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 noise filtering)
app.add_middleware(
    APIMonitorMiddleware,
    app_name="MY_BACKEND",
    include_noise=True,
)
```

## License

MIT
