Spring Boot 停止AWS XRay跟踪Sping Boot healthcheck端点

rvpgvaaj  于 2023-05-06  发布在  Spring
关注(0)|答案(1)|浏览(124)

我正在使用AWS XRay测试我的Sprint Boot 应用程序,它使用aws-xray-recorder-sdk-spring运行得非常好。然而,我只对用户与我的应用程序的交互感兴趣,而不是系统健康检查,但是我在禁用健康检查的检测方面遇到了麻烦。
根据我对文档的理解,这应该通过@Pointcut进行控制。您可以在下面看到,我已经尝试配置@Pointcut以通过!within(org.springframework.boot.actuate.health.*)排除spring健康检查,但健康检查仍然被检测,正如您可以从下面的XRay调试日志中看到的那样。
有谁知道我是如何做到这一点的?

@Aspect
@Component
public class XRayInspector extends BaseAbstractXRayInterceptor {
    @Override
    @Pointcut("@within(com.amazonaws.xray.spring.aop.XRayEnabled) && !springHealthChecks()")
    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);
    }
}
2023-05-05T14:40:10.710Z DEBUG 8 --- [io-8080-exec-10] com.amazonaws.xray.emitters.UDPEmitter   : {
  "name" : "Test Service",
  "id" : "5bcf87b917b29e69",
  "start_time" : 1.683297610683E9,
  "trace_id" : "1-6455154a-b6544c2518e7bd28a18a3267",
  "end_time" : 1.683297610709E9,
  "http" : {
    "request" : {
      "method" : "GET",
      "client_ip" : "127.0.0.1",
      "url" : "http://localhost:8080/testservice/management/health",
      "user_agent" : "curl/7.88.1"
    },
    "response" : {
      "status" : 200
    }
  },
  "aws" : {
    "xray" : {
      "sdk_version" : "2.9.1",
      "sdk" : "X-Ray for Java"
    }
  },
  "service" : {
    "runtime_version" : "17.0.6",
    "runtime" : "OpenJDK 64-Bit Server VM"
  }
}
6jjcrrmo

6jjcrrmo1#

您可以通过扩展AWSXRayServletFilter.class并配置自己的过滤器来实现这一点。这样,您可以排除不想跟踪的端点
示例代码:

import com.amazonaws.xray.javax.servlet.AWSXRayServletFilter;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class XrayEndpointFilter extends AWSXRayServletFilter {

    private final List<String> ignoredEndpoints;

    XrayEndpointFilter(final List<String> ignoredEndpoints) {
        super("appName");
        this.ignoredEndpoints = ignoredEndpoints;
    }

    @Override
    public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
            throws ServletException, IOException {

        final String uri = ((HttpServletRequest) request).getRequestURI();

        boolean shouldFilter = ignoredEndpoints.stream()
                .map(Pattern::compile)
                .map(pattern -> pattern.matcher(uri))
                .anyMatch(Matcher::find);

        if (shouldFilter) {
            chain.doFilter(request, response);
        } else {
            super.doFilter(request, response, chain);
        }
    }
}

相关问题