Skip to content

Use Server-Timing header for timing middleware #69

@alukach

Description

@alukach

Our timing middleware should report timing via the Server-Timing header1 to better integrate with the Server Timing2 / Performance Server Timing3 APIs

class AddProcessTimeHeaderMiddleware(BaseHTTPMiddleware):
"""Middleware to add a header with the process time to the response."""
async def dispatch(self, request: Request, call_next) -> Response:
"""Add a header with the process time to the response."""
start_time = time.perf_counter()
response = await call_next(request)
process_time = time.perf_counter() - start_time
response.headers["X-Process-Time"] = f"{process_time:.3f}"
return response

Footnotes

  1. https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Server-Timing

  2. https://web.dev/articles/custom-metrics?utm_source=devtools&utm_campaign=stable#server-timing-api

  3. https://developer.mozilla.org/en-US/docs/Web/API/PerformanceServerTiming

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions