Skip to content

Commit c894026

Browse files
committed
feat: make use of Server-Timing header
closes #69
1 parent a2c1268 commit c894026

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Middleware to add a header with the process time to the response."""
1+
"""Middleware to add Server-Timing header with proxy processing time."""
22

33
import time
44

@@ -7,12 +7,25 @@
77

88

99
class AddProcessTimeHeaderMiddleware(BaseHTTPMiddleware):
10-
"""Middleware to add a header with the process time to the response."""
10+
"""Middleware to add Server-Timing header with proxy processing time."""
1111

1212
async def dispatch(self, request: Request, call_next) -> Response:
13-
"""Add a header with the process time to the response."""
13+
"""Add Server-Timing header with proxy processing time to the response."""
1414
start_time = time.perf_counter()
1515
response = await call_next(request)
1616
process_time = time.perf_counter() - start_time
17-
response.headers["X-Process-Time"] = f"{process_time:.3f}"
17+
18+
# Add Server-Timing header with proxy processing time
19+
# Format: Server-Timing: proxy;dur=123.456
20+
server_timing_value = f"proxy;dur={process_time:.3f}"
21+
22+
# If there's already a Server-Timing header, append to it
23+
existing_timing = response.headers.get("Server-Timing")
24+
if existing_timing:
25+
response.headers["Server-Timing"] = (
26+
f"{existing_timing}, {server_timing_value}"
27+
)
28+
else:
29+
response.headers["Server-Timing"] = server_timing_value
30+
1831
return response

0 commit comments

Comments
 (0)