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.

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
전문가
검토됨 한 달 전
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
전문가
검토됨 한 달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