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'


@Configuration
public class XRayConfig {
private static final Logger LOG = LoggerFactory.getLogger(XRayConfig.class);
}

Create a constructor XRayConfig() within the XRayConfig class. In this constructor, configure AWS X-Ray as follows:
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");
}
}

Create the method Bean TracingFilter. This method defines a bean of type Filter using AWSXRayServletFilter.
@Bean
public Filter tracingFilter() {
return new AWSXRayServletFilter("productsservice");
}

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.

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"
}
]
}
