File size: 520 Bytes
7a88b43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import time

from fastapi import Request

from app.core.config import log


async def measure_execution_time(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time"] = f"{process_time:.2f} s"  # noqa: E231

    log_dict = {
        "url": request.url.path,
        "method": request.method,
        "process_time": process_time,
    }
    log.info(log_dict, extra=log_dict)

    return response