How To Setup AWS Xray Tracing Setup Or Django Application ?
Last Updated :
04 Jan, 2025
AWS X-Ray provides powerful tracing capabilities, allowing you to gain insights into your application’s performance, identify bottlenecks in your app, and troubleshoot issues effectively so that you can optimize your application performance.
This article will take you on a step-by-step guide to set up xray-tracing on your Django application, we'll guide you from installing, and configuring, and also we'll show where & how to analyze the traces.
What Is AWS Xray Tracing?
AWS X-Ray is a service that collects data about requests that your application serves, every request will be recorded either you using the GET, POST, or PUT method, and provides various tools that you can use to view, and filter. For any traced request to your application, you can see detailed information not only about the request and response, but also about calls that your application makes to downstream AWS resources like RDS, DynamoDB, etc, also in microservices, databases, and web APIs.
AWS X-Ray offers a comprehensive solution for visualizing and analyzing request traces across your entire application. It captures details like service calls, database interactions, and API invocations, enabling you to identify latency issues and optimize application performance with ease.
In this article we gonna discuss how to configure AWS Xray for your Django Application, enabling code profiling at ease. Assuming you've already deployed your Django application in AWS Lambda, if not follow this guide, How to Deploy Django Application in AWS Lambda?
Why X-Ray For Django Apps?
The following are reasons for using X-Ray Tracing for Django Applications:
- Enhanced Debugging: Gain insights into request lifecycles, pinpointing bottlenecks and identifying slow-performing components.
- Improved Error Handling: Correlate errors with specific requests and diagnose issues more efficiently.
- Cost Optimization: Identify underutilized resources and optimize infrastructure based on actual service usage patterns.
Django-Xray Tracing Setup: A Step-By-Step Guide
To configure the AWS Xray for your Django Application follow the below steps:
Step 1: Installation Of AWS Xray
- To install aws xray in your system, run the below command.
pip install aws_xray_sdk

Step 2: Configure AWS X-Ray In Django Settings
- Instrument Django Middleware: In settings.py, add below line at the top of MIDDLEWARE's list
"aws_xray_sdk.ext.django.middleware.XRayMiddleware"
- This would be similar to below:
MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
...
...
'aws_xray_sdk.ext.django.middleware.XRayMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
]
- Middleware provides a convenient mechanism to integrate AWS X-Ray tracing seamlessly into your Django application.
- By adding the aws_xray_sdk.ext.django.middleware.XRayMiddleware to your middleware stack, you can automatically capture request traces for X-Ray analysis.
- This middleware intercepts requests, injects necessary tracing headers, and flushes trace data to X-Ray, enabling comprehensive distributed tracing for your application.
Step 3: Configure Xray
- Add the Xray config in DJango settings file, using XRAY_RECORDER variable, refer the sample config in settings.py,
XRAY_RECORDER = {
'AWS_XRAY_DAEMON_ADDRESS': '127.0.0.1:2000',
'AUTO_INSTRUMENT': True,
'AWS_XRAY_TRACING_NAME': 'my-application',
'PATCH_MODULES': [
"my_module_1",
"my_module_2",
],
"AUTO_PATCH_PARENT_SEGMENT_NAME": True,
"SAMPLING": True,
'SAMPLING_RULES': {
"version": 1,
"rules": [
{
"description": "GraphQL APIs",
"service_name": "*",
"http_method": "*",
"url_path": "/graphql",
"fixed_target": 1,
"rate": 0.01
}
],
"default": {
"fixed_target": 0,
"rate": 0.01
}
}
}
- AWS_XRAY_DAEMON_ADDRESS: Set the host and port of the X-Ray daemon listener. By default, the SDK uses 127.0.0.1:2000 for both trace data (UDP) and sampling (TCP). Use this variable if you have configured the daemon to listen on a different port or if it is running on a different host.
- AUTO_INSTRUMENT: If turned on built-in database queries and template rendering will be recorded as sub-segments.
- AWS_XRAY_TRACING_NAME: Name of the trace, it will be shown in the xray-traces console.
- PATCH_MODULES: by default not all the modules are covered in xray-tracing, to include any third-party modules for analysis use this config.
- SAMPLING_RULES: The rules to use while sampling. By default only the default rule is used, you can customize the sampling rate based on specific paths as well.
Step 4: Instrument Function Calls (Optional)
- To include any specific method apart from XRAY_RECORDER config or without using the middleware, you can use the @xray_recorder decorator at any methods
from aws_xray_sdk.core import xray_recorder
@xray_recorder.capture('some_unique_name_here')
def my_function():
pass
Step 5: Enable Xray Tracing At Lambda Function Level
- To enable xray tracing in lambda, you've two methods either do it manually or you can update zappa_settings.json, set xray_tracing with value true.
Method 1: Enable XRay-Tracing Manually
- Go to AWS Lambda Page.
- Navigate to your function
- Open Function , then move to Configuration tab
- Then Open Monitoring and Operational tools tab
- Click on edit under Additional Monitoring tools
- Check the AWS Xray Active Sharing
- Then save.
.jpg)
- Navigate to test functions in lambda functions and edit the monitoring tools with enabling Active tracing.

