AWS X Ray Configuration to not track one endpoint

0

I have an application that uses AWS X-Ray. One of my endpoints is a GET request only for checking the health status. I don't want this endpoint to be monitored by AWS X-Ray. How can I achieve this?

This is my web config:

package com.poc.clientserviceapi.config;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.AWSXRayRecorderBuilder;
import com.amazonaws.xray.jakarta.servlet.AWSXRayServletFilter;
import com.amazonaws.xray.strategy.jakarta.SegmentNamingStrategy;
import com.amazonaws.xray.strategy.sampling.LocalizedSamplingStrategy;
import jakarta.servlet.Filter;
import com.amazonaws.xray.slf4j.SLF4JSegmentListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.URL;

@Configuration
public class WebConfig {
    @Bean
    public Filter TracingFilter() {
        AWSXRayRecorderBuilder builder = AWSXRayRecorderBuilder
                .standard().withSegmentListener(new SLF4JSegmentListener("CUSTOM-PREFIX"));

        URL ruleFile = WebConfig.class.getResource("/sampling-rules.json");
        builder.withSamplingStrategy(new LocalizedSamplingStrategy(ruleFile));

        AWSXRay.setGlobalRecorder(builder.build());

        return new AWSXRayServletFilter(SegmentNamingStrategy.dynamic("Scorekeep"));
    }
}

I have already tried this:

package com.poc.clientserviceapi.config;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.AWSXRayRecorderBuilder;
import com.amazonaws.xray.jakarta.servlet.AWSXRayServletFilter;
import com.amazonaws.xray.strategy.sampling.LocalizedSamplingStrategy;
import jakarta.servlet.*;
import com.amazonaws.xray.slf4j.SLF4JSegmentListener;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.URL;

@Configuration
public class WebConfig {

    @Bean
    public Filter TracingFilter() {
        AWSXRayRecorderBuilder builder = AWSXRayRecorderBuilder
                .standard().withSegmentListener(new SLF4JSegmentListener("CUSTOM-PREFIX"));

        URL ruleFile = WebConfig.class.getResource("/sampling-rules.json");
        builder.withSamplingStrategy(new LocalizedSamplingStrategy(ruleFile));

        AWSXRay.setGlobalRecorder(builder.build());

        return new MyAWSXRayServletFilter();
    }

    @WebFilter("/*")
    public static class MyAWSXRayServletFilter implements Filter {

        private final String ignoredEndpointPath = "/sqs/health";

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
                throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) servletRequest;

            // Ignore the specified endpoint
            if (!request.getRequestURI().equals(ignoredEndpointPath)) {
                AWSXRayServletFilter tracingFilter = new AWSXRayServletFilter();
                tracingFilter.doFilter(servletRequest, servletResponse, filterChain);
            } else {
                filterChain.doFilter(servletRequest, servletResponse);
            }
        }

        @Override
        public void destroy() {
        }
    }
}

but then I starting to face another types of issues, as:

java.lang.RuntimeException: The AWSXRayServletFilter requires either a fixedName init-param or an instance of SegmentNamingStrategy. Add an init-param tag to the AWSXRayServletFilter's declaration in web.xml, using param-name: 'fixedName'. Alternatively, pass an instance of SegmentNamingStrategy to the AWSXRayServletFilter constructor.

Larissa
已提問 4 個月前檢視次數 271 次
2 個答案
0

You can rule out health check by configuring sampling rules https://docs.aws.amazon.com/xray/latest/devguide/xray-console-sampling.html

已回答 4 個月前
profile picture
專家
已審閱 1 個月前
0

I use next part in my x-ray config.yaml for similar goal:

processors:
  filter/drophttp:
    error_mode: ignore
    traces:
      span:
        - attributes["http.user_agent"] == "ELB-HealthChecker/2.0"

check other attributes for filter/drophttp if needed. To apply this config I just updated awsxray provided collector docker image with COPY of this config.

FROM public.ecr.aws/aws-observability/aws-otel-collector:latest
COPY ./my-otel-config.yaml /etc/ecs/otel-config.yaml
已回答 2 個月前
profile picture
專家
已審閱 1 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南