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:
@@ -200,6 +200,8 @@ export HF_HUB_DISABLE_XET=1 # 这些 repo 是 Xet 存储;强
|
||||
| `BGFILTER_CONFIG` | `configs/default.yaml` | 管线配置文件 |
|
||||
| `BGFILTER_DEVICE` | `cpu` | 同时覆盖 model 和 segmentation 的 device(GPU 用 `cuda`)|
|
||||
| `BGFILTER_MAX_IMAGE_PIXELS` | `4194304` | 超过则返回 `413`(约 4 MP)|
|
||||
| `BGFILTER_LOG_FORMAT` | `text` | `text`(人读)或 `json`(每行一条结构化对象,便于日志采集)。每请求打一行访问日志,含 `request_id`、参数、分阶段耗时(`t_queue/fetch/decode/process/total_ms`);该 id 同时以 `X-Request-ID` 响应头返回。 |
|
||||
| `BGFILTER_LOG_LEVEL` | `INFO` | 根日志级别。 |
|
||||
| `BGFILTER_FETCH_MAX_BYTES` | `26214400` | `image_url` 模式:下载体上限(约 25 MB)→ `413`。无 SSRF 过滤——务必开鉴权 / 调用方可信。 |
|
||||
| `BGFILTER_FETCH_TIMEOUT` | `15` | `image_url` 模式:fetch 读超时(秒;连接超时固定 5 秒)。出站 fetch 并发固定为 `2 × cpu_workers`(硬顶 64)。 |
|
||||
| `BGFILTER_PRELOAD` | `1` | 启动时预加载默认模型(首个请求不用冷加载)|
|
||||
|
||||
Reference in New Issue
Block a user