Preparing Productsservice Spring Boot for AWS X-Ray Usage

Preparing Productsservice Spring Boot for AWS X-Ray Usage

  1. In the productsservice project, open the build.gradle file and add the X-Ray SDK dependencies as follows:

    implementation 'com.amazonaws:aws-xray-recorder-sdk-spring:2.14.0'
    implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-v2:2.14.0'
    

Architect

Preparing Productsservice Spring Boot for AWS X-Ray Usage

  1. Inside the config directory, create a new file named XRayConfig.java.

Architect

  1. Add the @Configuration annotation to the XRayConfig class. This annotation in Spring indicates that this class is used to define beans for the Spring application context. Next, create a logger object to record information related to AWS X-Ray operations in the application.
   @Configuration
   public class XRayConfig {
       private static final Logger LOG = LoggerFactory.getLogger(XRayConfig.class);
   }

Architect

  1. Create a constructor XRayConfig() within the XRayConfig class. In this constructor, configure AWS X-Ray as follows:

    • ruleFile: Read the configuration file xray-sampling-rules.json from the resources. This file contains sampling rules for AWS X-Ray, defining how AWS X-Ray should collect data.
    • AWSXRayRecorder: Create an instance of AWSXRayRecorder with default configuration, including default plugins and sampling strategy from the specified file.
    • AWSXRay.setGlobalRecorder(awsxRayRecorder): Set the newly created AWSXRayRecorder as the global recorder for AWS X-Ray.
    • Trapping exceptions: In case the configuration file is not found, catch a FileNotFoundException and log an error message using the logger.
public XRayConfig() {
        try {
            URL ruleFile = ResourceUtils
                    .getURL("classpath:xray/xray-sampling-rules.json");
            
            AWSXRayRecorder awsxRayRecorder = AWSXRayRecorderBuilder.standard()
                    .withDefaultPlugins()
                    .withSamplingStrategy(new CentralizedSamplingStrategy(ruleFile))
                    .build();

            AWSXRay.setGlobalRecorder(awsxRayRecorder);            
        } catch (FileNotFoundException e) {
            LOG.error("XRay config file not found");
        }
    }

Architect

  1. Create the method Bean TracingFilter. This method defines a bean of type Filter using AWSXRayServletFilter.

    • AWSXRayServletFilter: This filter for servlets is used to trace HTTP requests coming into and leaving the application. “productsservice” is the service name used in AWS X-Ray reporting and analysis.
   @Bean
   public Filter tracingFilter() {
       return new AWSXRayServletFilter("productsservice");
   }

Architect

  1. Finally, create an xray-sampling-rules.json file as described earlier. Navigate to the resources directory, create a new folder named xray, and then create a JSON file named xray-sampling-rules.json.

    Architect

  2. Configure the xray-sampling-rules.json file as follows:

{
  "version": 2,
  "default":
  {
    "fixed_target": 0,
    "rate": 1
  },
  "rules": [
    {
      "fixed_target": 0,
      "rate": 0,
      "url_path": "/actuator/health",
      "http_method": "GET",
      "host": "*",
      "description": "Load balancer health check"
    }
  ]
}

Architect