You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

17 lines
576 B

import time
from fastapi import Request
from fastapi.middleware.base import BaseHTTPMiddleware
class ProcessTimeHeaderMiddleware(BaseHTTPMiddleware):
def __init__(self, app, header_name: str = "X-Process-Time"):
super().__init__(app)
self.header_name = header_name
async def dispatch(self, request: Request, call_next):
start_time = time.perf_counter()
response = await call_next(request)
process_time = time.perf_counter() - start_time
response.headers[self.header_name] = str(process_time)
return response