Add structured per-request access logging with a request id
Each request now emits one access line (bgfilter.access) carrying a request_id, the effective params (source file/url, seg_model, cross_check, background_mode, screen_color, auth), input/output sizes, and stage timings (t_queue/fetch/decode/process/total_ms). A middleware binds the id (reusing a sanitized inbound X-Request-ID, else uuid), a logging filter injects it into every record, and it is echoed back as the X-Request-ID response header on every response -- including errors, which also log the detail. Requests that raise still produce a line (fields accumulate on request.state; the middleware finalizes status/total/client). request_id lives in a contextvar (not a thread-local) so a pooled worker never inherits the previous request's id; propagating it into the executor for deep pipeline logs is left for later. BGFILTER_LOG_FORMAT=text|json switches human vs shipper-friendly output. /healthz is not logged. Verified locally via TestClient (fake manager, no models): 200 path logs all fields + timings with rid == header; inbound id echoed; over-long id sanitized; XOR 400s log status + error. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -220,6 +220,8 @@ redirect back to `huggingface.co`, which recent `huggingface_hub` rejects with
|
||||
| `BGFILTER_FETCH_MAX_BYTES` | `26214400` | `image_url` mode: cap the downloaded body (~25 MB) → `413`. No SSRF filtering — keep auth on / callers trusted. |
|
||||
| `BGFILTER_FETCH_TIMEOUT` | `15` | `image_url` mode: read timeout (s) for the fetch (connect fixed at 5 s). Outbound fetch concurrency is fixed at `2 × cpu_workers` (hard cap 64). |
|
||||
| `BGFILTER_PRELOAD` | `1` | Load default models at startup (first request isn't cold) |
|
||||
| `BGFILTER_LOG_FORMAT` | `text` | `text` (human) or `json` (one structured object per line, for log shippers). Each request emits one access line with a `request_id`, params, and stage timings (`t_queue/fetch/decode/process/total_ms`); the id is also echoed as the `X-Request-ID` response header. |
|
||||
| `BGFILTER_LOG_LEVEL` | `INFO` | Root log level. |
|
||||
| `BGFILTER_CROSS_CHECK` | config (`on`) | Force the cross-check veto on/off (`0` disables; needs `BiRefNet_HR-matting`) |
|
||||
| `BGFILTER_CPU_WORKERS` | config `server.cpu_workers` (`1`) | Thread-pool size for request pipelining. `1` = strictly serial (one request at a time). `>1` lets that many requests run concurrently, overlapping their CPU stages while a GPU mutex serializes the model forwards — keeps the GPU fed instead of idle. Biggest win when the pipeline is CPU-bound (e.g. cross-check off). Set it in the config (`server:` section) or override per box with this env var. |
|
||||
| `BGFILTER_GPU_CONCURRENCY` | config `server.gpu_concurrency` (`1`) | How many requests may be inside a GPU forward at once. `1` = single-stream (safe). `2` lets two forwards co-schedule on the SMs (batch-1 underfills them), raising GPU-bound throughput — but concurrent activations multiply VRAM, so size it as `weights + N × peak_forward ≤ VRAM` (on a 32 GB card, `2` peaks ~19 GB at 2048 cross-check; keep `1` on ≤16 GB). Needs `cpu_workers>1` to matter. Config key `server.gpu_concurrency`; this env var overrides it. |
|
||||
|
||||
Reference in New Issue
Block a user