Monitor a Django app with Scout
In this post, I'll show how to
Wagtail is a fast, modern opensource content management system built on Django. Used at NASA, Google, MIT, and more, it's a great option for running your own CMS. When we add scout-apm
I'll start with the performance monitoring basics, then move onto advanced settings we've found valuable for monitoring our own apps at Scout.
Setup
Scout is distributed via the scout-apm
Python package and configuration takes just a couple of lines of code. Once configured, Scout auto-instruments Django apps, tracking the performance of individual web requests, SQL queries, and more.
If you don't have a Scout account, signup for a 14-day trial. After signup, follow our Django install instructions.
Wagtail uses environment-specific setting files. I'd suggest the following approach to configure Scout:
bakerydemo /settings/base.py
Add 'scout_apm.django'
to the top of the listof INSTALLED_APPS
. - Set a couple of cross-environment defaults (enabling monitoring and setting the application name):
# Scout settings SCOUT_MONITOR = True SCOUT_NAME = WAGTAIL_SITE_NAME SCOUT_KEY = "SCOUT_KEY" # Grab your key from the Scout UI
dev and prod environments
Scout aggregates metrics by the application name. It's useful to separate your apps by environment as your dev environment often behaves very differently than production.
I set unique names for dev
and production
environments.
In bakerydemo/settings/dev.py
:
SCOUT_NAME = SCOUT_NAME + " [DEV]"
In bakerydemo/settings/production.py
:
SCOUT_NAME = SCOUT_NAME + " [PROD]"
Loading the SCOUT_KEY from environment variables
If you prefer to keep your API keys out of version control, you can set SCOUT_KEY
SCOUT_KEY
base.py
Restart your app.
That's it! Just python manage.py runserver
Overview
After restarting your app, you'll see an overview of your app's performance within Scout:
Scout's basic installation automatically tracks SQL, view, and rendering performance.
You can also explore the performance of specific views via the "Web Endpoints" area of Scout:
...and dig into the performance of a specific view as well:
GitHub Integration
Scout can quickly connect a slow method call to the code behind it via our GitHub integration. To enable the GitHub integration, access "GitHub" under "Integrations" in the app settings:
Authorize Scout via GitHub's OAuth screen. Once
Deploy Tracking
Changes to our app can trigger performance issues. After configuring deploy tracking, it's easy to correlate these changes to performance. When combined with the GitHub integration, we can also view a diff of changes between deploys.
I'm using Heroku to deploy my Wagtail app, so all I need to do is enable dyno metadata:
heroku labs:enable runtime-dyno-metadata
You'll see deploy markers on charts going forward:
Scout's GitHub integration enriches deploy tracking, providing a diff summary of changes when you mouseover a deploy marker or the Git SHA next to your app name.
Error Monitoring
Scout doesn't provide detailed error monitoring by itself, but it easily integrates with exception monitoring services like Rollbar. You can enable the Rollbar integration in your app settings:
After adding the integration, you'll see application errors right on your Scout dashboard, giving you a
Alerting
Let's have Scout notify us via email if our app performance degrades. Configure an alert on "95th percentile response time":
This alert will be emailed to users of our choice.
Context & Trace Explorer
Many performance issues aren't experienced by everyone: they are triggered in specific situations. For example, a power user accessing a lot of data is more likely to see slower response times. Sometimes this performance profile may be hidden in overview metrics.
Scout has a context API: this lets you add metadata about your requests. For example, Scout automatically tracks the current user in the session. I'll add context with the search query term used and the number of search results found on Wagtail's search:
...then, in Scout's Trace Explorer, I can filter traces in realtime, accessing traces for specific search queries:
Custom Instrumentation
Last but not least, Scout instruments many popular Python libraries, but not your custom code. For this, you can add custom instrumentation.
Let's add some custom instrumentation around the search results pagination:
You'll see time spent in this category on your overview charts and within traces:
TL;DR
Scout makes monitoring the performance of Django apps affordable and approachable in just a couple of lines of code. In this walkthrough, I've added Scout to Wagtail, an opensource Django CMS. You can find my modifications to the Wagtail
Try Scout for Django monitoring today with our 14-day, credit card-less trial.