Monitorize and debug your Django app like a pro with Django-Sonar
DjangoSonar is a comprehensive debugging and introspection tool for Django applications, inspired by Laravel Telescope.
I’ve never user Laravel, so I didn’t know about his Telescope debug tool, but the Django equivalent Django-Sonar is AWESOME, so Larael Telescope must be awesome too.
Let’s dig into Django-Sonar and integrate it in one of our Django Apps.
What is Django-Sonar?
In its Github page, Django-Sonar describes itself as a comprehensive debugging and introspection tool for Django applications. Pretty self explanatory :)
Django-Sonar captures and historizes realtime events like:
Requests
Exceptions
Queries
Dumps
Authentications
Etc.
Provides a web interface to display those captured events, for later analysis. In its traces we can get useful info about:
Querys performed by the ORM
Payload of get/post requests
Session vars
Headers,
Etc
Those events can be also custom events, making Django-Sonar the central debug tool for our app. Very, very useful.
Installing Django-Sonar
Is very, very easy to install Django-Sonar. First, add the required dependency:
pip install django-sonar
Then, add the new app to Django’s INSTALLED_APPS, in the main settings.py
:
INSTALLED_APPS = [
...
'django_sonar',
...
]
Then, add the URL mapping to the main urls.py
file:
urlpatterns = [
...
path('sonar/', include('django_sonar.urls')),
...
]
And add Django-Sonar to the middleware to enable data capturing:
MIDDLEWARE = [
...
'django_sonar.middlewares.requests.RequestsMiddleware',
...
]
Django-Sonar will capture ALL events from our app, so is a good practive to filter some internal events to avoid excesive noise:
DJANGO_SONAR = {
'excludes': [
STATIC_URL,
MEDIA_URL,
'/sonar/',
'/admin/',
'/__reload__/',
],
}
Now all is configured. Execute the migration to create the required tables:
python manage.py migrate
Django-Sonar user interface
You will need an internal admin user to enter in Sonar’s UI. Creae it now if yoy don’t have one yet:
python manage.py createsuperuser
Then, go to the URL you configured un urls.py
for Sonar:
Enter the user and password to see the realtime captured data. For this example, we have removered the filters, so ALL accesses (even the internal ones) are beign captured :)
We can see each request, ad display it’s content:
Read every performed query:
And get the details of them:
And, one ot the more interesting thing, we can write something like this in our views:
from django.shortcuts import render
from django_sonar.utils import sonar
def homeView(request):
sonar('something from a view', request.GET, [1,2,3])
return render(request, 'home.html')
To get the traces in Sonar:
Very, very interesting. Django-Sonar has become a must for all my Django-Projects.
About the list
Among the Python and Docker posts, I will also write about other related topics (always tech and programming topics, I promise... with the fingers crossed), like:
Software architecture
Programming environments
Linux operating system
Etc.
If you found some interesting technology, programming language or whatever, please, let me know! I'm always open to learning something new!
About the author
I'm Andrés, a full-stack software developer based in Palma, on a personal journey to improve my coding skills. I'm also a self-published fantasy writer with four published novels to my name. Feel free to ask me anything!