Implementation of X-Ray with Django application.

Himansh Kumar
4 min readJun 6, 2020

--

Source: AWS Docs.

1. INTRODUCTION

In my last blog “A brief introduction to AWS X-Ray “, we discussed what AWS X-Ray is briefly. We also discussed the frameworks and services supported, advantages, and benefits of AWS X-Ray.

In this blog, we will be going through the implementation and hands-on how to implement X-Ray with your application on Elastic Beanstalk.

2. Implementing AWS X-Ray on Elastic Beanstalk

AWS Elastic Beanstalk is an orchestration service offered by Amazon Web Services for deploying applications that orchestrate various AWS services, including EC2, S3, Simple Notification Service, CloudWatch, Autoscaling, and Elastic Load Balancers.We can deploy multiple solution stacks on Elastic Beanstalk. Some of the solution stacks are python, java, Node.js, Ruby,Go etc.

In this blog, we will be implementing the X-Ray on a python Django application.

3. Prerequisite

  1. You have an IAM user with read and write permissions to Elastic beanstalk, AWS X-Ray, and S3.
  2. Assuming you already have deployed a python Django Application on elastic beanstalk.
  3. Have access to the source code of your application.

4. Preparing Elastic Beanstalk

As we know AWS X-Ray works with the combination of SDK and a daemon. The daemon needs to be installed on the systems.

AWS Elastic Beanstalk provides us with an integration to install X-Ray daemon on all the instances provisioned by the beanstalk. We can do this using the AWS Elastic Beanstalk console or the .ebextensions.

4.1 Adding X-Ray policy to elastic beanstalk-ec2-role.

  1. Create an inline policy in the elastic beanstalk-ec2-role.
**NOTE:-This is just a sample policy you can add or remove permissions according to your requirements.

4.2 Using the Elastic Beanstalk console.

  1. Go to the environment on which you want to integrate X-Ray.
  2. Go to configurations and click on Software configuration.
  3. Enable the X-Ray daemon checkbox.
  4. Apply the changes and wait till elastic beanstalk make changes.

4.3 Using .ebextensions

1. Create a file xray-daemon.config in folder .ebextensions, the folder should be placed in the root of the application code.

2. (optional) X-Ray daemon also stores logs inside the system, you can bring those logs to a centralized location managed by elastic beanstalk.The logs configuration are as follows:-

5. Preparing your code

1. Install aws-xray-sdk==2.5.0, enter in requirements.txt with all other requirements of your application.

2. Add X-Ray Middleware as first entry in the MIDDLEWARE in the settings.py file as shown in the example:-

All the incoming requests in the Django application are recorded as segments.

3. Add aws_xray_sdk.ext.django in the INSTALED_APPS segment of file settings.py.

4. Configure X-RAY to record incoming requests on the application. The configuration to record segments are as follows:-

A brief about all the keywords used in the record configuration.

1. AWS_XRAY_DAEMON_ADDRESS:- Address of the system on which the X-Ray demon is executed.
2. AUTO_INSTRUMENT:- If turned on built-in database queries and template rendering will be recorded as sub-segments.
3. AWS_XRAY_CONTEXT_MISSING:- This is used to log any error that occurs on the runtime of the application rather than stopping the application.
4. AWS_XRAY_TRACING_NAME:- Name that is shown on the X-Ray console, it can also be passed as an environment variable.
5. PATCH_MODULES:- To keep track of other modules as a separate entity. Example:- To keep track of third party API performance which you are hitting with requests library add requests in patch modules.
6. PLUGINS:- The plugin adds extra metadata for each segment if the app is running on that environment. The SDK provides three plugins:-
6.1 Amazon EC2:-EC2Plugin adds the instance ID and Availability Zone.
6.2 Elastic Beanstalk:-ElasticBeanstalkPlugin adds the environment name, version label, and deployment ID.
6.3. Amazon ECS:- ECSPlugin adds the container host-name.
7. SAMPLING:- Sampling is enabled by default. Whenever the global recorder creates a segment, it decides whether to sample this segment. If it does not sample this segment, it is discarded and not sent to the X-Ray daemon.
8. SAMPLING_RULES:- The rules to use while sampling. By default only the default rule is used.
9. DYNAMIC_NAMING:- Defines a pattern that host names should match.
10. STREAMING_THRESHOLD:- Defines when a segment starts to stream out its children sub-segments.

6. Creating a sampling rule.

When an application is deployed on elastic beanstalk a health check API is developed. The API only gives the health check whether the application is deployed properly or not. Load Balancer keeps hitting that API to monitor the instance health. So there are lots of traces of only hitting the health API which are of no use and can finish the free quota of recording traces. Therefore we create a sampling rule defining to ignore the health check API.

7. Conclusion

AWS X-Ray is an AWS managed service which helps in getting the overview of all the API requests made by the application. It helps you understand the number of API’s falling, the bottlenecks in the application and how robust your application is. In the blog we did a hands on a python Django application.

The implementation can also be done on java, nodejs, go lang etc.

References:- Django — aws-xray-sdk 2.5.0 documentation

--

--