AWS Xray
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
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.
- 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.
AWS Xray Tracing And Django Applications – FAQ’s
Does X-ray Impact Performance?
X-Ray adds minimal overhead, but extensive tracing can have a small impact. Use sampling to optimize.
Why X-ray For Django?
Improves debugging by visualizing request lifecycles, Makes error handling faster by correlating errors with requests, Optimizes costs by identifying underutilized AWS resources.
How Does Middleware Help?
Injects tracing headers & flushes trace data to X-Ray automatically, Add aws_xray_sdk.ext.django.middleware.XRayMiddleware to your Django settings
Does X-ray Covers All Modules?
No, as of now Xray covers only few modules, you might observer that for few packages traces will not be shown.
Comments
Post a Comment