如何停止AWS XRay追踪Spring Boot健康检查端点?
解决AWS XRay禁用Spring Boot健康检查追踪的问题
你的核心问题在于:当前的Aspect配置仅针对带有@XRayEnabled注解的业务类,但健康检查请求的追踪来自XRay默认的XrayServletFilter——这个过滤器会拦截所有HTTP请求(包括Actuator健康端点),因此你的Aspect排除规则对它完全无效。
下面提供两种可行的解决方案:
方案1:配置XRay过滤器忽略健康检查路径(推荐)
这是最直接的方式,通过配置让XRay过滤器跳过健康检查路径:
方式A:通过配置文件实现
在application.properties中添加:
# 替换为你的健康检查实际路径,支持通配符匹配 aws.xray.servlet.filter.ignored-urls=/testservice/management/health,/actuator/health/**
方式B:自定义过滤器注册Bean
如果需要更灵活的配置逻辑,可以手动注册过滤器并设置忽略规则:
import com.amazonaws.xray.javax.servlet.AWSXRayServletFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class XRayFilterConfig { @Bean public FilterRegistrationBean<AWSXRayServletFilter> xrayServletFilter() { FilterRegistrationBean<AWSXRayServletFilter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new AWSXRayServletFilter()); // 设置需要忽略的URL,多个路径用逗号分隔 registrationBean.addInitParameter("ignoredUrls", "/testservice/management/health,/actuator/health/**"); // 确保过滤器优先执行 registrationBean.setOrder(1); return registrationBean; } }
方案2:禁用默认Servlet过滤器,仅依赖Aspect追踪
如果只想通过自定义Aspect控制追踪范围,可以先关闭XRay的默认过滤器,确保只有标记@XRayEnabled的类/方法被追踪:
步骤1:禁用自动配置的过滤器
在application.properties中添加:
spring.autoconfigure.exclude=com.amazonaws.xray.spring.boot.autoconfigure.XRayServletAutoConfiguration
步骤2:保留并验证Aspect配置
你的现有Aspect配置可以继续使用,确保所有需要追踪的业务类都添加了@XRayEnabled注解:
@Aspect @Component public class XRayInspector extends BaseAbstractXRayInterceptor { @Override @Pointcut("@within(com.amazonaws.xray.spring.aop.XRayEnabled) && !within(org.springframework.boot.actuate.health.*)") public void xrayEnabledClasses() { } @Pointcut("within(org.springframework.boot.actuate.health.*)") public void springHealthChecks() { } @Override protected Map<String, Map<String, Object>> generateMetadata(ProceedingJoinPoint proceedingJoinPoint, Subsegment subsegment) { return super.generateMetadata(proceedingJoinPoint, subsegment); } }
注意:这种方式仅适用于所有用户交互相关的业务类都已标记@XRayEnabled的场景,否则会遗漏部分请求追踪。
修改完成后重启应用,再次调用健康检查端点,查看XRay日志即可确认追踪已被禁用。
内容的提问来源于stack exchange,提问作者Verbitan
相关产品推荐
相关产品推荐

