Skip to content

Commit c8b4c0b

Browse files
committed
Implement MQL panel for Django Debug Toolbar
1 parent e93b47c commit c8b4c0b

File tree

18 files changed

+2271
-69
lines changed

18 files changed

+2271
-69
lines changed

README.md

Lines changed: 215 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,216 @@
1-
# django-mongodb-extensions
1+
# Django MongoDB Extensions
22

3-
Extensions for Django MongoDB Backend
3+
A collection of extensions for Django projects using
4+
[django-mongodb-backend](https://github.com/mongodb-labs/django-mongodb-backend).
5+
Inspired by
6+
[django-extensions](https://github.com/django-extensions/django-extensions),
7+
this package provides MongoDB-specific tools to enhance your development
8+
workflow.
9+
10+
## Installation
11+
12+
```bash
13+
pip install django-mongodb-extensions
14+
```
15+
16+
Add to `INSTALLED_APPS`:
17+
18+
```python
19+
INSTALLED_APPS = [
20+
...
21+
'django_mongodb_extensions',
22+
...
23+
]
24+
```
25+
26+
## Features
27+
28+
### MQL Panel for Django Debug Toolbar
29+
30+
The MQL (MongoDB Query Language) Panel provides detailed insights into MongoDB queries executed during a request, similar to the SQL panel for relational databases.
31+
32+
#### Setup
33+
34+
Add to Django Debug Toolbar panels:
35+
36+
```python
37+
DEBUG_TOOLBAR_PANELS = [
38+
...
39+
'django_mongodb_extensions.debug_toolbar.panels.MQLPanel',
40+
...
41+
]
42+
```
43+
44+
#### Features
45+
46+
- **Query Tracking**: Automatically tracks all MongoDB operations (find, aggregate, count, etc.)
47+
- **Query Timing**: Shows execution time for each query with visual timeline
48+
- **Query Grouping**: Groups similar and duplicate queries for easy identification
49+
- **Interactive Execution**: Re-execute read queries and view results directly in the panel
50+
- **Explain Plans**: View MongoDB explain output for queries
51+
- **BSON Support**: Proper handling of MongoDB-specific types (ObjectId, datetime, etc.)
52+
- **Thread-Safe**: Works correctly in multi-threaded environments
53+
54+
#### Configuration
55+
56+
You can customize the MQL panel behavior with these Django settings:
57+
58+
```python
59+
# Maximum number of documents to return when re-executing queries (default: 100)
60+
DJDT_MQL_MAX_SELECT_RESULTS = 100
61+
```
62+
63+
#### Usage
64+
65+
Once configured, the MQL panel will automatically appear in the Django Debug Toolbar when MongoDB queries are executed. You can:
66+
67+
1. **View Queries**: See all MongoDB operations with timing and parameters
68+
2. **Re-execute Queries**: Click "Sel" to re-execute read operations and view results
69+
3. **Explain Queries**: Click "Expl" to view MongoDB's query execution plan
70+
4. **Identify Issues**: Slow queries are highlighted, and duplicate queries are grouped
71+
72+
## Performance Considerations
73+
74+
### Query Result Limits
75+
76+
When re-executing queries through the debug toolbar, results are limited to prevent memory issues:
77+
- Default limit: 100 documents
78+
- Configurable via `DJDT_MQL_MAX_SELECT_RESULTS`
79+
- Cursors are properly closed to free server resources
80+
81+
### Memory Management
82+
83+
The MQL panel uses `WeakSet` for tracking patched connections, ensuring that closed connections are garbage collected and don't cause memory leaks.
84+
85+
### Thread Safety
86+
87+
The panel is designed to work correctly in multi-threaded environments:
88+
- Connection patching is idempotent
89+
- Each request gets its own logger instance
90+
- Thread-local connection storage prevents cross-request contamination
91+
92+
## Troubleshooting
93+
94+
### Django Debug Toolbar Not Installed
95+
96+
Before the MQL panel can work, you must have Django Debug Toolbar properly installed and configured. Follow the [official installation instructions](https://django-debug-toolbar.readthedocs.io/en/latest/installation.html):
97+
98+
1. **Install the package**:
99+
```bash
100+
pip install django-debug-toolbar
101+
```
102+
103+
2. **Add to `INSTALLED_APPS`**:
104+
```python
105+
INSTALLED_APPS = [
106+
# ...
107+
"debug_toolbar",
108+
# ...
109+
]
110+
```
111+
112+
3. **Add the URLs**:
113+
```python
114+
from django.urls import include, path
115+
from debug_toolbar.toolbar import debug_toolbar_urls
116+
117+
urlpatterns = [
118+
# ... your URLconf ...
119+
] + debug_toolbar_urls()
120+
```
121+
122+
4. **Add the middleware**:
123+
```python
124+
MIDDLEWARE = [
125+
# ...
126+
"debug_toolbar.middleware.DebugToolbarMiddleware",
127+
# ...
128+
]
129+
```
130+
131+
5. **Configure `INTERNAL_IPS`**:
132+
```python
133+
INTERNAL_IPS = [
134+
"127.0.0.1",
135+
]
136+
```
137+
138+
For complete setup instructions, including Docker configurations and troubleshooting, see the [Django Debug Toolbar documentation](https://django-debug-toolbar.readthedocs.io/en/latest/installation.html).
139+
140+
### Panel Not Showing
141+
142+
If the MQL panel doesn't appear:
143+
144+
1. Verify Django Debug Toolbar is installed and working (see above)
145+
2. Verify `django_mongodb_extensions` is in `INSTALLED_APPS`
146+
3. Check that `MQLPanel` is in `DEBUG_TOOLBAR_PANELS`
147+
4. Ensure you're using `django-mongodb-backend` as your database engine
148+
5. Verify MongoDB queries are actually being executed in your view
149+
150+
### Queries Not Tracked
151+
152+
If queries aren't being tracked:
153+
154+
1. Ensure Debug Toolbar is enabled (`DEBUG = True`)
155+
2. Check that the toolbar middleware is installed
156+
3. Verify your IP is in `INTERNAL_IPS`
157+
4. Check that queries are executed during the request (not in background tasks)
158+
159+
### Re-execution Errors
160+
161+
If you get errors when re-executing queries:
162+
163+
- **"Query does not have structured data"**: The query was logged before security improvements. Only newly logged queries can be re-executed.
164+
- **"Unsupported operation"**: Write operations (insert, update, delete) cannot be re-executed for safety.
165+
- **Connection errors**: Check your MongoDB connection settings and ensure the server is accessible.
166+
167+
## Security
168+
169+
The MQL panel implements several security measures:
170+
171+
- **Signed Forms**: All query re-execution requests use Django's signed data to prevent tampering
172+
- **Structured Data Only**: Only queries with validated structured data can be re-executed
173+
- **Read-Only Re-execution**: Write operations cannot be re-executed through the panel
174+
- **Server-Side Validation**: All query data is validated before execution
175+
176+
## Development
177+
178+
### Running Tests
179+
180+
```bash
181+
# Install development dependencies
182+
pip install -e ".[dev]"
183+
184+
# Run tests
185+
pytest
186+
187+
# Run tests with coverage
188+
pytest --cov=django_mongodb_extensions --cov-report=html
189+
```
190+
191+
### Code Quality
192+
193+
```bash
194+
# Format code
195+
ruff format .
196+
197+
# Lint code
198+
ruff check .
199+
200+
# Type checking (if using mypy)
201+
mypy django_mongodb_extensions
202+
```
203+
204+
## Contributing
205+
206+
Contributions are welcome! Please:
207+
208+
1. Fork the repository
209+
2. Create a feature branch
210+
3. Add tests for new functionality
211+
4. Ensure all tests pass
212+
5. Submit a pull request
213+
214+
## License
215+
216+
See [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)