Monitoring Django Performance with Scout APM: A Step-by-Step Guide

Django is one of the most popular web frameworks for building applications. Its elegance and flexibility make it a favorite among developers, enabling them to craft intricate applications with ease. However, as applications grow in complexity and user traffic grows, the need for active performance monitoring becomes imperative. This is where Application Performance Monitoring (APM) tools step in, offering invaluable insights into an application's behavior and aiding in pinpointing bottlenecks, slowdowns, and other performance-related issues.

In the world of APM tools tailored for Django, Scout APM stands out as a remarkable solution. Scout APM is designed to empower developers with the ability to easily monitor the performance of their Django applications. Its user-friendly interface, coupled with its robust set of features such as custom transaction monitoring, error monitoring, and GitHub integration to tie errors to source code, allows developers to gain real-time visibility into the inner workings of their applications. This, in turn, enables them to proactively identify and address performance bottlenecks, thereby ensuring a seamless user experience and efficient resource utilization.

In this article, you will learn how to set up Scout APM in a sample Django article. You will then see how to explore the Scout APM interface to gain insights on the performance of your app, such as response time, throughput, memory consumption, and more.

Setting Up Scout APM in Your Django App

Setting up Scout APM in a Django App is quite straightforward. All you need to do is:

That’s all you need to do to get started with Scout APM! You can try it out for yourself with a Django app. This GitHub repository contains a simple Django calculator app that looks like this when run:

It takes in a mathematical expression and prints its result on the screen. To keep things simple for this tutorial, no database or other external services are added to the project. You can fork the repo to your profile and deploy it to any deployment provider, such as AWS or Railway. This tutorial will make use of Railway to deploy the Django app and set up Scout APM in it.

Start by forking this repo to your GitHub account. This repo contains the code for the sample Django app discussed above.

Next, clone this repo to your local system so that you can set up Scout APM in it.

Once you’ve cloned the repo, add the following line to its requirements.txt file:


Next, open the project/ file and add 'scout_apm.django' to the list of INSTALLED_APPS. This is what your INSTALLED_APPS array should look like now:


Finally, commit and push these changes to your GitHub repo so that you can deploy them.

To deploy the app to Railway, head over to the Railway homepage and sign up for a free trial. Once you have created a new account, click on the New Project button.

On the next screen, click on Deploy from GitHub repo and configure your GitHub account to access repos for deploying to Railway. Once configured, you will be able to see all your GitHub repos in the Deploy from GitHub repo list:

Choose the Django-calculator app from this list and click on the Add Variables button to add environment variables before deploying the app. As instructed earlier, you will need to provide the SCOUT_KEY, SCOUT_MONITOR, and SCOUT_NAME variables to set up Scout APM in your deployment. 

This will start the deployment while asking you to add environment variables. You now need to head over to the Scout APM app and create a new account with a free trial

Once you have created your Scout APM account, click on the Applications > Add Application option from the top navigation bar in the top left (or go to this link). On this page, choose Python as your app’s language. You will now see a page asking you to install the Scout APM package in your project based on your framework, and you will also find a value for your SCOUT_KEY

Go back to your Railway environment variables page and create a new variable with the name SCOUT_KEY and the value from the Scout new app page. Also, create another variable with the name SCOUT_NAME and set its value as "Django Calculator". This is the name with which your app will be referenced in the Scout APM dashboard. Finally, add a third environment variable SCOUT_MONITOR with the value set as "True" to enable Scout to monitor your app’s performance.

Now you can trigger another deployment on the Railway page and wait for Scout to start collecting data from your app. Here’s what the Scout APM page would look like when the installation is successful:

You are now ready to explore your app’s performance in the Scout APM dashboard. You might need to wait for 5-10 minutes for Scout to collect some data to show in the dashboard.

Exploring App Performance

Once Scout starts receiving performance data from your Django app, here’s how the dashboard would look:

