How to: Trace Django Requests¶
Trace a Django request for inspection, visualization, and debugging purposes.
Not using Django or just want to trace a function? Check out How to: Trace a function or script
Trace a Django request¶
The best way to try out Kolo is by using it on a Django codebase that you work on. For the purposes of this guide, we’ll be demonstrating what Kolo looks like when used with our Todo Demo app.
Install kolo using
pip install kolo
Add
"kolo.middleware.KoloMiddleware"
to the top of your MIDDLEWARE list in settings.py
Kolo will automatically disable itself whenever the Django DEBUG
setting is set to False
(as should be the case in production). But if you’d like more control over when the KoloMiddleware is enabled, you can also dynamically add it to your MIDDLEWARE
list:
# settings.py
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
...
]
if DEBUG:
MIDDLEWARE.insert(0, 'kolo.middleware.KoloMiddleware')
Start your Django server using
python manage.py runserver
You will see a message confirming that Kolo is active:
➜ todo-demo git:(main) ✗ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
View recent requests at http://127.0.0.1:8000/_kolo/
Make a request to any page.
In our todo demo app, this would be localhost:8000/add/
Browse to
localhost:8000/_kolo/
You’ll see all recently traced requests
Click on the request that is of interest.
You’ll now be able to fully inspect, visualize, and debug the request with Kolo.
We can immediately see a number of things here:
The function responsible for handling the request:
core.views.add_todo
A number of SQL queries were made during the request (purple color)
An external API request was made which made up for most of the request duration (blue color in the flame graph on the bottom left)