xtr-web-profiler 7.0.0__tar.gz

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.
Files changed (53) hide show
  1. xtr_web_profiler-7.0.0/LICENSE +22 -0
  2. xtr_web_profiler-7.0.0/PKG-INFO +322 -0
  3. xtr_web_profiler-7.0.0/README.md +298 -0
  4. xtr_web_profiler-7.0.0/pyproject.toml +155 -0
  5. xtr_web_profiler-7.0.0/pyproject.toml.orig +154 -0
  6. xtr_web_profiler-7.0.0/src/xtr_web_profiler/.agents/skills/xtr-web-profiler/SKILL.md +122 -0
  7. xtr_web_profiler-7.0.0/src/xtr_web_profiler/__init__.py +40 -0
  8. xtr_web_profiler-7.0.0/src/xtr_web_profiler/bundle/__init__.py +14 -0
  9. xtr_web_profiler-7.0.0/src/xtr_web_profiler/bundle/web_debug_toolbar_middleware_factory.py +41 -0
  10. xtr_web_profiler-7.0.0/src/xtr_web_profiler/bundle/web_profiler_bundle.py +147 -0
  11. xtr_web_profiler-7.0.0/src/xtr_web_profiler/bundle/web_profiler_config.py +57 -0
  12. xtr_web_profiler-7.0.0/src/xtr_web_profiler/controller/__init__.py +7 -0
  13. xtr_web_profiler-7.0.0/src/xtr_web_profiler/controller/profiler_controller.py +371 -0
  14. xtr_web_profiler-7.0.0/src/xtr_web_profiler/csp/__init__.py +19 -0
  15. xtr_web_profiler-7.0.0/src/xtr_web_profiler/csp/content_security_policy_handler.py +126 -0
  16. xtr_web_profiler-7.0.0/src/xtr_web_profiler/csp/nonce_generator.py +21 -0
  17. xtr_web_profiler-7.0.0/src/xtr_web_profiler/exception/__init__.py +9 -0
  18. xtr_web_profiler-7.0.0/src/xtr_web_profiler/exception/invalid_argument_error.py +14 -0
  19. xtr_web_profiler-7.0.0/src/xtr_web_profiler/exception/template_not_found_error.py +32 -0
  20. xtr_web_profiler-7.0.0/src/xtr_web_profiler/exception/web_profiler_error.py +13 -0
  21. xtr_web_profiler-7.0.0/src/xtr_web_profiler/profiler/__init__.py +7 -0
  22. xtr_web_profiler-7.0.0/src/xtr_web_profiler/profiler/collector_view.py +25 -0
  23. xtr_web_profiler-7.0.0/src/xtr_web_profiler/profiler/template_manager.py +72 -0
  24. xtr_web_profiler-7.0.0/src/xtr_web_profiler/py.typed +0 -0
  25. xtr_web_profiler-7.0.0/src/xtr_web_profiler/recipe/__init__.py +15 -0
  26. xtr_web_profiler-7.0.0/src/xtr_web_profiler/recipe/files/config/routes/dev/web_profiler.py.tmpl +26 -0
  27. xtr_web_profiler-7.0.0/src/xtr_web_profiler/recipe/manifest.toml +27 -0
  28. xtr_web_profiler-7.0.0/src/xtr_web_profiler/routing/__init__.py +7 -0
  29. xtr_web_profiler-7.0.0/src/xtr_web_profiler/routing/_endpoints.py +123 -0
  30. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/ajax.html.j2 +71 -0
  31. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/config.html.j2 +167 -0
  32. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/events.html.j2 +142 -0
  33. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/exception.html.j2 +135 -0
  34. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/logger.html.j2 +151 -0
  35. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/memory.html.j2 +60 -0
  36. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/request.html.j2 +155 -0
  37. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/router.html.j2 +147 -0
  38. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/collector/time.html.j2 +388 -0
  39. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/base.html.j2 +1462 -0
  40. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/info.html.j2 +33 -0
  41. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/layout.html.j2 +65 -0
  42. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/open.html.j2 +59 -0
  43. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/results.html.j2 +75 -0
  44. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/toolbar.css.j2 +584 -0
  45. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/toolbar.html.j2 +64 -0
  46. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/toolbar_item.html.j2 +27 -0
  47. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/toolbar_js.html.j2 +454 -0
  48. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templates/profiler/toolbar_redirect.html.j2 +167 -0
  49. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templating/__init__.py +16 -0
  50. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templating/dump.py +91 -0
  51. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templating/environment_factory.py +86 -0
  52. xtr_web_profiler-7.0.0/src/xtr_web_profiler/templating/template_loader.py +65 -0
  53. xtr_web_profiler-7.0.0/src/xtr_web_profiler/web_debug_toolbar_middleware.py +281 -0
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 xterr
4
+ Copyright (c) 2004-present Fabien Potencier
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
@@ -0,0 +1,322 @@
1
+ Metadata-Version: 2.4
2
+ Name: xtr-web-profiler
3
+ Version: 7.0.0
4
+ Summary: A development toolbar on every page and a profile of every request: timing, logs, events, routing, the request and the response.
5
+ Keywords: profiler,debug,toolbar,development,fastapi,timeline
6
+ Author: Xterr
7
+ Author-email: Xterr <me@xterr.dev>
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3.14
13
+ Classifier: Topic :: Software Development :: Debuggers
14
+ Classifier: Typing :: Typed
15
+ Requires-Dist: fastapi>=0.143.1
16
+ Requires-Dist: jinja2>=3.1
17
+ Requires-Dist: markupsafe>=2.1
18
+ Requires-Dist: starlette>=1.6
19
+ Requires-Dist: xtr-dependency-injection>=7.0,<8
20
+ Requires-Dist: xtr-http-kernel>=7.0,<8
21
+ Requires-Dist: xtr-http-message-contracts>=7.0,<8
22
+ Requires-Python: >=3.14.4
23
+ Description-Content-Type: text/markdown
24
+
25
+ <div align="center">
26
+
27
+ # xtr-web-profiler
28
+
29
+ **A development toolbar on every page, and a profile of every request.**
30
+
31
+ <img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
32
+ <img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
33
+ <img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
34
+
35
+ </div>
36
+
37
+ ---
38
+
39
+ ## Why?
40
+
41
+ "What did that request actually do?" is the question asked most while building a web
42
+ application, and print statements answer it one piece at a time. In development, this package
43
+ answers it for every request at once: a toolbar at the foot of each HTML page shows the status,
44
+ the time, the memory, the log, the events and the route, and one click opens the request's full
45
+ profile — kept for two days and searchable.
46
+
47
+ - 🧭 **A toolbar on every page.** Drawn into each HTML response as it streams out; nothing to add
48
+ to a template.
49
+ - ⏱️ **A timeline.** Every event listener, the application's own handling and the whole request
50
+ on one axis, filterable by duration and by kind.
51
+ - 📜 **The request's log.** Every record it made — and only its own, however many requests the
52
+ process serves at once.
53
+ - 💥 **The exception, with its frames.** Source around each line, local variables, the chain of
54
+ causes; a link opens the line in your editor.
55
+ - 🔁 **Background requests too.** The toolbar lists the `fetch` and `XMLHttpRequest` calls the
56
+ page makes, each linked to its own profile.
57
+ - 🧩 **Panels from any library.** A data collector and a template are a new panel; the profiler
58
+ does not need to know it exists.
59
+
60
+ ## Install
61
+
62
+ ```sh
63
+ uv run xtr-recipes recipes:add --dev xtr-web-profiler
64
+ ```
65
+
66
+ Requires Python 3.14.4+. It installs as a development dependency: production does not install it,
67
+ and the recipe lists the bundle for the `dev` environment only.
68
+
69
+ ## Quick start
70
+
71
+ With [xtr-http-kernel](../xtr-http-kernel) already serving the application through
72
+ `setup(app, kernel)`, adding the package is the whole of it. Start the application in `dev`, open
73
+ any HTML page, and the toolbar is at its foot:
74
+
75
+ ```console
76
+ $ curl -si localhost:8000/books | grep -i debug
77
+ x-debug-token: 3f9a1c
78
+ x-debug-token-link: http://localhost:8000/_profiler/3f9a1c
79
+ ```
80
+
81
+ A response that is not a page — JSON from an API — carries the same two headers: follow
82
+ `X-Debug-Token-Link` to its profile, or open `/_profiler/latest`.
83
+
84
+ ## What a profile shows
85
+
86
+ | Panel | Shows |
87
+ |---|---|
88
+ | Request | the method, URI, query, parsed body, path parameters, cookies, headers and state of the request; the status and headers of the response; a `curl` command replaying it. Credentials — `Authorization`, `Cookie`, `Set-Cookie`, and every field named like a password, a secret or a token — are kept as `******` |
89
+ | Time | the whole request's duration and a timeline of every timed event: the request's section, the application's own handling (`controller`), each event listener |
90
+ | Memory | the peak memory of the process serving the request |
91
+ | AJAX | the background requests the page made, listed in its toolbar |
92
+ | Exception | what handling raised: class, message, chain of causes, and each frame with its source and local variables |
93
+ | Logs | every record the request logged, by level, with its context — when [xtr-logging](../xtr-logging) is active. Context entries named like credentials are kept as `******` too |
94
+ | Events | the listeners called, how often and for how long, those not called, and the events nobody listened to |
95
+ | Routing | the route that answered — name, path template, methods, endpoint, parameters — a redirect's target, and how every route of the application matched the request |
96
+ | Config | the Python version, the environment, the active bundles and every xtr package installed |
97
+
98
+ The profiles themselves are collected by [xtr-http-kernel](../xtr-http-kernel#profiler); this
99
+ package shows them.
100
+
101
+ ## The toolbar
102
+
103
+ The toolbar is drawn into a response only when all of these hold: the toolbar is on, the response
104
+ names a profile in `X-Debug-Token`, it is no redirect, its content type is HTML, it is no
105
+ attachment, its body is not compressed, and the request was not a background one —
106
+ `X-Requested-With: XMLHttpRequest`, or a fetch the browser marks as not loading a document. It
107
+ goes in just before the closing `</body>`, found even when a streamed body splits the tag across
108
+ two chunks, and `Content-Length` is dropped since the body grew. A page without `</body>` goes out
109
+ as it was.
110
+
111
+ What goes into the page is small: a placeholder and a script. The script fetches the toolbar from
112
+ `/_wdt/<token>` — retrying a moment later when the profile has not been written yet, since it is
113
+ written once the response has left — and remembers whether you minimised it.
114
+
115
+ A compressed body cannot be drawn into, and a browser always accepts one. So a request that may get
116
+ the toolbar — one accepting HTML, or anything, that is not a background request — reaches the
117
+ application without its `Accept-Encoding`: an application compressing its responses, with the
118
+ framework's `GZipMiddleware` say, leaves its pages uncompressed in development, and its other
119
+ responses compressed.
120
+
121
+ ### Background requests
122
+
123
+ The page's `fetch` and `XMLHttpRequest` calls are recorded as they go, with their status, their
124
+ duration and the profile named by their response, and listed in the toolbar. Calls to the
125
+ profiler's own paths are left out — `excluded_ajax_paths` says which. With `ajax_replace`, a
126
+ background request whose response names a profile reloads the toolbar with that profile — for a
127
+ page that changes what it shows without loading another.
128
+
129
+ ### Redirects
130
+
131
+ With `intercept_redirects`, a redirect answering a page is replaced by a page saying where it
132
+ leads, toolbar included, so the request that redirected — a form post, a sign-in — can be read
133
+ before following it.
134
+
135
+ ### Content security policy
136
+
137
+ The toolbar is an inline script and an inline style. A page sending a policy gets the toolbar's
138
+ nonces added to every script and style directive of it, so the toolbar runs and the page's own
139
+ rules stay as they were; a page without a policy is left alone. The profiler's own pages are sent
140
+ with no policy at all.
141
+
142
+ ## Editor links
143
+
144
+ A frame of an exception links to the line that ran. By default the link opens the profiler's own
145
+ source view, which shows only files under the project directory and never a hidden one. Point it at
146
+ your editor instead with `file_link_format`, `{file}` and `{line}` standing for them:
147
+
148
+ | Editor | `file_link_format` |
149
+ |---|---|
150
+ | PyCharm | `"pycharm://open?file={file}&line={line}"` |
151
+ | VS Code | `"vscode://file/{file}:{line}"` |
152
+ | Cursor | `"cursor://file/{file}:{line}"` |
153
+
154
+ ## Writing a panel
155
+
156
+ A panel is a data collector — registered by any bundle, see
157
+ [xtr-http-kernel](../xtr-http-kernel#writing-a-collector) — and a template naming up to four
158
+ blocks:
159
+
160
+ - `toolbar` — the item in the toolbar: call `toolbar_item(name, icon, text, link, status)` with
161
+ the icon and value shown and the text of its popover;
162
+ - `menu` — the entry in the profile page's menu;
163
+ - `panel` — the panel itself;
164
+ - `head` — anything the panel needs in the page's head.
165
+
166
+ The template is a file of the collector's own package, under `templates/`, named
167
+ `"@<import package>/<path>"`; the package needs no template engine of its own:
168
+
169
+ ```jinja
170
+ {# src/xtr_orm/templates/collector/orm.html.j2 #}
171
+ {% block toolbar %}
172
+ {% from "@xtr_web_profiler/profiler/toolbar_item.html.j2" import toolbar_item %}
173
+ {% set icon %}<span class="xtr-toolbar-value">{{ collector.data.count }} queries</span>{% endset %}
174
+ {% set text %}<b>Time</b> {{ collector.data.duration_ms | format_ms }}{% endset %}
175
+ {{ toolbar_item(collector.name, icon, text, link=panel_url(collector.name)) }}
176
+ {% endblock %}
177
+ {% block menu %}<span class="xtr-label">Database</span>{% endblock %}
178
+ {% block panel %}
179
+ {% for query in collector.data.queries %}<pre>{{ query.sql }}</pre>{% endfor %}
180
+ {% endblock %}
181
+ ```
182
+
183
+ Every block sees `collector` — `collector.name`, and `collector.data`, what the collector kept —
184
+ `profile`, `token`, `url(route_name, **parameters)`, `panel_url(panel)`,
185
+ `open_file_url(file, line)`, and the CSP nonces `csp_script_nonce` and `csp_style_nonce` for an
186
+ inline script or style. Filters: `profiler_dump` shows a value kept through `clone_var` as a
187
+ browsable tree; `format_bytes`, `format_ms`, `format_time` and `file_link(file, line)`. A block
188
+ naming anything else fails, rather than rendering empty.
189
+
190
+ ## Use in an application
191
+
192
+ Everything adding this package to an application on
193
+ [xtr-dependency-injection](../xtr-dependency-injection) takes — and, read backwards, what
194
+ removing it undoes.
195
+
196
+ - **Install** — `uv run xtr-recipes recipes:add --dev xtr-web-profiler`: a development dependency,
197
+ never installed in production. [xtr-http-kernel](../xtr-http-kernel) comes with it; install
198
+ [xtr-logging](../xtr-logging) beside it for the log panel. The timeline needs nothing more:
199
+ [xtr-stopwatch](../xtr-stopwatch) comes with the http kernel.
200
+ - **Recipe** — `recipes:add` does the *Activate* and *Configure* steps below: it lists
201
+ `WebProfilerBundle` for `dev` and writes `<app>/config/routes/dev/web_profiler.py`. There is no
202
+ environment or ignore line to write; it prints the `setup(app, kernel)` step, which a recipe
203
+ cannot make for you.
204
+ - **Activate** — `WebProfilerBundle: {"dev": True}` in `BUNDLES` in `<app>/config/bundles.py`,
205
+ imported from `xtr_web_profiler.bundle`, and `setup(app, kernel)` where the application is
206
+ built, as the http kernel requires.
207
+ - **Brings along** — the [http kernel](../xtr-http-kernel)'s bundle, required outright, and with it
208
+ the event dispatcher's. Listing this bundle turns the http kernel's profiler on in the same
209
+ environment.
210
+ - **Configure** — `<app>/config/routes/dev/web_profiler.py`, which the recipe writes, serves the
211
+ toolbar under `/_wdt` and the pages under `/_profiler`; it sits in the `dev` folder, so nothing
212
+ of the profiler is routed elsewhere, and deleting it takes the pages down. The bundle's own
213
+ configuration needs nothing; a change goes in `<app>/config/packages/dev/web_profiler.py` — see
214
+ [Configure](#configure).
215
+ - **Environment** — nothing.
216
+ - **Ignore** — nothing: the profiles are kept under the kernel's cache directory.
217
+ - **Remove** — `uv run xtr-recipes recipes:remove xtr-web-profiler` drops the `BUNDLES` entry and
218
+ deletes `<app>/config/routes/dev/web_profiler.py`, unless you edited it, in which case it is moved
219
+ aside.
220
+ - **Check** — `debug:bundles` shows `web_profiler` as `listed` and `active` in `dev`; any HTML page
221
+ shows the toolbar; `GET /_profiler/latest` shows the last request's profile.
222
+
223
+ ## Configure
224
+
225
+ `WebProfilerConfig` is a frozen dataclass buildable with no arguments:
226
+
227
+ | Field | Default | What it sets |
228
+ |---|---|---|
229
+ | `toolbar` | `True` | draw the toolbar into HTML pages |
230
+ | `ajax_replace` | `False` | reload the toolbar with the profile a background request's response names |
231
+ | `intercept_redirects` | `False` | show a redirect answering a page as a page of its own |
232
+ | `excluded_ajax_paths` | `"^/(_wdt\|_profiler)"` | a regular expression; background requests to a matching path are not listed |
233
+ | `file_link_format` | `None` | how a file and line open in an editor — see [Editor links](#editor-links) |
234
+
235
+ The constructor refuses an `excluded_ajax_paths` that is no regular expression, and a
236
+ `file_link_format` that does not name `{file}`, with `InvalidArgumentError` — also a `ValueError`.
237
+
238
+ What is profiled — every request, only those asking with a parameter, only failures — is the http
239
+ kernel's [profiler configuration](../xtr-http-kernel#profiler).
240
+
241
+ ## Kernel / bundle
242
+
243
+ ```python
244
+ # app/config/bundles.py
245
+ from xtr_web_profiler.bundle import WebProfilerBundle
246
+
247
+ BUNDLES = {..., WebProfilerBundle: {"dev": True}}
248
+ ```
249
+
250
+ ```python
251
+ # app/config/packages/dev/web_profiler.py
252
+ from xtr_dependency_injection import configure
253
+
254
+ from xtr_web_profiler.bundle import WebProfilerConfig
255
+
256
+
257
+ @configure
258
+ def web_profiler() -> WebProfilerConfig:
259
+ return WebProfilerConfig(
260
+ intercept_redirects=True,
261
+ file_link_format="pycharm://open?file={file}&line={line}",
262
+ )
263
+ ```
264
+
265
+ The bundle requires the http kernel's bundle and turns its profiler on. It registers the
266
+ controller answering the pages, the template environment, the content security policy handler
267
+ and — when the toolbar or redirect interception is on — the toolbar middleware, tagged
268
+ `http_kernel.middleware` at priority `1024` (`WEB_DEBUG_TOOLBAR_PRIORITY`): outside the request
269
+ lifecycle, whose default is `0`, so it sees the profile's token on the response. An application
270
+ raising `HttpKernelConfig.middleware_priority` above it keeps the toolbar off its pages. It
271
+ contributes the route resources `web_profiler_wdt` and `web_profiler`. Turned off again through
272
+ the http kernel's configuration, the profiler records nothing and every page answers 404. The
273
+ zero-config path builds and boots with nothing configured and touches no disk until a request is
274
+ profiled.
275
+
276
+ ## Security
277
+
278
+ **Never list this bundle outside development.** A profile holds every request's headers,
279
+ cookies, parameters and state; anybody who can reach `/_profiler` can read them. The recipe lists
280
+ it for `dev` only and writes its routes into the `dev` folder, so in any other environment the
281
+ pages do not exist, and the bundle warns at build if it finds itself in `prod`. The token naming a
282
+ profile is short and not a secret: keeping the pages out of reach is what keeps the profiles
283
+ private.
284
+
285
+ ## Errors
286
+
287
+ Everything this library raises derives from `WebProfilerError`.
288
+
289
+ | Error | Raised when |
290
+ |---|---|
291
+ | `InvalidArgumentError` | a `WebProfilerConfig` field holds a value the toolbar would misread; also a `ValueError` |
292
+ | `TemplateNotFoundError` | a data collector's tag names a template that does not exist; also a `LookupError` |
293
+
294
+ ## Layout
295
+
296
+ ```
297
+ xtr_web_profiler/
298
+ ├── web_debug_toolbar_middleware.py draws the toolbar into the pages, links every profile
299
+ ├── controller/ the pages: toolbar, profile, search, source view
300
+ ├── routing/ the route resources web_profiler_wdt and web_profiler
301
+ ├── profiler/ which panels a profile has, and rendering their blocks
302
+ ├── templating/ the template environment, its loader and its filters
303
+ ├── csp/ the toolbar's nonces under a page's policy
304
+ ├── templates/ the pages, the toolbar and the core panels
305
+ ├── recipe/ what recipes:add applies
306
+ ├── exception/ WebProfilerError, the root of everything this library raises
307
+ └── bundle/ WebProfilerBundle and WebProfilerConfig
308
+ ```
309
+
310
+ ## Development
311
+
312
+ Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
313
+ `packages/xtr-web-profiler`; run the commands below from there.
314
+
315
+ ```sh
316
+ uv sync
317
+ uv run ruff check && uv run ruff format --check && uv run basedpyright && uv run ty check && uv run pytest
318
+ ```
319
+
320
+ ## License
321
+
322
+ MIT — see [LICENSE](LICENSE).
@@ -0,0 +1,298 @@
1
+ <div align="center">
2
+
3
+ # xtr-web-profiler
4
+
5
+ **A development toolbar on every page, and a profile of every request.**
6
+
7
+ <img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
8
+ <img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
9
+ <img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
10
+
11
+ </div>
12
+
13
+ ---
14
+
15
+ ## Why?
16
+
17
+ "What did that request actually do?" is the question asked most while building a web
18
+ application, and print statements answer it one piece at a time. In development, this package
19
+ answers it for every request at once: a toolbar at the foot of each HTML page shows the status,
20
+ the time, the memory, the log, the events and the route, and one click opens the request's full
21
+ profile — kept for two days and searchable.
22
+
23
+ - 🧭 **A toolbar on every page.** Drawn into each HTML response as it streams out; nothing to add
24
+ to a template.
25
+ - ⏱️ **A timeline.** Every event listener, the application's own handling and the whole request
26
+ on one axis, filterable by duration and by kind.
27
+ - 📜 **The request's log.** Every record it made — and only its own, however many requests the
28
+ process serves at once.
29
+ - 💥 **The exception, with its frames.** Source around each line, local variables, the chain of
30
+ causes; a link opens the line in your editor.
31
+ - 🔁 **Background requests too.** The toolbar lists the `fetch` and `XMLHttpRequest` calls the
32
+ page makes, each linked to its own profile.
33
+ - 🧩 **Panels from any library.** A data collector and a template are a new panel; the profiler
34
+ does not need to know it exists.
35
+
36
+ ## Install
37
+
38
+ ```sh
39
+ uv run xtr-recipes recipes:add --dev xtr-web-profiler
40
+ ```
41
+
42
+ Requires Python 3.14.4+. It installs as a development dependency: production does not install it,
43
+ and the recipe lists the bundle for the `dev` environment only.
44
+
45
+ ## Quick start
46
+
47
+ With [xtr-http-kernel](../xtr-http-kernel) already serving the application through
48
+ `setup(app, kernel)`, adding the package is the whole of it. Start the application in `dev`, open
49
+ any HTML page, and the toolbar is at its foot:
50
+
51
+ ```console
52
+ $ curl -si localhost:8000/books | grep -i debug
53
+ x-debug-token: 3f9a1c
54
+ x-debug-token-link: http://localhost:8000/_profiler/3f9a1c
55
+ ```
56
+
57
+ A response that is not a page — JSON from an API — carries the same two headers: follow
58
+ `X-Debug-Token-Link` to its profile, or open `/_profiler/latest`.
59
+
60
+ ## What a profile shows
61
+
62
+ | Panel | Shows |
63
+ |---|---|
64
+ | Request | the method, URI, query, parsed body, path parameters, cookies, headers and state of the request; the status and headers of the response; a `curl` command replaying it. Credentials — `Authorization`, `Cookie`, `Set-Cookie`, and every field named like a password, a secret or a token — are kept as `******` |
65
+ | Time | the whole request's duration and a timeline of every timed event: the request's section, the application's own handling (`controller`), each event listener |
66
+ | Memory | the peak memory of the process serving the request |
67
+ | AJAX | the background requests the page made, listed in its toolbar |
68
+ | Exception | what handling raised: class, message, chain of causes, and each frame with its source and local variables |
69
+ | Logs | every record the request logged, by level, with its context — when [xtr-logging](../xtr-logging) is active. Context entries named like credentials are kept as `******` too |
70
+ | Events | the listeners called, how often and for how long, those not called, and the events nobody listened to |
71
+ | Routing | the route that answered — name, path template, methods, endpoint, parameters — a redirect's target, and how every route of the application matched the request |
72
+ | Config | the Python version, the environment, the active bundles and every xtr package installed |
73
+
74
+ The profiles themselves are collected by [xtr-http-kernel](../xtr-http-kernel#profiler); this
75
+ package shows them.
76
+
77
+ ## The toolbar
78
+
79
+ The toolbar is drawn into a response only when all of these hold: the toolbar is on, the response
80
+ names a profile in `X-Debug-Token`, it is no redirect, its content type is HTML, it is no
81
+ attachment, its body is not compressed, and the request was not a background one —
82
+ `X-Requested-With: XMLHttpRequest`, or a fetch the browser marks as not loading a document. It
83
+ goes in just before the closing `</body>`, found even when a streamed body splits the tag across
84
+ two chunks, and `Content-Length` is dropped since the body grew. A page without `</body>` goes out
85
+ as it was.
86
+
87
+ What goes into the page is small: a placeholder and a script. The script fetches the toolbar from
88
+ `/_wdt/<token>` — retrying a moment later when the profile has not been written yet, since it is
89
+ written once the response has left — and remembers whether you minimised it.
90
+
91
+ A compressed body cannot be drawn into, and a browser always accepts one. So a request that may get
92
+ the toolbar — one accepting HTML, or anything, that is not a background request — reaches the
93
+ application without its `Accept-Encoding`: an application compressing its responses, with the
94
+ framework's `GZipMiddleware` say, leaves its pages uncompressed in development, and its other
95
+ responses compressed.
96
+
97
+ ### Background requests
98
+
99
+ The page's `fetch` and `XMLHttpRequest` calls are recorded as they go, with their status, their
100
+ duration and the profile named by their response, and listed in the toolbar. Calls to the
101
+ profiler's own paths are left out — `excluded_ajax_paths` says which. With `ajax_replace`, a
102
+ background request whose response names a profile reloads the toolbar with that profile — for a
103
+ page that changes what it shows without loading another.
104
+
105
+ ### Redirects
106
+
107
+ With `intercept_redirects`, a redirect answering a page is replaced by a page saying where it
108
+ leads, toolbar included, so the request that redirected — a form post, a sign-in — can be read
109
+ before following it.
110
+
111
+ ### Content security policy
112
+
113
+ The toolbar is an inline script and an inline style. A page sending a policy gets the toolbar's
114
+ nonces added to every script and style directive of it, so the toolbar runs and the page's own
115
+ rules stay as they were; a page without a policy is left alone. The profiler's own pages are sent
116
+ with no policy at all.
117
+
118
+ ## Editor links
119
+
120
+ A frame of an exception links to the line that ran. By default the link opens the profiler's own
121
+ source view, which shows only files under the project directory and never a hidden one. Point it at
122
+ your editor instead with `file_link_format`, `{file}` and `{line}` standing for them:
123
+
124
+ | Editor | `file_link_format` |
125
+ |---|---|
126
+ | PyCharm | `"pycharm://open?file={file}&line={line}"` |
127
+ | VS Code | `"vscode://file/{file}:{line}"` |
128
+ | Cursor | `"cursor://file/{file}:{line}"` |
129
+
130
+ ## Writing a panel
131
+
132
+ A panel is a data collector — registered by any bundle, see
133
+ [xtr-http-kernel](../xtr-http-kernel#writing-a-collector) — and a template naming up to four
134
+ blocks:
135
+
136
+ - `toolbar` — the item in the toolbar: call `toolbar_item(name, icon, text, link, status)` with
137
+ the icon and value shown and the text of its popover;
138
+ - `menu` — the entry in the profile page's menu;
139
+ - `panel` — the panel itself;
140
+ - `head` — anything the panel needs in the page's head.
141
+
142
+ The template is a file of the collector's own package, under `templates/`, named
143
+ `"@<import package>/<path>"`; the package needs no template engine of its own:
144
+
145
+ ```jinja
146
+ {# src/xtr_orm/templates/collector/orm.html.j2 #}
147
+ {% block toolbar %}
148
+ {% from "@xtr_web_profiler/profiler/toolbar_item.html.j2" import toolbar_item %}
149
+ {% set icon %}<span class="xtr-toolbar-value">{{ collector.data.count }} queries</span>{% endset %}
150
+ {% set text %}<b>Time</b> {{ collector.data.duration_ms | format_ms }}{% endset %}
151
+ {{ toolbar_item(collector.name, icon, text, link=panel_url(collector.name)) }}
152
+ {% endblock %}
153
+ {% block menu %}<span class="xtr-label">Database</span>{% endblock %}
154
+ {% block panel %}
155
+ {% for query in collector.data.queries %}<pre>{{ query.sql }}</pre>{% endfor %}
156
+ {% endblock %}
157
+ ```
158
+
159
+ Every block sees `collector` — `collector.name`, and `collector.data`, what the collector kept —
160
+ `profile`, `token`, `url(route_name, **parameters)`, `panel_url(panel)`,
161
+ `open_file_url(file, line)`, and the CSP nonces `csp_script_nonce` and `csp_style_nonce` for an
162
+ inline script or style. Filters: `profiler_dump` shows a value kept through `clone_var` as a
163
+ browsable tree; `format_bytes`, `format_ms`, `format_time` and `file_link(file, line)`. A block
164
+ naming anything else fails, rather than rendering empty.
165
+
166
+ ## Use in an application
167
+
168
+ Everything adding this package to an application on
169
+ [xtr-dependency-injection](../xtr-dependency-injection) takes — and, read backwards, what
170
+ removing it undoes.
171
+
172
+ - **Install** — `uv run xtr-recipes recipes:add --dev xtr-web-profiler`: a development dependency,
173
+ never installed in production. [xtr-http-kernel](../xtr-http-kernel) comes with it; install
174
+ [xtr-logging](../xtr-logging) beside it for the log panel. The timeline needs nothing more:
175
+ [xtr-stopwatch](../xtr-stopwatch) comes with the http kernel.
176
+ - **Recipe** — `recipes:add` does the *Activate* and *Configure* steps below: it lists
177
+ `WebProfilerBundle` for `dev` and writes `<app>/config/routes/dev/web_profiler.py`. There is no
178
+ environment or ignore line to write; it prints the `setup(app, kernel)` step, which a recipe
179
+ cannot make for you.
180
+ - **Activate** — `WebProfilerBundle: {"dev": True}` in `BUNDLES` in `<app>/config/bundles.py`,
181
+ imported from `xtr_web_profiler.bundle`, and `setup(app, kernel)` where the application is
182
+ built, as the http kernel requires.
183
+ - **Brings along** — the [http kernel](../xtr-http-kernel)'s bundle, required outright, and with it
184
+ the event dispatcher's. Listing this bundle turns the http kernel's profiler on in the same
185
+ environment.
186
+ - **Configure** — `<app>/config/routes/dev/web_profiler.py`, which the recipe writes, serves the
187
+ toolbar under `/_wdt` and the pages under `/_profiler`; it sits in the `dev` folder, so nothing
188
+ of the profiler is routed elsewhere, and deleting it takes the pages down. The bundle's own
189
+ configuration needs nothing; a change goes in `<app>/config/packages/dev/web_profiler.py` — see
190
+ [Configure](#configure).
191
+ - **Environment** — nothing.
192
+ - **Ignore** — nothing: the profiles are kept under the kernel's cache directory.
193
+ - **Remove** — `uv run xtr-recipes recipes:remove xtr-web-profiler` drops the `BUNDLES` entry and
194
+ deletes `<app>/config/routes/dev/web_profiler.py`, unless you edited it, in which case it is moved
195
+ aside.
196
+ - **Check** — `debug:bundles` shows `web_profiler` as `listed` and `active` in `dev`; any HTML page
197
+ shows the toolbar; `GET /_profiler/latest` shows the last request's profile.
198
+
199
+ ## Configure
200
+
201
+ `WebProfilerConfig` is a frozen dataclass buildable with no arguments:
202
+
203
+ | Field | Default | What it sets |
204
+ |---|---|---|
205
+ | `toolbar` | `True` | draw the toolbar into HTML pages |
206
+ | `ajax_replace` | `False` | reload the toolbar with the profile a background request's response names |
207
+ | `intercept_redirects` | `False` | show a redirect answering a page as a page of its own |
208
+ | `excluded_ajax_paths` | `"^/(_wdt\|_profiler)"` | a regular expression; background requests to a matching path are not listed |
209
+ | `file_link_format` | `None` | how a file and line open in an editor — see [Editor links](#editor-links) |
210
+
211
+ The constructor refuses an `excluded_ajax_paths` that is no regular expression, and a
212
+ `file_link_format` that does not name `{file}`, with `InvalidArgumentError` — also a `ValueError`.
213
+
214
+ What is profiled — every request, only those asking with a parameter, only failures — is the http
215
+ kernel's [profiler configuration](../xtr-http-kernel#profiler).
216
+
217
+ ## Kernel / bundle
218
+
219
+ ```python
220
+ # app/config/bundles.py
221
+ from xtr_web_profiler.bundle import WebProfilerBundle
222
+
223
+ BUNDLES = {..., WebProfilerBundle: {"dev": True}}
224
+ ```
225
+
226
+ ```python
227
+ # app/config/packages/dev/web_profiler.py
228
+ from xtr_dependency_injection import configure
229
+
230
+ from xtr_web_profiler.bundle import WebProfilerConfig
231
+
232
+
233
+ @configure
234
+ def web_profiler() -> WebProfilerConfig:
235
+ return WebProfilerConfig(
236
+ intercept_redirects=True,
237
+ file_link_format="pycharm://open?file={file}&line={line}",
238
+ )
239
+ ```
240
+
241
+ The bundle requires the http kernel's bundle and turns its profiler on. It registers the
242
+ controller answering the pages, the template environment, the content security policy handler
243
+ and — when the toolbar or redirect interception is on — the toolbar middleware, tagged
244
+ `http_kernel.middleware` at priority `1024` (`WEB_DEBUG_TOOLBAR_PRIORITY`): outside the request
245
+ lifecycle, whose default is `0`, so it sees the profile's token on the response. An application
246
+ raising `HttpKernelConfig.middleware_priority` above it keeps the toolbar off its pages. It
247
+ contributes the route resources `web_profiler_wdt` and `web_profiler`. Turned off again through
248
+ the http kernel's configuration, the profiler records nothing and every page answers 404. The
249
+ zero-config path builds and boots with nothing configured and touches no disk until a request is
250
+ profiled.
251
+
252
+ ## Security
253
+
254
+ **Never list this bundle outside development.** A profile holds every request's headers,
255
+ cookies, parameters and state; anybody who can reach `/_profiler` can read them. The recipe lists
256
+ it for `dev` only and writes its routes into the `dev` folder, so in any other environment the
257
+ pages do not exist, and the bundle warns at build if it finds itself in `prod`. The token naming a
258
+ profile is short and not a secret: keeping the pages out of reach is what keeps the profiles
259
+ private.
260
+
261
+ ## Errors
262
+
263
+ Everything this library raises derives from `WebProfilerError`.
264
+
265
+ | Error | Raised when |
266
+ |---|---|
267
+ | `InvalidArgumentError` | a `WebProfilerConfig` field holds a value the toolbar would misread; also a `ValueError` |
268
+ | `TemplateNotFoundError` | a data collector's tag names a template that does not exist; also a `LookupError` |
269
+
270
+ ## Layout
271
+
272
+ ```
273
+ xtr_web_profiler/
274
+ ├── web_debug_toolbar_middleware.py draws the toolbar into the pages, links every profile
275
+ ├── controller/ the pages: toolbar, profile, search, source view
276
+ ├── routing/ the route resources web_profiler_wdt and web_profiler
277
+ ├── profiler/ which panels a profile has, and rendering their blocks
278
+ ├── templating/ the template environment, its loader and its filters
279
+ ├── csp/ the toolbar's nonces under a page's policy
280
+ ├── templates/ the pages, the toolbar and the core panels
281
+ ├── recipe/ what recipes:add applies
282
+ ├── exception/ WebProfilerError, the root of everything this library raises
283
+ └── bundle/ WebProfilerBundle and WebProfilerConfig
284
+ ```
285
+
286
+ ## Development
287
+
288
+ Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
289
+ `packages/xtr-web-profiler`; run the commands below from there.
290
+
291
+ ```sh
292
+ uv sync
293
+ uv run ruff check && uv run ruff format --check && uv run basedpyright && uv run ty check && uv run pytest
294
+ ```
295
+
296
+ ## License
297
+
298
+ MIT — see [LICENSE](LICENSE).