You can see that Scout provides you with a number of metrics without much hassle. You can view your app’s mean and 95th response time, throughput, allocations, and Apdex score.

You can use the options at the top right corner of the page to move the timeline of the data being viewed and access historical performance information.

You will notice that Scout also provides you with other insights, such as N+1 queries, slow queries, memory bloat incidents, and errors. While these do not apply to the sample app at hand (since it does not interact with databases or external services), here’s what it looks like for a different sample app:

The N+1 queries section provides you with transaction traces of potential incidents of N+1 queries. You can click on a trace to learn more about its timeline, and you will also notice that Scout automatically flags this trace as a potential case of N+1 queries. 

You can find similar insights for slow query and memory bloat incidents.

Scout APM also allows you to explore the performance of each of your web endpoints. Coming back to our Django calculator app, if you click on the “Web endpoints” section at the top of the Scout dashboard, you will see a list of endpoints recognized by Scout.

You can click on the endpoint to learn more about its performance, specifically:

At the bottom, you will see transaction traces which you can click on to learn more about them. You can further explore the dashboard to learn more about your application. If you choose to add databases or external services to your project, you can easily monitor them through Scout APM. For reference, here’s what database monitoring looks like:

Scout provides you with the complete database activity of your project while also pointing out usage spikes on the left-hand side. In the next sections, you will learn how to set up custom instrumentation to profile certain parts of your code and how to set up alerts to notify your team in times of metric spikes.

How to Set up Custom Instrumentation

While ScoutAPM provides you with a definitive set of metrics that it tracks in your app, it also allows you to profile custom bits of your code to see their performance in isolation. For instance, go to calculator/ file in your Django project and update it to look like this:

from django.shortcuts import render

# Add this import for Scout
import scout_apm.api
# Add this import to access environment variables
import os

def calculator(request):

    expr = str(request.GET.get('expression'))

    # Create a config object to initialize the Scout package
    config = {
        "name": os.environ['SCOUT_NAME'],
        "key": os.environ['SCOUT_KEY'],
        "monitor": os.environ['SCOUT_MONITOR'],

    # Initialize the Scout package

    # Use the WebTransaction.start method to start tracking a transaction's performance
    scout_apm.api.WebTransaction.start("Expression Evaluation")
    ans = eval(expr)
  # Use the WebTransaction.stop method to denote the end of a transaction's code

    return render(request, 'calculator.html', {'ans': ans})

Commit and push these changes to your GitHub repo to enable Scout to start tracking your custom transaction.

In the web transactions section on your Scout APM dashboard, you will now see two list items.

You can see the insights for the “Expression Evaluation” transaction by clicking on this list item.

Setting up Alerts

Scout APM allows you to set up custom alerts based on the metrics being captured from your app. You can click on the Alerts tab in your ScoutAPM dashboard and set a new Alert Condition by clicking on the Alert Conditions option from the left navigation pane. Here’s how the Alert Conditions page would look like:

Choose any trigger for your alert from the screen, and you will next be asked to choose when the alert is sent. You can choose from a number of metrics.

You can choose to send alerts to pre-configured groups from the rightmost dropdown.

If you haven’t configured any notification groups in your Scout account, you can do so by clicking on the Notification Groups option from the left navigation pane on the Alerts page.

Here’s what a successful set-up alert condition would look like:

You will now start receiving alerts from the Scout dashboard when the condition is met.

Final Thoughts

In a market where web application performance directly influences user satisfaction and business success, Scout APM is an indispensable asset for Django developers. Its intuitive interface and potent features empower developers to proactively detect and resolve performance bottlenecks. Armed with newfound insights, developers can optimize their applications for speed and efficiency, ultimately ensuring a top-tier user experience. 

In this article, you saw how to set up Scout APM in a Django article and navigate the Scout UI to learn about your app’s performance. You're now equipped to utilize the full potential of Scout APM with your Django apps to enhance their performance and reliability.