Blog Detail

Covid-19 Tracker Ask Question

preview image DevOps
by Anurag Srivastava, Jan 14, 2020, 10:22:34 AM | 6 minutes |

Configuring Django application with Elastic APM

In the previous blog "Introduction to Elastic APM", I have covered the introduction to Elastic APM where we have discussed different components of APM and how they can work together to monitor the Application performance. Now we will learn how to configure APM for a Django blog API application setup. I will explain this process on Ubuntu operating system ver. 19.04. So let us start the configuration of Elastic APM for Python Django APIs.

If you want to learn more about monitoring then please refer to "Why monitoring is important?" blog. You may also like the blog on how to do "Log analysis with Elastic stack".

As I have already mentioned in my previous blog, we need APM Server which will receive the application metrics, APM agent which will send the metrics to the APM server after it is configured with the application and at last the running application which we are going to monitor. So the first step is to install the APM server.

Install the APM server:
Here I am going to explain how this can be done on Ubuntu but on Kibana's APM page you can click on the "Setup Instructions" link and can see the steps to install APM Server on different platforms, so don't worry and install it on your system. But before APM you need to install Elasticsearch and Kibana as this is required before APM setup. So to install APM Server we need to do the following:

1) You can download and unpack the APM server using the following commands:

curl -L -O
sudo dpkg -i apm-server-7.5.1-amd64.deb

2) After the installation, you can edit the apm-server.yml file and change the "output.elasticsearch" setting as per you Elasticsearch credentials:
    hosts: ["<es_url>"]
    username: <username>
    password: <password>

3) After changing the configuration you can start the APM server using the below command:
service apm-server start

4) You can verify if the service is up and running by checking the service status:
service apm-server status.

5) It usually runs on 8200 port so if you try to open it on the browser, you would get the following response:
    "build_date": "2019-12-16T20:57:59Z",
    "build_sha": "348d8d83c3c823b64fc0692be607b1a5a8fac775",
    "version": "7.5.1"
This way we can install and configure the APM Server, now we should run the Django application in order to configure APM Agent with the application.

Run your Python Django Application:

You can get a test Django blog application from my GitHub repository ( ).
You can use your own Django application  or can refer to my test blog application. If you are trying my test application then you need to do the following to run the blog application:

1. You need to download this application from the GitHub page, and then navigate inside the main directory to execute the following commands:
# git clone the project
git clone

# move inside the project directory
cd django_blog_api/

# make migrations
python3 makemigrations

# migrate the tables
python3 migrate

# run the server
python3 runserver
2. After successfully executing the preceding command, you can run the Django application using the following URL:

This application is very simple where the user can add blogs and list them using the API. The following screenshot shows the default UI of Django for adding a blog:

3. You can access the swagger page using the following link:

Django Super admin account:
You can create the Django super admin using the below command:

python createsuperuser

After creating the super admin account you can access the Django admin interface using below URL:

So as the Django application is running successfully, we are now good to configure the Elastic APM Agent.

Configure APM Agent with Django Application:
As here we are dealing with a Python Django application so we have to install the APM Agent for Django. But if you are dealing with other languages or tool then you can refer to the  "Setup Instructions" link on Kibana's APM page. There you can get the installation steps of APM Agents for Java, RUM(JS), Node.js, Flask, Ruby in Rails, Rack, Go and .NET. To set up the APM Agent with Django, we need to do the following:

1) In Django to run the APM agent for Python, we need to install the elastic-apm module using the following command:

pip install elastic-apm

2) Now we need to configure the agent and configure APM with this Django application. We need to make the following changes in the file:

# Add the agent to the installed apps
# ...
# Set required service name. Allowed characters:
# a-z, A-Z, 0-9, -, _, and space
'SERVICE_NAME': 'django application',
# Use if APM Server requires a token
'SECRET_TOKEN': 'mysecrettoken',
# Set custom APM Server URL (default: http://localhost:8200)
'SERVER_URL': 'http://localhost:8200',
# To send performance metrics, add our tracing middleware:

Using the preceding changes in the file, the APM agent can be configured to the Python Django application.

3) After doing these changes in the file, we can start the Django application and then verify from the Kibana's "Setup Instructions" page if everything is working as per our expectations.

Once the configuration is done in Django, we can restart the Django application and can start getting the metrics in Kibana under APM. We can monitor the APM data using the inbuilt APM UI or can create our dashboard in Kibana. So in this blog, we have covered how to configure the Elastic APM using a Django application. In the next blog, I will explain how we can monitor the APM data using the Kibana dashboard.

Other Blogs on Elastic Stack:

How to create Elasticsearch Cluster
Bucket Aggregation in Elasticsearch

Metrics Aggregation in Elasticsearch
Configure Logstash to push MySQL data into Elasticsearch 
Wildcard and Boolean Search in Elasticsearch
Elasticsearch Rest API
Basics of Data Search in Elasticsearch
Elasticsearch Rest API
Log analysis with Elastic stack 
Elasticsearch Installation and Configuration on Ubuntu 14.04
Introduction to Elasticsearch
Configure Logstash to push MongoDB data into Elasticsearch
Load CSV Data into Elasticsearch

In case of any doubt please leave your comments. You can also follow me on Twitter:

If you found this article interesting, then you can explore “Mastering Kibana 6.0”, “Kibana 7 Quick Start Guide”, “Learning Kibana 7”, and “Elasticsearch 7 Quick Start Guide” books to get more insight about Elastic Stack, how to perform data analysis, and how you can create dashboards for key performance indicators using Kibana.

Comments (0)

Leave a comment

Related Blogs

Introduction to Cloud Computing

Sep 23, 2020, 2:38:00 PM | Anurag Srivastava

Monitoring website uptime using Kibana

Sep 6, 2020, 5:20:33 PM | Anurag Srivastava

Monitoring Application using Elastic APM

Jan 16, 2020, 11:43:29 AM | Anurag Srivastava

Introduction to Elastic APM

Jan 7, 2020, 7:15:34 PM | Anurag Srivastava

Why monitoring is important?

Jan 6, 2020, 7:30:13 PM | Anurag Srivastava

Introduction to Logstash

Dec 20, 2019, 11:38:31 AM | Anurag Srivastava

Top Blogs

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Importing MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava