Distributed Tracing using AWS X-Ray
When we move our applications from monolithic design to a micro services-oriented design, we get several advantages like :
- Simple to develop.
- Simple to test.
- Simple to deploy.
- Simple to scale horizontally by running multiple copies behind a load balancer.
Nothing is free in this world and the same for the microservice world. We have to pay some price for this transformation.
In the monolithic world, the whole application packaged in a single deployment unit so all the metrics and log information tends to be captured and recorded in a component and machine-centric way.
While in the microservice world, our components are spread across machines and physical locations which are kept on vary as the load varies on these microservices. So traditional tools to capture and analyze information become powerless or unuseful.
So we need Distributed Tracing.
Distributed tracing refers to methods of observing user transactions as they flow through the various components in a distributed system.
Spring provides various capabilities including Spring Cloud Sleuth, which provides support for distributed tracing. It instruments Spring components to gather trace information and can deliver it to a Zipkin Server, which gathers and displays traces.
But applications running on the cloud are getting more and more complex. Nowadays, it is very common that systems that defined as a set of microservices cooperate not only between each other but also with native cloud providers and solutions from ecosystem partners.
As application complexity increases, the debugging process in production environments gets more complicated as well.
Amazon Web Services (AWS) understands this challenge and includes tracing tools in its cloud services. AWS X-Ray helps developers analyze and debug distributed applications, such as those built using a microservices architecture.
Epsagon build on on AWS tools by providing automated end-to-end tracing across not distributed AWS services, as well as services outside of AWS
In this article, I will try to explain to you the AWS X Ray service in a simple way and guide you how to integrate this services in any Spring boot application. In next article, I will explain Epsagon services also.
What is AWS X-Ray?
AWS X-Ray is a service that collects data about requests that your application serves and provides tools you can use to view, filter, and gain insights into that data to identify issues and opportunities for optimization. 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, microservices, databases and HTTP web APIs.
The X-Ray SDK provides:
- Interceptors to add to your code to trace incoming HTTP requests
- Client handlers to instrument AWS SDK clients that your application uses to call other AWS services
- An HTTP client to use to instrument calls to other internal and external HTTP web services. The SDK also supports instrumenting calls to SQL databases, automatic AWS SDK client instrumentation, and other features.
How to integrate the AWS X-Ray in your application:
I have 2 micro services, i. e. Currency Conversion and Currency Exchange Service.
We will do the following changes in our existing application to integrate the XRay services.
- pom.xml
- AwsXrayConfig.java
- XRayInspector.java
- CurrencyExchangeController.java
- pom.xml:We will add the dependency in our existing application pom file.
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-xray-recorder-sdk-spring</artifactId> <version>2.3.0</version> </dependency>
Now we will do some modifications related to AOP configuration in our existing app.
AwsXrayConfig.java
We will create a new class and defined the name of our service inside the method TracingFilter(). Inside the logs of X-Ray service, we will find the service name as “currency-exchange-service”.
XRayInspector.java:
Now we create a new class name XRayInspector.java, where we will defined the point cut and defined which request should intercept.
As per our pointcut definition, any class which has the annotation of XRayEnabled will be intercepted.
CurrencyExchangeController.java
We will add the @XRayEnable annotation in the controller,so that the request will be intercepted.
Now let see, how we will find the logs related to our microservices in AWS.
Just Go to AWS Service>AWS X-Ray >Service Map
In Search, box type the name of the service, in our case currency exchange service.
From the above snapshot, various metrics related to our API are available to analyses.
Example: Average response time: 51ms.
On the Right side panel, we can find the Response Distribution, and below, we are seeing the Response status for all the requests. Luckily, the status for all requests is OK.
Click on Analytics Tab:
From there you can below screen. Here you can find the total number of requests hit on your server. Out of the total request, you will find the aggregate result of HTTP status code.
On clicking the URL.
We can easily trace any request. From here we can calculate how much time each service is consuming.
Refer the below image: Currency-conversion-service is taking overall 2.2 sec and you can further drill down to know the time consume by individual service.
Thanks a lot :)
Please provide your valuable feedback.
Hi NIkhil, Can you please share the code base for this project? And we use SpringCloud OpenFeign for inter service communications. Any idea of how to x-xray instrument openfeign?