---
title: Distributed Tracing using AWS X-Ray
description: "As monoliths have been replaced by microservice applications, growing in size\nand complexity,\nthe challenge of troubleshooting and identifying performance issues has\nincreased.\nIn addition, extensive usage of cloud platforms such as AWS to deploy\nmicroservice architectures make the code execution points highly elastic and\ndynamic. Traditional tools to capture"
image: https://blog.ippon.tech/hubfs/Imported_Blog_Media/Distributed-Tracing-using-AWS-X-Ray-3.png
---

[Skip to main content](https://blog.ippon.tech/distributed-tracing-using-aws-x-ray#main)

[![IpponLogoKleinBlue](https://blog.ippon.tech/hs-fs/hubfs/IpponLogoKleinBlue.png?width=219&height=64&name=IpponLogoKleinBlue.png) ![IpponLogoKleinBlue](https://blog.ippon.tech/hs-fs/hubfs/IpponLogoKleinBlue.png?width=219&height=64&name=IpponLogoKleinBlue.png) ![IpponLogoKleinBlue](https://blog.ippon.tech/hs-fs/hubfs/IpponLogoKleinBlue.png?width=168&height=49&name=IpponLogoKleinBlue.png) ![IpponLogoKleinBlue](https://blog.ippon.tech/hs-fs/hubfs/IpponLogoKleinBlue.png?width=168&height=49&name=IpponLogoKleinBlue.png)](https://ipponusa.com/)

- [Home](https://ipponusa.com)
- [Show submenu for About About](https://ipponusa.com/about-us) 
    - [Who We Are](https://ipponusa.com/who-we-are)
    - [Careers](https://ipponusa.com/careers)
    - [Show submenu for Our Partners Our Partners](https://ipponusa.com/our-partners/) 
          - [AWS](https://ipponusa.com/our-partners/aws/)
          - [Snowflake](https://ipponusa.com/our-partners/snowflake/)
          - [Databricks](https://ipponusa.com/our-partners/databricks/)
          - [Microsoft](https://ipponusa.com/our-partners/microsoft/)
- [Show submenu for Services Services](https://ipponusa.com/services) 
    - [Snowflake](https://ipponusa.com/service/snowflake-concierge/)
    - [Artificial Intelligence](https://ipponusa.com/service/artificial-intelligence/)
    - [Data & Analytics](https://ipponusa.com/service/data-analytics/)
    - [Cloud Strategy](https://ipponusa.com/service/cloud-strategy/)
    - [Platform Modernization](https://ipponusa.com/service/platform-moderization/)
    - [Product Innovation](https://ipponusa.com/service/product-innovation/)
    - [Operating Model](https://ipponusa.com/service/operating-model/)
- [Blogs](https://blog.ippon.tech/)
- [Success Stories](https://ipponusa.com/success-stories/)
- [Show submenu for Resources Resources](https://ipponusa.com/resources/) 
    - [eBooks](https://ipponusa.com/ebooks/)
    - [The Data Pour](https://info.ippon.tech/the-data-pour)
    - [Videos & Webinars](https://info.ippon.tech/videos-and-webinars)
    - [Media Center](https://ipponusa.com/media-center/)

Search

Open main navigation

Close main navigation

- [Home](https://ipponusa.com)
- Show submenu for About About 
  
    - About
    - [About](https://ipponusa.com/about-us)
    - [Who We Are](https://ipponusa.com/who-we-are)
    - [Careers](https://ipponusa.com/careers)
    - Show submenu for Our Partners Our Partners 
      
          - Our Partners
          - [Our Partners](https://ipponusa.com/our-partners/)
          - [AWS](https://ipponusa.com/our-partners/aws/)
          - [Snowflake](https://ipponusa.com/our-partners/snowflake/)
          - [Databricks](https://ipponusa.com/our-partners/databricks/)
          - [Microsoft](https://ipponusa.com/our-partners/microsoft/)
- Show submenu for Services Services 
  
    - Services
    - [Services](https://ipponusa.com/services)
    - [Snowflake](https://ipponusa.com/service/snowflake-concierge/)
    - [Artificial Intelligence](https://ipponusa.com/service/artificial-intelligence/)
    - [Data & Analytics](https://ipponusa.com/service/data-analytics/)
    - [Cloud Strategy](https://ipponusa.com/service/cloud-strategy/)
    - [Platform Modernization](https://ipponusa.com/service/platform-moderization/)
    - [Product Innovation](https://ipponusa.com/service/product-innovation/)
    - [Operating Model](https://ipponusa.com/service/operating-model/)
- [Blogs](https://blog.ippon.tech/)
- [Success Stories](https://ipponusa.com/success-stories/)
- Show submenu for Resources Resources 
  
    - Resources
    - [Resources](https://ipponusa.com/resources/)
    - [eBooks](https://ipponusa.com/ebooks/)
    - [The Data Pour](https://info.ippon.tech/the-data-pour)
    - [Videos & Webinars](https://info.ippon.tech/videos-and-webinars)
    - [Media Center](https://ipponusa.com/media-center/)
- Search
- [Contact Us](https://ipponusa.com/contact/)

[Contact Us](https://ipponusa.com/contact/)

# Distributed Tracing using AWS X-Ray

![Amine Ouali Alami](https://app.hubspot.com/settings/avatar/d41d8cd98f00b204e9800998ecf8427e)

 by [Amine Ouali Alami](https://blog.ippon.tech/author/amine-ouali-alami)

March 8, 2021

![Distributed Tracing using AWS X-Ray](https://blog.ippon.tech/hubfs/Imported_Blog_Media/Distributed-Tracing-using-AWS-X-Ray-3.png)

[DevOps](https://blog.ippon.tech/tag/devops/) [Spring](https://blog.ippon.tech/tag/spring/) [AWS](https://blog.ippon.tech/tag/aws/) [Cloud](https://blog.ippon.tech/tag/cloud/) [Microservices](https://blog.ippon.tech/tag/microservices/) [Monitoring](https://blog.ippon.tech/tag/monitoring/) [X-ray](https://blog.ippon.tech/tag/x-ray/) [Distributed Tracing](https://blog.ippon.tech/tag/distributed-tracing/) [Architectures](https://blog.ippon.tech/tag/architectures/) [Observability](https://blog.ippon.tech/tag/observability/)

As monoliths have been replaced by microservice applications, growing in size and complexity, the challenge of troubleshooting and identifying performance issues has increased. In addition, extensive usage of cloud platforms such as AWS to deploy microservice architectures make the code execution points highly elastic and dynamic. Traditional tools to capture and analyze information in a monolithic world become powerless in this new environment.

Architects and IT operations now require operational visibility into their entire architecture.

On AWS we can use distributed tracing services, such as [AWS X-Ray](https://aws.amazon.com/xray/?ref=blog.ippon.tech), to collect and record traces as requests travel through the application, generate maps to see how requests flow across services, gain insight on the relationships between components, as well as identify and analyze issues in real time.

## Integrate X-Ray to a Spring Boot application

In this article we will see how we can setup distributed tracing for a sample application written in Spring Boot.  
This is how it works:

- The application need to implement the X-Ray Java SDK which generates and sends the tracing data to a X-Ray daemon service
- The X-Ray daemon service will then send the data to the AWS X-Ray API
- Tracing data is available on the AWS X-Ray Service Console

### Maven dependencies

We need first to add the AWS X-Ray Java SDK to our pom.xml.

```
    <dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-core</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-aws-sdk-instrumentor</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>com.amazonaws</groupId>
			<artifactId>aws-xray-recorder-sdk-spring</artifactId>
			<version>2.8.0</version>
		</dependency>
```

### Tracing incoming requests

To trace all incoming requests to our microservice, first we configure a Servlet Filter provided by the SDK. Then we add the Servlet to our Spring configuration.

```
  ``` java
  @Configuration
  public class AwsXRayConfig {

    @Bean
    public Filter TracingFilter() {
      return new AWSXRayServletFilter("micro1-app");
    }
  }
  ```
```

After adding the X-Ray servlet filter to our application, the X-Ray SDK for Java creates a segment for each incoming request with an `http` block that contains the following information:

- HTTP method: GET, POST, PUT, DELETE, etc.
- Client address: The IP address of the client that sent the request.
- Response code: The HTTP response code for the completed request.
- Timing: The start time (when the request was received) and end time (when the response was sent).
- User agent: The user-agent from the request.
- Content length: The content-length from the response.

### Tracing your spring bean components

The X-Ray SDK offers a non-invasive Spring integration way of tracing our Spring beans. This help us see the complete call stack of the application from the controller down through the back-end layers.

- We need to add first a spring AOP Aspect that will catch your method's execution and generate your tracing metadata:
  
  ```
  @Aspect
  @Component
  public class XRayInspector extends BaseAbstractXRayInterceptor {
  
      @Override
      protected Map<String, Map<String, Object>> generateMetadata(ProceedingJoinPoint proceedingJoinPoint,
              Subsegment subsegment) {
          return super.generateMetadata(proceedingJoinPoint, subsegment);
      }
  
      @Override
      @Pointcut("@within(com.amazonaws.xray.spring.aop.XRayEnabled) && bean(*)")
      public void xrayEnabledClasses() {
      }
  
  }
  ```
- Let's annotate the spring components that we want to trace:
  
  ```
  @Component
  @XRayEnabled
  public class HelloService { 
    
      public String greetingHello() {
          return "Hello from micro 1 service";
      }
  }
  ```

The @XRayEnabled annotation can be used on the class or method level for more fine-grained control.

### Tracing your outbound HTTP calls

When making external calls to API's, we will need to use the AWS X-Ray Apache library which act as a wrapper to the Apache HttpComponents. This is the only time when we may have an impact on the existing code, especially if another HTTP client is used other than Apache like Spring Rest Template or HTTP Client (available since Java 11).

```
``` java
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import com.amazonaws.xray.proxies.apache.http.HttpClientBuilder;

  public String externalApiCall() throws IOException {
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet("http://names.example.com/api/");
    CloseableHttpResponse response = httpclient.execute(httpGet);
    try {
      HttpEntity entity = response.getEntity();
      InputStream inputStream = entity.getContent();
      // consume the response
      return response;
    } finally {
      response.close();
    }
  }
```
```

## Run the AWS X-Ray daemon as a side container on Amazon ECS

Before to deploy our microservice on AWS ECS, we need to setup the AWS X-Ray Daemon.  
In this example the X-Ray Daemon will be running as a [Sidecar Container](https://medium.com/bb-tutorials-and-thoughts/kubernetes-learn-sidecar-container-pattern-6d8c21f873d?ref=blog.ippon.tech) using the X-Ray-Daemon container provided by AWS.  
Our ECS task will have then two container definitions when the application container will send trace data on `localhost:2000` to the X-Ray Daemon container.

![01](https://blog.ippon.tech/hubfs/Imported_Blog_Media/X-Ray-container-2.jpg)

We also need to give the `task` role permission to contact the AWS X-Ray API:

```
```json
  {
    "family": "micro1",
    "executionRoleArn": "arn:aws:iam::9666461757999:role/ecsTaskExecutionRole",
    "taskRoleArn": "arn:aws:iam::926646175999:role/xray-daemon-write",
    "containerDefinitions": [
      {
        "name": "xray-daemon",
        "image": "amazon/aws-xray-daemon",
        "cpu": 32,
        "memoryReservation": 256,
        "logConfiguration": {
          "logDriver": "awslogs",
          "options": {
            "awslogs-group": "/ecs/group1",
            "awslogs-region": "us-east-2",
            "awslogs-stream-prefix": "ecs"
          }
        },
        "portMappings": [
          {
            "hostPort": 2000,
            "containerPort": 2000,
            "protocol": "udp"
          }
        ]
      },
      {
        "name": "micro1-container",
        "image": "9666461757999.dkr.ecr.us-east-2.amazonaws.com/micro1:65435390a2d95f73f6f379cb63338a305641987ba",
        "cpu": 512,
        "portMappings": [
          {
            "hostPort": 8888,
            "protocol": "tcp",
            "containerPort": 8888
          }
        ],
        "memory": 1024,
        "essential": true,
        "logConfiguration": {
          "logDriver": "awslogs",
          "options": {
            "awslogs-group": "/ecs/group1",
            "awslogs-region": "us-east-2",
            "awslogs-stream-prefix": "ecs"
          }
        }
      }
    ],
    "requiresCompatibilities": [
      "FARGATE"
    ],
    "networkMode": "awsvpc",
    "cpu": "1024",
    "memory": "2048"
  }

```
```

Please checkout this article on [how to deploy a Spring Boot microservice on Amazon ECS using Github Actions](https://blog.ippon.tech/deploytoecswithgithubaction/).

## View and Analyse the tracing data on AWS X-Ray Console

X-Ray console uses trace data sent by the X-Ray daemon to generate a detailed service graph. The service graph can shows the client, front-end service, and backend services.

In this example we are tracing the client requests as they travel through the application which is comprised of 2 microservices:

![02](https://blog.ippon.tech/hubfs/Imported_Blog_Media/X-Ray-console-2.png)

The console provides a filter expression language for filtering requests, and services based on data in request headers, response status, and indexed fields on the tracing data.

The service graph helps identify bottlenecks and latency spikes as shown in the example bellow where we introduced a latency on the first microservice:

![03](https://blog.ippon.tech/hubfs/Imported_Blog_Media/X-Ray-console2-2.png)

It also identify services where errors are occurring:

![04](https://blog.ippon.tech/hubfs/Imported_Blog_Media/X-Ray-console3-2.png)

Exception logs are also available:

![05](https://blog.ippon.tech/hubfs/Imported_Blog_Media/X-Ray-console4-2.png)

More details on the AWS X-Ray console can be found [here](https://docs.aws.amazon.com/xray/latest/devguide/xray-console.html?ref=blog.ippon.tech)

## Conclusion

AWS X-Ray integrates natively with other AWS Services to increase the visibility of your architecture. The [AWS X-Ray agent can also be run locally](https://docs.aws.amazon.com/xray/latest/devguide/xray-daemon-local.html?ref=blog.ippon.tech) if some pieces of the application is deployed on-premises.

AWS X-Ray always encrypts traces at rest, and can also be configured to use AWS Key Management Service (AWS KMS) for compliance requirement like PCI and HIPAA.

Want to implement Distributed Tracing to monitor your architecture? Feel free to [contact us](https://en.ippon.tech/contact/?ref=blog.ippon.tech). It would be a pleasure to help and work with you!

### Sources

- [AWS X-Ray developer guide](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html?ref=blog.ippon.tech)

## Related Articles

##### [![Deep Dive Webflux](https://blog.ippon.tech/hs-fs/hubfs/1-2.png?width=520&height=294&name=1-2.png) Spring • July 11, 2024 Deep Dive Webflux 4 min read](https://blog.ippon.tech/deep-dive-webflux)

##### [![Getting Your Application Started With WebFlux](https://blog.ippon.tech/hs-fs/hubfs/Webflux%20image%20(1).png?width=520&height=294&name=Webflux%20image%20(1).png) Spring • May 7, 2024 Getting Your Application Started With WebFlux 6 min read](https://blog.ippon.tech/getting-your-application-started-with-webflux)

##### [![Webflux - Starting your journey](https://blog.ippon.tech/hs-fs/hubfs/Untitled%20design.png?width=520&height=294&name=Untitled%20design.png) Ippon • March 19, 2024 Webflux - Starting your journey 6 min read](https://blog.ippon.tech/webflux-starting-your-journey)

### Comments

### Subscribe to Our Blog!

Stay informed with the latest insights and updates by signing up for our weekly blog newsletter – delivered straight to your inbox!

[Back to blog homepage »](https://blog.ippon.tech/)

![IpponLogoKleinBlue](https://blog.ippon.tech/hs-fs/hubfs/IpponLogoKleinBlue.png?width=247&height=73&name=IpponLogoKleinBlue.png "IpponLogoKleinBlue")

Ippon is a consulting and expertise firm, who is convinced that technology is a source of progress for society. We help our clients leverage their digital assets to design an appropriate strategy and deploy their transformation roadmap at scale.

#### Navigation

- [Home](https://ipponusa.com/)
- [About](https://ipponusa.com/about-us/)
- [Services](https://ipponusa.com/services/)
- [Blogs](https://blog.ippon.tech/?__hstc=223043268.1036d82247623b329dfefe4e92697801.1714729179243.1714729179243.1714729179243.1&__hssc=223043268.2.1714729179244&__hsfp=803678701)
- [eBooks](https://ipponusa.com/ebooks/)
- [The Data Pour](https://info.ippon.tech/the-data-pour?__hstc=223043268.1036d82247623b329dfefe4e92697801.1714729179243.1714729179243.1714729179243.1&__hssc=223043268.2.1714729179244&__hsfp=803678701)

[Contact Us](https://ipponusa.com/contact/)

[Join Us](https://ipponusa.com/careers/)

#### Services

- [Data & Analytics](https://ipponusa.com/service/data-analytics/)
- [Cloud Strategy](https://ipponusa.com/service/cloud-strategy/)
- [Artificial Intelligence](https://ipponusa.com/service/artificial-intelligence/)
- [Operating Model](https://ipponusa.com/service/operating-model/)
- [Platform Modernization](https://ipponusa.com/service/platform-moderization/)
- [Product Innovation](https://ipponusa.com/service/product-innovation/)

#### Ippon International

- [France](https://fr.ippon.tech/?__hstc=223043268.1036d82247623b329dfefe4e92697801.1714729179243.1714729179243.1714729179243.1&__hssc=223043268.2.1714729179244&__hsfp=803678701)
- [Australia](https://au.ippon.tech/?__hstc=223043268.1036d82247623b329dfefe4e92697801.1714729179243.1714729179243.1714729179243.1&__hssc=223043268.2.1714729179244&__hsfp=803678701)

- #### Contact
- Ippon Technologies  
  [3431 West Leigh Street Richmond, VA 23230, USA](https://maps.app.goo.gl/RySm6SAwZnsxeiLz9)
- [(844) 477- 6687](tel:8444776687)
- [Sales@ipponusa.com](mailto:Sales@ipponusa.com)

<https://www.facebook.com/IpponUSA/> <https://www.youtube.com/c/ipponusa> <https://www.linkedin.com/company/ippon-technology>

©Copyright 2024 Ippon USA. All Rights Reserved.   |   [Terms and Conditions](https://ipponusa.com/privacy-policy/)   |   [Privacy Policy](https://ipponusa.com/privacy-policy/)   |   [Website by Skol Marketing](https://skolmarketing.com/)

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Amine Ouali Alami",
    "url" : "https://blog.ippon.tech/author/amine-ouali-alami"
  },
  "dateModified" : "2024-01-25T17:44:40.106Z",
  "datePublished" : "2021-03-08T17:44:00.000Z",
  "headline" : "Distributed Tracing using AWS X-Ray",
  "image" : [ "https://blog.ippon.tech/hubfs/Imported_Blog_Media/Distributed-Tracing-using-AWS-X-Ray-3.png" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.ippon.tech/distributed-tracing-using-aws-x-ray",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.ippon.tech/hubfs/logo_SVG.svg"
    },
    "name" : "Ippon Technologies"
  }
}
```