Skip to content

Commit 0baef8c

Browse files
committed
Add architecture documentation for the project.
This starts with high level information about the project. In the future we can go into more detail on each of the components.
1 parent eb0b6ea commit 0baef8c

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

docs/architecture.rst

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Architecture
2+
============
3+
4+
The Django Debug Toolbar is designed to be flexible and extensible for
5+
developers and third-party panel creators.
6+
7+
Core Components
8+
---------------
9+
10+
While there are several components, the majority of logic and complexity
11+
lives within the following:
12+
13+
- ``debug_toolbar.middleware.DebugToolbarMiddleware``
14+
- ``debug_toolbar.toolbar.DebugToolbar``
15+
- ``debug_toolbar.panels``
16+
17+
^^^^^^^^^^^^^^^^^^^^^^
18+
DebugToolbarMiddleware
19+
^^^^^^^^^^^^^^^^^^^^^^
20+
21+
The middleware is how the toolbar integrates with Django projects.
22+
It determines if the toolbar should instrument the request, which
23+
panels to use, facilitates the processing of the request and augmenting
24+
the response with the toolbar. Most logic for how the toolbar interacts
25+
with the user's Django project belongs here.
26+
27+
^^^^^^^^^^^^
28+
DebugToolbar
29+
^^^^^^^^^^^^
30+
31+
The ``DebugToolbar`` class orchestrates the processing of a request
32+
for each of the panels. It contains the logic that needs to be aware
33+
of all the panels, but doesn't need to interact with the user's Django
34+
project.
35+
36+
^^^^^^
37+
Panels
38+
^^^^^^
39+
40+
The majority of the complex logic lives within the panels themselves. This
41+
is because the panels are responsible for collecting the various metrics.
42+
Some of the metrics are collected via
43+
`monkey-patching <https://stackoverflow.com/a/5626250>`_, such as
44+
``TemplatesPanel``. Others, such as ``SettingsPanel`` don't need to collect
45+
anything and include the data directly in the response.
46+
47+
Some panels such as ``SQLPanel`` have additional functionality. This tends
48+
to involve a user clicking on something, and the toolbar presenting a new
49+
page with additional data. That additional data is handled in views defined
50+
in the panels package (for example, ``debug_toolbar.panels.sql.views``).
51+
52+
Logic Flow
53+
----------
54+
55+
When a request comes in, the toolbar first interacts with it in the
56+
middleware. If the middleware determines the request should be instrumented,
57+
it will instantiate the toolbar and pass the request for processing. The
58+
toolbar will use the enabled panels to collect information on the request
59+
and/or response. When the toolbar has completed collecting its metrics on
60+
both the request and response, the middleware will collect the results
61+
from the toolbar. It will inject the HTML and JavaScript to render the
62+
toolbar as well as any headers into the response.
63+
64+
After the browser renders the panel and the user interacts with it, the
65+
toolbar's JavaScript will send requests to the server. If the view handling
66+
the request needs to fetch data from the toolbar, the request must supply
67+
the store ID. This is so that the toolbar can load the collected metrics
68+
for that particular request.
69+
70+
The history panel allows a user to view the metrics for any request since
71+
the application was started. The toolbar maintains its state entirely in
72+
memory for the process running ``runserver``. If the application is
73+
restarted the toolbar will lose its state.
74+
75+
Problematic Parts
76+
-----------------
77+
78+
- ``debug.panels.templates.panel``: This monkey-patches template rendering
79+
when the panel module is loaded
80+
- ``debug.panels.sql``: This package is particularly complex, but provides
81+
the main benefit of the toolbar
82+
- Support for async and multi-threading: This is currently unsupported, but
83+
is being implemented as per the
84+
`Async compatible toolbar project <https://github.com/orgs/jazzband/projects/9>`_.

docs/changes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Pending
1111
return invalid json.
1212
* Render forms with ``as_div`` to silence Django 5.0 deprecation warnings.
1313
* Stayed on top of pre-commit hook updates.
14+
* Added :doc:`architecture documentation <architecture>` to help
15+
on-board new contributors.
1416

1517
4.3.0 (2024-02-01)
1618
------------------

docs/contributing.rst

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ For convenience, there's an alias for the second command::
4848
Look at ``example/settings.py`` for running the example with another database
4949
than SQLite.
5050

51+
Architecture
52+
------------
53+
54+
There is high-level information on how the Django Debug Toolbar is structured
55+
in the :doc:`architecture documentation <architecture>`.
56+
5157
Tests
5258
-----
5359

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Django Debug Toolbar
1212
commands
1313
changes
1414
contributing
15+
architecture

0 commit comments

Comments
 (0)