Metadata-Version: 2.4
Name: djogbe-tracer
Version: 0.1.0
Summary: Djogbe ASGI middleware for per-endpoint request metrics (method, route, latency, status)
Requires-Python: >=3.9
Description-Content-Type: text/markdown

# djogbe-tracer

A lightweight, dependency-free ASGI middleware that instruments API endpoints
to capture per-request metrics: HTTP method, matched route template, status
code, and latency (ms). Works with any ASGI 3 application (FastAPI,
Starlette, or a raw ASGI app) — no framework-specific import required.

Metrics are emitted as a single structured JSON line per request via the
standard `logging` module (logger name `djogbe.access`), so they can be
captured by any log-shipping pipeline already in place (e.g. CloudWatch Logs
→ Kinesis) without new infrastructure.

## Install

```bash
pip install djogbe-tracer
```

Or, from source in this monorepo:

```bash
cd tracers/python
uv sync
```

## Usage

```python
from fastapi import FastAPI
from djogbe_tracer import TracerMiddleware

app = FastAPI()
app.add_middleware(TracerMiddleware)
```

For a plain ASGI app:

```python
from djogbe_tracer import TracerMiddleware

app = TracerMiddleware(my_asgi_app)
```

## Emitted log shape

```json
{"event": "http_request", "method": "GET", "route": "/items/{item_id}", "status": 200, "latency_ms": 12.4, "timestamp": 1721840000.123}
```

`route` is the matched route **template** (e.g. `/items/{item_id}`), not the
raw request path, to keep metric cardinality bounded. When no route matches
(e.g. a 404), the raw request path is used as a fallback so the request is
still observed.

## Development

```bash
cd tracers/python
uv sync
uv run pytest
```
