如何在 Python FastAPI 中记录原始 HTTP request/response

How to log raw HTTP request/response in Python FastAPI

我们正在使用 Python FastAPI 编写 Web 服务,该服务将托管在 Kubernetes 中。出于审计目的,我们需要为特定路由保存 request/response 的原始 JSON 正文。 请求和响应的正文大小 JSON 约为 1MB,最好不要影响响应时间。 我们该怎么做?

你可以使用backgroundTasksfastapi到运行后台的一些任务,你的响应时间不会受到影响,请求应该不需要等待这些任务完成。

https://fastapi.tiangolo.com/tutorial/background-tasks/

您可以尝试自定义 APIRouter 就像 FastAPI 官方文档中那样:

import time
from typing import Callable

from fastapi import APIRouter, FastAPI, Request, Response
from fastapi.routing import APIRoute


class TimedRoute(APIRoute):
    def get_route_handler(self) -> Callable:
        original_route_handler = super().get_route_handler()

        async def custom_route_handler(request: Request) -> Response:
            before = time.time()
            response: Response = await original_route_handler(request)
            duration = time.time() - before
            response.headers["X-Response-Time"] = str(duration)
            print(f"route duration: {duration}")
            print(f"route response: {response}")
            print(f"route response headers: {response.headers}")
            return response

        return custom_route_handler


app = FastAPI()
router = APIRouter(route_class=TimedRoute)


@app.get("/")
async def not_timed():
    return {"message": "Not timed"}


@router.get("/timed")
async def timed():
    return {"message": "It's the time of my life"}


app.include_router(router)