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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What Is Cloud Computing ? Types, Architecture, Examples and Benefits
Nowadays, Cloud computing is adopted by every company, whether it is an MNC or a startup many are still migrating towards it because of the cost-cutting, lesser maintenance, and the increased capacity of the data with the help of servers maintained by the cloud providers. Cloud Computing means stori
15 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read