Configuring Django application with Elastic APM


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

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 https://artifacts.elastic.co/downloads/apm-server/apm-server-7.5.1-amd64.deb
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:
output.elasticsearch:
    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 (https://github.com/anubioinfo/django_blog_api ).
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 https://github.com/anubioinfo/django_blog_api.git

# move inside the project directory
cd django_blog_api/

# make migrations
python3 manage.py makemigrations

# migrate the tables
python3 manage.py migrate

# run the server
python3 manage.py runserver
2. After successfully executing the preceding command, you can run the Django application using the following URL:
http://127.0.0.1:8000/blogs

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:
http://127.0.0.1:8000/swagger/



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

python manage.py createsuperuser

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

http://127.0.0.1:8000/admin

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 settings.py file:

# Add the agent to the installed apps
INSTALLED_APPS = (
'elasticapm.contrib.django',
# ...
)
ELASTIC_APM = {
# 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:
MIDDLEWARE = (
'elasticapm.contrib.django.middleware.TracingMiddleware',
#...
)

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

3) After doing these changes in the settings.py 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: https://twitter.com/anu4udilse

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

preview thumbnail
Introduction to Logstash

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

preview thumbnail
Importing MongoDB data into Elasticsearch

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

preview thumbnail
Importing MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

preview thumbnail
Snapshot and Restore Elasticsearch Indices

Sep 16, 2019, 5:55:06 AM | Anurag Srivastava

preview thumbnail
Log analysis with Elastic stack

Jan 31, 2018, 6:11:29 AM | Anurag Srivastava

preview thumbnail
Creating Elasticsearch Cluster

Apr 6, 2019, 8:41:41 PM | Anurag Srivastava

Top Blogs

preview thumbnail
Configure SonarQube Scanner with Jenkins

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

preview thumbnail
Execute Commands on Remote Machines using sshpass

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

preview thumbnail
Importing MongoDB data into Elasticsearch

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

preview thumbnail
Importing MySQL data into Elasticsearch

Feb 9, 2019, 12:06:18 PM | Anurag Srivastava

preview thumbnail
Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

preview thumbnail
Deploying Angular code using Python script

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava