fastapi-perfstats 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_perfstats/__init__.py +73 -0
- fastapi_perfstats/auth.py +60 -0
- fastapi_perfstats/collector.py +159 -0
- fastapi_perfstats/config.py +163 -0
- fastapi_perfstats/ext/__init__.py +9 -0
- fastapi_perfstats/ext/base.py +102 -0
- fastapi_perfstats/ext/recent.py +167 -0
- fastapi_perfstats/ext/sizes.py +109 -0
- fastapi_perfstats/ext/stddev.py +84 -0
- fastapi_perfstats/headers.py +36 -0
- fastapi_perfstats/keys.py +34 -0
- fastapi_perfstats/lifecycle.py +43 -0
- fastapi_perfstats/live.py +133 -0
- fastapi_perfstats/middleware.py +81 -0
- fastapi_perfstats/models.py +93 -0
- fastapi_perfstats/mounts.py +34 -0
- fastapi_perfstats/page.py +299 -0
- fastapi_perfstats/parts.py +9 -0
- fastapi_perfstats/prometheus.py +96 -0
- fastapi_perfstats/py.typed +0 -0
- fastapi_perfstats/reset.py +60 -0
- fastapi_perfstats/route.py +48 -0
- fastapi_perfstats/slow_context.py +118 -0
- fastapi_perfstats/sqlite_schema.py +53 -0
- fastapi_perfstats/stores.py +400 -0
- fastapi_perfstats-0.1.0.dist-info/METADATA +478 -0
- fastapi_perfstats-0.1.0.dist-info/RECORD +29 -0
- fastapi_perfstats-0.1.0.dist-info/WHEEL +4 -0
- fastapi_perfstats-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""Recent activity: per-minute buckets give 1/5/15-minute rates, error rates and latency (like a load average).
|
|
2
|
+
|
|
3
|
+
A bucket is one wall-clock minute (``int(ts // 60)``) holding ``count``, ``e5`` (5xx calls) and ``total_ms``.
|
|
4
|
+
Memory keeps a bounded deque per route, SQLite the table ``ext_recent`` (summing naturally across worker processes).
|
|
5
|
+
Both stores clamp a timestamp that goes backwards (clock change) into the newest bucket of the route, so they agree.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import sqlite3
|
|
11
|
+
from collections import deque
|
|
12
|
+
from collections.abc import Callable, Iterable, Mapping
|
|
13
|
+
from typing import Any, NamedTuple
|
|
14
|
+
|
|
15
|
+
from fastapi_perfstats.config import StatsConfig
|
|
16
|
+
from fastapi_perfstats.ext.base import Column, NoopExtension
|
|
17
|
+
from fastapi_perfstats.models import CallRecord, RecentStats, RouteStats
|
|
18
|
+
|
|
19
|
+
MAX_MINUTES = 1440 # upper bound of ``StatsConfig.recent_minutes``; memory buckets never exceed it
|
|
20
|
+
|
|
21
|
+
_SCHEMA = (
|
|
22
|
+
"CREATE TABLE IF NOT EXISTS ext_recent "
|
|
23
|
+
"(key TEXT NOT NULL, minute INTEGER NOT NULL, count INTEGER NOT NULL, e5 INTEGER NOT NULL, "
|
|
24
|
+
"total_ms REAL NOT NULL, PRIMARY KEY (key, minute))"
|
|
25
|
+
)
|
|
26
|
+
_UPSERT = (
|
|
27
|
+
"INSERT INTO ext_recent (key, minute, count, e5, total_ms) VALUES (?, ?, 1, ?, ?) "
|
|
28
|
+
"ON CONFLICT(key, minute) DO UPDATE SET count = count + 1, e5 = e5 + excluded.e5, "
|
|
29
|
+
"total_ms = total_ms + excluded.total_ms"
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Bucket(NamedTuple):
|
|
34
|
+
minute: int
|
|
35
|
+
calls: int
|
|
36
|
+
e5: int
|
|
37
|
+
total_ms: float
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def recent_stats(buckets: Iterable[Bucket], now: float, minutes: int, first_ts: float) -> RecentStats:
|
|
41
|
+
"""Aggregate the buckets of the minutes ``(now_minute - minutes, now_minute]``.
|
|
42
|
+
|
|
43
|
+
``calls_per_min`` divides by the observed span: the full window, shortened to the time since ``first_ts`` for
|
|
44
|
+
young routes (at least one second, so a burst right after start does not explode).
|
|
45
|
+
"""
|
|
46
|
+
now_minute = int(now // 60)
|
|
47
|
+
calls = e5 = 0
|
|
48
|
+
total = 0.0
|
|
49
|
+
for b in buckets:
|
|
50
|
+
if now_minute - minutes < b.minute <= now_minute:
|
|
51
|
+
calls += b.calls
|
|
52
|
+
e5 += b.e5
|
|
53
|
+
total += b.total_ms
|
|
54
|
+
span = max(min((minutes - 1) + (now % 60) / 60, (now - first_ts) / 60), 1 / 60)
|
|
55
|
+
return RecentStats(
|
|
56
|
+
calls=calls,
|
|
57
|
+
calls_per_min=calls / span,
|
|
58
|
+
errors_5xx=e5,
|
|
59
|
+
error_rate_5xx=e5 / calls if calls else 0.0,
|
|
60
|
+
mean_ms=total / calls if calls else 0.0,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def label(minutes: int) -> str:
|
|
65
|
+
return f"{minutes}m"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _fill(row: RouteStats, buckets: list[Bucket], config: StatsConfig, now: float) -> None:
|
|
69
|
+
if not config.recent_minutes:
|
|
70
|
+
return
|
|
71
|
+
row.recent = {label(w): recent_stats(buckets, now, w, row.first_ts) for w in config.recent_windows}
|
|
72
|
+
now_minute = int(now // 60)
|
|
73
|
+
counts = {b.minute: b.calls for b in buckets}
|
|
74
|
+
row.series = [counts.get(m, 0) for m in range(now_minute - config.recent_minutes + 1, now_minute + 1)]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _rate_cell(name: str) -> Callable[[Any, StatsConfig], tuple[str, float | None]]:
|
|
78
|
+
def cell(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
79
|
+
stats = row.recent.get(name)
|
|
80
|
+
return ("n/a", -1.0) if stats is None else (f"{stats.calls_per_min:.2f}", stats.calls_per_min)
|
|
81
|
+
|
|
82
|
+
return cell
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _error_cell(name: str) -> Callable[[Any, StatsConfig], tuple[str, float | None]]:
|
|
86
|
+
def cell(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
87
|
+
stats = row.recent.get(name)
|
|
88
|
+
return ("n/a", -1.0) if stats is None else (f"{stats.error_rate_5xx * 100:.1f}%", stats.error_rate_5xx)
|
|
89
|
+
|
|
90
|
+
return cell
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class RecentExtension(NoopExtension):
|
|
94
|
+
def __init__(self) -> None:
|
|
95
|
+
super().__init__("recent")
|
|
96
|
+
|
|
97
|
+
def sqlite_schema(self) -> tuple[str, ...]:
|
|
98
|
+
return (_SCHEMA,)
|
|
99
|
+
|
|
100
|
+
# -- memory ----------------------------------------------------------------------------------------------
|
|
101
|
+
def memory_update(self, state: dict[str, Any], call: CallRecord) -> None:
|
|
102
|
+
# The hook has no config: keep at most MAX_MINUTES buckets; ``memory_finish`` trims to ``recent_minutes``.
|
|
103
|
+
buckets: deque[list[float]] = state.setdefault("buckets", deque(maxlen=MAX_MINUTES))
|
|
104
|
+
minute = int(call.ts // 60)
|
|
105
|
+
e5 = call.status >= 500
|
|
106
|
+
if buckets and minute <= buckets[-1][0]: # same minute, or the clock went backwards
|
|
107
|
+
last = buckets[-1]
|
|
108
|
+
last[1] += 1
|
|
109
|
+
last[2] += e5
|
|
110
|
+
last[3] += call.duration_ms
|
|
111
|
+
else:
|
|
112
|
+
buckets.append([minute, 1, e5, call.duration_ms])
|
|
113
|
+
|
|
114
|
+
def memory_finish(self, row: RouteStats, state: dict[str, Any], config: StatsConfig, now: float) -> None:
|
|
115
|
+
buckets = state.get("buckets")
|
|
116
|
+
if not config.recent_minutes:
|
|
117
|
+
if buckets is not None:
|
|
118
|
+
buckets.clear() # nothing is kept when the feature is off
|
|
119
|
+
return
|
|
120
|
+
if buckets is None:
|
|
121
|
+
return
|
|
122
|
+
while len(buckets) > config.recent_minutes:
|
|
123
|
+
buckets.popleft()
|
|
124
|
+
_fill(row, [Bucket(int(m), int(c), int(e), t) for m, c, e, t in buckets], config, now)
|
|
125
|
+
|
|
126
|
+
# -- sqlite ----------------------------------------------------------------------------------------------
|
|
127
|
+
def sqlite_update(self, db: sqlite3.Connection, call: CallRecord) -> None:
|
|
128
|
+
minute = int(call.ts // 60)
|
|
129
|
+
newest = db.execute("SELECT MAX(minute) FROM ext_recent WHERE key = ?", (call.key,)).fetchone()[0]
|
|
130
|
+
if newest is not None and minute < newest: # clock went backwards: count into the newest bucket
|
|
131
|
+
minute = newest
|
|
132
|
+
db.execute(_UPSERT, (call.key, minute, call.status >= 500, call.duration_ms))
|
|
133
|
+
|
|
134
|
+
def sqlite_prune(self, db: sqlite3.Connection, config: StatsConfig, now: float) -> None:
|
|
135
|
+
if not config.recent_minutes:
|
|
136
|
+
db.execute("DELETE FROM ext_recent")
|
|
137
|
+
return
|
|
138
|
+
db.execute("DELETE FROM ext_recent WHERE minute <= ?", (int(now // 60) - config.recent_minutes,))
|
|
139
|
+
|
|
140
|
+
def sqlite_finish(
|
|
141
|
+
self, rows: Mapping[str, RouteStats], db: sqlite3.Connection, config: StatsConfig, now: float
|
|
142
|
+
) -> None:
|
|
143
|
+
if not config.recent_minutes:
|
|
144
|
+
return
|
|
145
|
+
by_key: dict[str, list[Bucket]] = {}
|
|
146
|
+
for key, minute, count, e5, total in db.execute("SELECT key, minute, count, e5, total_ms FROM ext_recent"):
|
|
147
|
+
by_key.setdefault(key, []).append(Bucket(minute, count, e5, total))
|
|
148
|
+
for key, row in rows.items():
|
|
149
|
+
_fill(row, by_key.get(key, []), config, now)
|
|
150
|
+
|
|
151
|
+
def sqlite_reset(self, db: sqlite3.Connection) -> None:
|
|
152
|
+
db.execute("DELETE FROM ext_recent")
|
|
153
|
+
|
|
154
|
+
# -- page ------------------------------------------------------------------------------------------------
|
|
155
|
+
def columns(self, config: StatsConfig) -> tuple[Column, ...]:
|
|
156
|
+
if not config.recent_minutes or not config.recent_windows:
|
|
157
|
+
return ()
|
|
158
|
+
cols = [
|
|
159
|
+
Column("freq", f"Calls/min ({label(w)})", min(91 + i, 98), _rate_cell(label(w)))
|
|
160
|
+
for i, w in enumerate(config.recent_windows)
|
|
161
|
+
]
|
|
162
|
+
biggest = label(config.recent_windows[-1])
|
|
163
|
+
cols.append(Column("freq", f"5xx % ({biggest})", 99, _error_cell(biggest)))
|
|
164
|
+
return tuple(cols)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
EXT = RecentExtension()
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"""Request and response payload sizes per route (mean request, mean response, max response)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sqlite3
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from fastapi_perfstats.config import StatsConfig
|
|
10
|
+
from fastapi_perfstats.ext.base import Column, NoopExtension
|
|
11
|
+
from fastapi_perfstats.models import CallRecord, RouteStats
|
|
12
|
+
|
|
13
|
+
_SCHEMA = (
|
|
14
|
+
"CREATE TABLE IF NOT EXISTS ext_sizes (key TEXT PRIMARY KEY, sized_count INTEGER NOT NULL, "
|
|
15
|
+
"req_total INTEGER NOT NULL, resp_total INTEGER NOT NULL, resp_max INTEGER NOT NULL)"
|
|
16
|
+
)
|
|
17
|
+
_UPSERT = (
|
|
18
|
+
"INSERT INTO ext_sizes (key, sized_count, req_total, resp_total, resp_max) VALUES (?, 1, ?, ?, ?) "
|
|
19
|
+
"ON CONFLICT(key) DO UPDATE SET sized_count = sized_count + 1, req_total = req_total + excluded.req_total, "
|
|
20
|
+
"resp_total = resp_total + excluded.resp_total, resp_max = MAX(resp_max, excluded.resp_max)"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
_UNITS = ("B", "KiB", "MiB", "GiB", "TiB")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _bytes(n: float) -> str:
|
|
27
|
+
"""Human readable size: ``512 B``, ``1.5 KiB``, ``2.0 MiB`` (binary units)."""
|
|
28
|
+
value = float(n)
|
|
29
|
+
unit = 0
|
|
30
|
+
while value >= 1024 and unit < len(_UNITS) - 1:
|
|
31
|
+
value /= 1024
|
|
32
|
+
unit += 1
|
|
33
|
+
return f"{value:.0f} B" if unit == 0 else f"{value:.1f} {_UNITS[unit]}"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _size_cell(value: float | None) -> tuple[str, float | None]:
|
|
37
|
+
if value is None:
|
|
38
|
+
return "n/a", -1.0
|
|
39
|
+
return _bytes(value), value
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _mean_request(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
43
|
+
return _size_cell(row.mean_request_bytes)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _mean_response(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
47
|
+
return _size_cell(row.mean_response_bytes)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _max_response(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
51
|
+
return _size_cell(row.max_response_bytes)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _apply(row: RouteStats, count: int, req_total: int, resp_total: int, resp_max: int) -> None:
|
|
55
|
+
if count <= 0:
|
|
56
|
+
return
|
|
57
|
+
row.mean_request_bytes = req_total / count
|
|
58
|
+
row.mean_response_bytes = resp_total / count
|
|
59
|
+
row.max_response_bytes = resp_max
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class SizesExtension(NoopExtension):
|
|
63
|
+
"""Only calls with both sizes known contribute; routes without any sized call keep ``None``."""
|
|
64
|
+
|
|
65
|
+
def __init__(self) -> None:
|
|
66
|
+
super().__init__("sizes")
|
|
67
|
+
|
|
68
|
+
def sqlite_schema(self) -> tuple[str, ...]:
|
|
69
|
+
return (_SCHEMA,)
|
|
70
|
+
|
|
71
|
+
def memory_update(self, state: dict[str, Any], call: CallRecord) -> None:
|
|
72
|
+
if call.request_bytes is None or call.response_bytes is None:
|
|
73
|
+
return
|
|
74
|
+
state["sized_count"] = state.get("sized_count", 0) + 1
|
|
75
|
+
state["req_total"] = state.get("req_total", 0) + call.request_bytes
|
|
76
|
+
state["resp_total"] = state.get("resp_total", 0) + call.response_bytes
|
|
77
|
+
state["resp_max"] = max(state.get("resp_max", 0), call.response_bytes)
|
|
78
|
+
|
|
79
|
+
def memory_finish(self, row: RouteStats, state: dict[str, Any], config: StatsConfig, now: float) -> None:
|
|
80
|
+
count = state.get("sized_count", 0)
|
|
81
|
+
_apply(row, count, state.get("req_total", 0), state.get("resp_total", 0), state.get("resp_max", 0))
|
|
82
|
+
|
|
83
|
+
def sqlite_update(self, db: sqlite3.Connection, call: CallRecord) -> None:
|
|
84
|
+
if call.request_bytes is None or call.response_bytes is None:
|
|
85
|
+
return
|
|
86
|
+
db.execute(_UPSERT, (call.key, call.request_bytes, call.response_bytes, call.response_bytes))
|
|
87
|
+
|
|
88
|
+
def sqlite_finish(
|
|
89
|
+
self, rows: Mapping[str, RouteStats], db: sqlite3.Connection, config: StatsConfig, now: float
|
|
90
|
+
) -> None:
|
|
91
|
+
for key, count, req_total, resp_total, resp_max in db.execute(
|
|
92
|
+
"SELECT key, sized_count, req_total, resp_total, resp_max FROM ext_sizes"
|
|
93
|
+
):
|
|
94
|
+
found = rows.get(key)
|
|
95
|
+
if found is not None:
|
|
96
|
+
_apply(found, count, req_total, resp_total, resp_max)
|
|
97
|
+
|
|
98
|
+
def sqlite_reset(self, db: sqlite3.Connection) -> None:
|
|
99
|
+
db.execute("DELETE FROM ext_sizes")
|
|
100
|
+
|
|
101
|
+
def columns(self, config: StatsConfig) -> tuple[Column, ...]:
|
|
102
|
+
return (
|
|
103
|
+
Column("sizes", "Mean request", 30, _mean_request),
|
|
104
|
+
Column("sizes", "Mean response", 40, _mean_response),
|
|
105
|
+
Column("sizes", "Max response", 50, _max_response),
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
EXT = SizesExtension()
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Error rates (4xx / 5xx share of all calls) and the population standard deviation of durations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
import sqlite3
|
|
7
|
+
from collections.abc import Callable, Mapping
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from fastapi_perfstats.config import StatsConfig
|
|
11
|
+
from fastapi_perfstats.ext.base import Column, NoopExtension
|
|
12
|
+
from fastapi_perfstats.models import CallRecord, RouteStats
|
|
13
|
+
|
|
14
|
+
_SCHEMA = "CREATE TABLE IF NOT EXISTS ext_stddev (key TEXT PRIMARY KEY, total_sq_ms REAL NOT NULL)"
|
|
15
|
+
_UPSERT = (
|
|
16
|
+
"INSERT INTO ext_stddev (key, total_sq_ms) VALUES (?, ?) "
|
|
17
|
+
"ON CONFLICT(key) DO UPDATE SET total_sq_ms = total_sq_ms + excluded.total_sq_ms"
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _stddev(total_sq: float, row: RouteStats) -> float:
|
|
22
|
+
"""Population stddev ``sqrt(E[x^2] - mean^2)`` from the sum of squares; never negative (rounding)."""
|
|
23
|
+
return math.sqrt(max(total_sq / row.count - row.mean_ms**2, 0.0))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _apply_rates(row: RouteStats) -> None:
|
|
27
|
+
row.error_rate_4xx = row.errors_4xx / row.count if row.count else 0.0
|
|
28
|
+
row.error_rate_5xx = row.errors_5xx / row.count if row.count else 0.0
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _percent_cell(attr: str) -> Callable[[Any, StatsConfig], tuple[str, float | None]]:
|
|
32
|
+
def cell(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
33
|
+
rate: float = getattr(row, attr)
|
|
34
|
+
return f"{rate * 100:.1f}%", rate
|
|
35
|
+
|
|
36
|
+
return cell
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _stddev_cell(row: RouteStats, config: StatsConfig) -> tuple[str, float | None]:
|
|
40
|
+
if row.stddev_ms is None:
|
|
41
|
+
return "n/a", -1.0
|
|
42
|
+
return f"{row.stddev_ms:.2f}", row.stddev_ms
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class StddevExtension(NoopExtension):
|
|
46
|
+
def __init__(self) -> None:
|
|
47
|
+
super().__init__("stddev")
|
|
48
|
+
|
|
49
|
+
def sqlite_schema(self) -> tuple[str, ...]:
|
|
50
|
+
return (_SCHEMA,)
|
|
51
|
+
|
|
52
|
+
def memory_update(self, state: dict[str, Any], call: CallRecord) -> None:
|
|
53
|
+
state["total_sq"] = state.get("total_sq", 0.0) + call.duration_ms**2
|
|
54
|
+
|
|
55
|
+
def memory_finish(self, row: RouteStats, state: dict[str, Any], config: StatsConfig, now: float) -> None:
|
|
56
|
+
_apply_rates(row)
|
|
57
|
+
if "total_sq" in state and row.count:
|
|
58
|
+
row.stddev_ms = _stddev(state["total_sq"], row)
|
|
59
|
+
|
|
60
|
+
def sqlite_update(self, db: sqlite3.Connection, call: CallRecord) -> None:
|
|
61
|
+
db.execute(_UPSERT, (call.key, call.duration_ms**2))
|
|
62
|
+
|
|
63
|
+
def sqlite_finish(
|
|
64
|
+
self, rows: Mapping[str, RouteStats], db: sqlite3.Connection, config: StatsConfig, now: float
|
|
65
|
+
) -> None:
|
|
66
|
+
for row in rows.values():
|
|
67
|
+
_apply_rates(row)
|
|
68
|
+
for key, total_sq in db.execute("SELECT key, total_sq_ms FROM ext_stddev"):
|
|
69
|
+
found = rows.get(key)
|
|
70
|
+
if found is not None and found.count:
|
|
71
|
+
found.stddev_ms = _stddev(total_sq, found)
|
|
72
|
+
|
|
73
|
+
def sqlite_reset(self, db: sqlite3.Connection) -> None:
|
|
74
|
+
db.execute("DELETE FROM ext_stddev")
|
|
75
|
+
|
|
76
|
+
def columns(self, config: StatsConfig) -> tuple[Column, ...]:
|
|
77
|
+
return (
|
|
78
|
+
Column("freq", "5xx %", 45, _percent_cell("error_rate_5xx")),
|
|
79
|
+
Column("freq", "4xx %", 65, _percent_cell("error_rate_4xx")),
|
|
80
|
+
Column("rt", "Stddev", 65, _stddev_cell),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
EXT = StddevExtension()
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Response headers of the stats endpoints (owned by #8)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import base64
|
|
6
|
+
import hashlib
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def csp_hash(text: str) -> str:
|
|
10
|
+
"""CSP source expression (``'sha256-...'``) for an inline ``<style>`` / ``<script>`` body, exactly as served."""
|
|
11
|
+
digest = hashlib.sha256(text.encode()).digest()
|
|
12
|
+
return "'sha256-" + base64.b64encode(digest).decode("ascii") + "'"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def page_headers(css: str, js: str) -> dict[str, str]:
|
|
16
|
+
"""Headers for the HTML page, given the exact inline CSS and JS between its tags (hashes computed per call).
|
|
17
|
+
|
|
18
|
+
Rule for the page markup: no inline event handlers, no ``style="..."`` attributes, no dynamic content inside
|
|
19
|
+
``<script>`` / ``<style>``; pass data via ``data-*`` attributes.
|
|
20
|
+
"""
|
|
21
|
+
policy = (
|
|
22
|
+
f"default-src 'none'; style-src {csp_hash(css)}; script-src {csp_hash(js)}; img-src 'self' data:; "
|
|
23
|
+
"connect-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'"
|
|
24
|
+
)
|
|
25
|
+
return {
|
|
26
|
+
"Content-Security-Policy": policy,
|
|
27
|
+
"X-Content-Type-Options": "nosniff",
|
|
28
|
+
"Referrer-Policy": "no-referrer",
|
|
29
|
+
"X-Frame-Options": "DENY",
|
|
30
|
+
"Cache-Control": "no-store",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def data_headers() -> dict[str, str]:
|
|
35
|
+
"""Headers for the JSON / text endpoints and the 401/403 responses."""
|
|
36
|
+
return {"Cache-Control": "no-store", "X-Content-Type-Options": "nosniff"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Key building for a finished call (owned by #5)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from fastapi_perfstats.config import StatsConfig
|
|
9
|
+
from fastapi_perfstats.models import KNOWN_METHODS, OTHER_METHOD, UNMATCHED_ROUTE
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def build_key(scope: Mapping[str, Any], config: StatsConfig, prefix: str) -> str | None:
|
|
13
|
+
"""Default key ``METHOD + prefix + route template``; ``None`` skips the call.
|
|
14
|
+
|
|
15
|
+
Requests without a matching route follow ``config.unmatched``: ``"group"`` -> one key ``METHOD <unmatched>``,
|
|
16
|
+
``"raw"`` -> the raw path, ``"skip"`` -> ``None``. Unknown HTTP methods are keyed as ``OTHER``.
|
|
17
|
+
"""
|
|
18
|
+
if config.key_func is not None:
|
|
19
|
+
key = config.key_func(scope)
|
|
20
|
+
else:
|
|
21
|
+
raw_method = scope.get("method", "GET")
|
|
22
|
+
method = raw_method if raw_method in KNOWN_METHODS else OTHER_METHOD
|
|
23
|
+
route = scope.get("route") # set by routing, i.e. only valid *after* the app has run
|
|
24
|
+
template = getattr(route, "path", None)
|
|
25
|
+
if template:
|
|
26
|
+
path = prefix + template
|
|
27
|
+
elif config.unmatched == "skip":
|
|
28
|
+
return None
|
|
29
|
+
elif config.unmatched == "group":
|
|
30
|
+
path = UNMATCHED_ROUTE
|
|
31
|
+
else:
|
|
32
|
+
path = scope.get("path", "")
|
|
33
|
+
key = f"{method} {path}"
|
|
34
|
+
return None if key is None else key[: config.max_key_length]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""ASGI lifespan handling (owned by #7): close the store on shutdown, show dropped records."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
import anyio.to_thread
|
|
8
|
+
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
9
|
+
|
|
10
|
+
from fastapi_perfstats.ext.base import PagePart
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from fastapi_perfstats.collector import StatsCollector
|
|
14
|
+
|
|
15
|
+
_SHUTDOWN_DONE = ("lifespan.shutdown.complete", "lifespan.shutdown.failed")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def handle_lifespan(collector: StatsCollector, app: ASGIApp, scope: Scope, receive: Receive, send: Send) -> None:
|
|
19
|
+
"""Run the lifespan protocol; close the store before the app's shutdown message goes out.
|
|
20
|
+
|
|
21
|
+
With ``close_on_shutdown=False`` the app sees the lifespan untouched. ``close()`` may block (the writer thread is
|
|
22
|
+
joined for up to 10 s), so it runs in a worker thread.
|
|
23
|
+
"""
|
|
24
|
+
if not collector.config.close_on_shutdown:
|
|
25
|
+
await app(scope, receive, send)
|
|
26
|
+
return
|
|
27
|
+
|
|
28
|
+
async def send_wrapper(message: Message) -> None:
|
|
29
|
+
if message["type"] in _SHUTDOWN_DONE:
|
|
30
|
+
await anyio.to_thread.run_sync(collector.close)
|
|
31
|
+
await send(message)
|
|
32
|
+
|
|
33
|
+
await app(scope, receive, send_wrapper)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _html_top(collector: StatsCollector) -> str:
|
|
37
|
+
dropped = collector.store.dropped
|
|
38
|
+
if dropped <= 0:
|
|
39
|
+
return ""
|
|
40
|
+
return f'<p class="bad">{int(dropped)} records dropped (writer queue full)</p>'
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
PART = PagePart(name="lifecycle", html_top=_html_top)
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Live (auto-refreshing) stats page: refresh selector, table merge script and per-route sparklines (#16).
|
|
2
|
+
|
|
3
|
+
The selector is always rendered; ``StatsConfig.refresh_seconds`` is its initial value (``None`` and ``0`` = Off).
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from html import escape
|
|
9
|
+
from typing import TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from fastapi_perfstats.config import StatsConfig
|
|
12
|
+
from fastapi_perfstats.ext.base import Column, NoopExtension, PagePart
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from fastapi_perfstats.collector import StatsCollector
|
|
16
|
+
|
|
17
|
+
_CHOICES = ((0, "Off"), (5, "5 s"), (15, "15 s"), (60, "60 s"))
|
|
18
|
+
_WIDTH = 120
|
|
19
|
+
_HEIGHT = 20
|
|
20
|
+
_PAD = 2
|
|
21
|
+
|
|
22
|
+
_JS = """
|
|
23
|
+
(function(){
|
|
24
|
+
var sel=document.getElementById('refresh'),note=document.getElementById('refresh-note');
|
|
25
|
+
if(!sel)return;
|
|
26
|
+
var KEY='fastapi-perfstats-refresh',timer=null,busy=false,state={};
|
|
27
|
+
function valid(v){return v==='0'||v==='5'||v==='15'||v==='60';}
|
|
28
|
+
try{var saved=localStorage.getItem(KEY);if(saved!==null&&valid(saved))sel.value=saved;}catch(e){}
|
|
29
|
+
document.querySelectorAll('table.s').forEach(function(t){
|
|
30
|
+
var st=state[t.id]={i:-1,dir:1};
|
|
31
|
+
t.tHead.querySelectorAll('th').forEach(function(th,i){
|
|
32
|
+
th.addEventListener('click',function(){st.dir=-st.dir;st.i=i;});
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
function resort(t){
|
|
36
|
+
var st=state[t.id],tb=t.tBodies[0];
|
|
37
|
+
if(!st||st.i<0)return;
|
|
38
|
+
Array.from(tb.rows).sort(function(a,b){
|
|
39
|
+
var x=a.cells[st.i],y=b.cells[st.i],p=x.dataset.v,q=y.dataset.v;
|
|
40
|
+
if(p!==undefined&&q!==undefined)return (parseFloat(p)-parseFloat(q))*st.dir;
|
|
41
|
+
return x.textContent.localeCompare(y.textContent)*st.dir;
|
|
42
|
+
}).forEach(function(r){tb.appendChild(r);});
|
|
43
|
+
}
|
|
44
|
+
function refresh(){
|
|
45
|
+
if(busy)return;
|
|
46
|
+
busy=true;
|
|
47
|
+
fetch(location.href,{credentials:'same-origin',cache:'no-store'}).then(function(r){
|
|
48
|
+
if(!r.ok)throw new Error('http '+r.status);
|
|
49
|
+
return r.text();
|
|
50
|
+
}).then(function(text){
|
|
51
|
+
var doc=new DOMParser().parseFromString(text,'text/html');
|
|
52
|
+
document.querySelectorAll('table.s').forEach(function(t){
|
|
53
|
+
var fresh=doc.getElementById(t.id);
|
|
54
|
+
if(!fresh)return;
|
|
55
|
+
var tb=t.tBodies[0];
|
|
56
|
+
tb.replaceChildren.apply(tb,Array.from(fresh.tBodies[0].childNodes).map(function(n){
|
|
57
|
+
return document.importNode(n,true);
|
|
58
|
+
}));
|
|
59
|
+
resort(t);
|
|
60
|
+
});
|
|
61
|
+
document.querySelectorAll('input.f').forEach(function(inp){inp.dispatchEvent(new Event('input'));});
|
|
62
|
+
note.textContent='';
|
|
63
|
+
}).catch(function(){
|
|
64
|
+
note.textContent='refresh failed';
|
|
65
|
+
}).then(function(){busy=false;});
|
|
66
|
+
}
|
|
67
|
+
function apply(){
|
|
68
|
+
if(timer!==null){clearInterval(timer);timer=null;}
|
|
69
|
+
var s=parseInt(sel.value,10);
|
|
70
|
+
if(s>0)timer=setInterval(refresh,s*1000);
|
|
71
|
+
}
|
|
72
|
+
sel.addEventListener('change',function(){
|
|
73
|
+
try{localStorage.setItem(KEY,sel.value);}catch(e){}
|
|
74
|
+
apply();
|
|
75
|
+
});
|
|
76
|
+
apply();
|
|
77
|
+
})();
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _html_top(collector: StatsCollector) -> str:
|
|
82
|
+
current = collector.config.refresh_seconds or 0
|
|
83
|
+
options = "".join(
|
|
84
|
+
f'<option value="{seconds}"{" selected" if seconds == current else ""}>{escape(label)}</option>'
|
|
85
|
+
for seconds, label in _CHOICES
|
|
86
|
+
)
|
|
87
|
+
return (
|
|
88
|
+
'<p><label>Auto-refresh <select id="refresh">' + options + "</select></label> "
|
|
89
|
+
'<span id="refresh-note" class="mut"></span></p>'
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _body_attrs(config: StatsConfig) -> dict[str, str]:
|
|
94
|
+
return {"data-refresh": str(config.refresh_seconds or 0)}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def sparkline(series: list[int], minutes: int) -> str:
|
|
98
|
+
"""Inline SVG of ``series`` (calls per minute, oldest first); an empty or all-zero series is a flat line."""
|
|
99
|
+
peak = max(series, default=0)
|
|
100
|
+
n = len(series)
|
|
101
|
+
usable = _HEIGHT - 2 * _PAD
|
|
102
|
+
points = []
|
|
103
|
+
for i, v in enumerate(series):
|
|
104
|
+
x = _WIDTH * i / (n - 1) if n > 1 else _WIDTH / 2
|
|
105
|
+
y = _HEIGHT - _PAD - (usable * v / peak if peak else 0)
|
|
106
|
+
points.append(f"{x:.1f},{y:.1f}")
|
|
107
|
+
if not points:
|
|
108
|
+
points = [f"0,{_HEIGHT - _PAD}", f"{_WIDTH},{_HEIGHT - _PAD}"]
|
|
109
|
+
label = escape(f"calls per minute, last {minutes} min, max {peak}")
|
|
110
|
+
return (
|
|
111
|
+
f'<svg width="{_WIDTH}" height="{_HEIGHT}" viewBox="0 0 {_WIDTH} {_HEIGHT}" role="img" aria-label="{label}">'
|
|
112
|
+
f'<polyline points="{" ".join(points)}" fill="none" stroke="currentColor" stroke-width="1"/></svg>'
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class _LiveExtension(NoopExtension):
|
|
117
|
+
def columns(self, config: StatsConfig) -> tuple[Column, ...]:
|
|
118
|
+
if config.recent_minutes <= 0:
|
|
119
|
+
return ()
|
|
120
|
+
minutes = config.recent_minutes
|
|
121
|
+
return (
|
|
122
|
+
Column(
|
|
123
|
+
"freq",
|
|
124
|
+
f"Last {minutes} min",
|
|
125
|
+
25,
|
|
126
|
+
lambda r, c: (sparkline(r.series, minutes), float(sum(r.series))),
|
|
127
|
+
raw_html=True,
|
|
128
|
+
),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
PART = PagePart(name="live", html_top=_html_top, js=_JS, body_attrs=_body_attrs)
|
|
133
|
+
EXT = _LiveExtension("live")
|