-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathrequest_timings.py
More file actions
34 lines (25 loc) · 994 Bytes
/
request_timings.py
File metadata and controls
34 lines (25 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from __future__ import annotations
from flask import g, has_request_context
def init_request_timings() -> None:
if not has_request_context():
return
if not hasattr(g, "request_timings"):
g.request_timings = {}
if not hasattr(g, "request_timing_context"):
g.request_timing_context = {}
def record_timing(name: str, duration_seconds: float) -> None:
if not has_request_context():
return
init_request_timings()
g.request_timings[name] = round(duration_seconds * 1000.0, 3)
def add_context(**kwargs: object) -> None:
if not has_request_context():
return
init_request_timings()
g.request_timing_context.update({k: v for k, v in kwargs.items() if v is not None})
def get_timings() -> tuple[dict[str, float], dict[str, object]]:
if not has_request_context():
return {}, {}
timings = getattr(g, "request_timings", {})
context = getattr(g, "request_timing_context", {})
return timings, context