Method 2: Using Zappa Configuration
- If you're using Zappa for deployment as mentioned above, you can enable AWS X-Ray support on your function with a configuration setting:
- You just need to set `xray_tracing` to true, the above manual process will be automated by zappa using boto3, refer the sample config below
{
"alpha": {
"project_name": "my-project",
...
...
"xray_tracing": true
}
}
- After the above steps deploy your application and make the few requests, then check out xray traces at https://ap-south-1.console.aws.amazon.com/cloudwatch/home?region=ap-south-1#xray:traces (update region based on your deployment)
Step 6: View Traces And Analyze Data
- Open Xray-Traces by searching "X-ray" in AWS console, on clicking it you will landed on it directly.

- Navigate to "Traces" under "Xray traces" group in the left panel.

- You will find the traces here only if your application had a run after deploying.
- Click on the trace to view the more details, here you can find breakdown with different components in your application along with duration, refer the image below.

Conclusion
By employing X-Ray tracing for your Django application, you gain valuable insights into requests, helps in debugging issues, black sheep behind high latencies, improving performance management, efficiency, and ultimately, user experience.
Similar Reads
How to Configure AWS X-Ray Application Tracing?
AWS X-Ray is a service that helps a developer trace and analyze distributed applications in the cloud, when dealing with a significant level of complexity, in terms of how different components interrelate, especially since the inception of microservices and serverless architecture, knowledge is key.
8 min read
How To Use PostgreSQL with your Django Application on Ubuntu
This article describes how to configure PostgreSQL with the Django application on your Ubuntu machine. First, let's look at an overview of all the tools we use. PostgreSQL is a high-performance, reliable, and robust open-source relational database management system (RDBMS).Django is a robust, free,
4 min read
How To Integrate Ajax with Django Applications
Django is one of the most popular web frameworks for building robust and scalable web applications. However, many modern applications require asynchronous features, enabling real-time interactions without the need to reload the entire page. This is where Ajax (Asynchronous JavaScript and XML) comes
5 min read
How to Deploy Django Application in AWS Lambda?
Pre-requisite: AWS , Python Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development. AWS Lambda is a serverless computing platform that runs your code in Docker c
7 min read
How to Deploy Django Application in AWS EC2?
In this article, we will study how we can deploy our existing Django web application to Windows Server in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the Django application. For this article, you should know about setting up EC2 in AWS. We will see how to deploy
3 min read
How To Deploy Python Application In AWS?
In this article, we will explore how one as a Python developer can deploy the application by harnessing the capabilities of AWS. AWS, a leading cloud computing platform, offers a wide range of services to help developers build, deploy, and manage applications at scale EC2. It provides scalable compu
4 min read
Build a To-Do application Using Django, React and Tailwind
This article will guide you in creating a To-Do application using React and Tailwind with the Django Framework. Weâll explore the integration of Django, React, and Tailwind, and go through the step-by-step process of implementing the To-Do application. What is a To-Do application?A To-Do application
6 min read
How to Deploy a Django Application in Kubernetes
In this article, we will study how we can deploy Django web applications to Kubernetes. We will also see how to dockerize and build an image of the Django application using Dockerfile. For this article, you should know about setting up a VM in Azure. We will see how to deploy applications on Linux S
5 min read
How To Deploy Node Js Application In AWS Lightsail ?
Lightsail can be defined as a simple, easy-to-use, and user-friendly service offered by Amazon Web Services (AWS). The main goal of Lightsail is to provide an easy way for individuals, startups, and small businesses to launch and manage virtual private servers (VPS) and other cloud services without
6 min read
How to Run Django's Test Using In-Memory Database
By default, Django creates a test database in the file system, but it's possible to run tests using an in-memory database, which can make our tests faster because it avoids disk I/O operations.In this article, weâll explore how to run Django tests with an in-memory database, the advantages of this a
6 min read