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 Antworten
0

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

beantwortet vor 4 Monaten
profile picture
EXPERTE
überprüft vor einem Monat
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
beantwortet vor 2 Monaten
profile picture
EXPERTE
überprüft vor einem Monat

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen