避免在 Spring Boot 应用程序中向 Sentry 报告“管道断裂”错误

2022-09-04 03:48:51

我有一个Spring Boot应用程序,它使用Sentry进行异常跟踪,我收到一些如下所示的错误:

ClientAbortExceptionorg.apache.catalina.connector.OutputBuffer in realWriteBytes
errorjava.io.IOException: Broken pipe

我的理解是,这只是一个网络错误,因此我通常应该忽略它们。我想做的是向Librato报告所有其他管道并记录损坏的管道,这样我就可以密切关注我得到的数量(峰值可能意味着客户端存在问题,这也是我用Java开发的):IOExceptions

我想出了这个:

@ControllerAdvice
@Priority(1)
@Order(1)
public class RestExceptionHandler {
    @ExceptionHandler(ClientAbortException.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    public ResponseEntity<?> handleClientAbortException(ClientAbortException ex, HttpServletRequest request) {
        Throwable rootCause = ex;
        while (ex.getCause() != null) {
            rootCause = ex.getCause();
        }
        if (rootCause.getMessage().contains("Broken pipe")) {
            logger.info("count#broken_pipe=1");
        } else {
            Sentry.getStoredClient().sendException(ex);
        }
        return null;
    }
}

这是处理这个问题的可接受的方法吗?

我以这种方式按照文档配置了Sentry:

@Configuration
public class FactoryBeanAppConfig {
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new SentryExceptionResolver();
    }

    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new SentryServletContextInitializer();
    }
}

答案 1

如果你看一下这门课SentryExceptionResolver

public class SentryExceptionResolver implements HandlerExceptionResolver, Ordered {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception ex) {

        Sentry.capture(ex);

        // null = run other HandlerExceptionResolvers to actually handle the exception
        return null;
    }

    @Override
    public int getOrder() {
        // ensure this resolver runs first so that all exceptions are reported
        return Integer.MIN_VALUE;
    }
}

通过返回,它确保它首先被调用。即使您已将 设置为 ,它也不起作用。所以你想改变你的Integer.MIN_VALUEgetOrderPriority1

@Configuration
public class FactoryBeanAppConfig {
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new SentryExceptionResolver();
    }

    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new SentryServletContextInitializer();
    }
}

@Configuration
public class FactoryBeanAppConfig {
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new SentryExceptionResolver() {
                @Override
                public int getOrder() {
                    // ensure we can get some resolver earlier than this
                    return 10;
                }
         };
    }

    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new SentryServletContextInitializer();
    }
}

这将确保您可以更早地运行处理程序。在代码中,要获取的循环不正确rootCause

while (ex.getCause() != null) {
    rootCause = ex.getCause();
}

这是一个无限循环,因为您使用了 而不是 。即使你纠正它,它仍然可以成为一个无限循环。当异常原因返回自身时,它将被卡住。我还没有彻底测试过它,但我相信它应该像下面这样exrootCause

while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
    rootCause = rootCause.getCause();
}

这是解决问题的一种方法。但是您需要自己将异常发送到哨兵。因此,还有另一种方法可以处理您的要求

方式 2

在这种情况下,您可以在配置中执行整个逻辑并将其更改为以下内容

@Configuration
public class FactoryBeanAppConfig {
    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new SentryExceptionResolver() {
            @Override
            public ModelAndView resolveException(HttpServletRequest request,
                    HttpServletResponse response,
                    Object handler,
                    Exception ex) {
                Throwable rootCause = ex;

                while (rootCause .getCause() != null && rootCause.getCause() != rootCause) {
                    rootCause = rootCause.getCause();
                }

                if (!rootCause.getMessage().contains("Broken pipe")) {
                    super.resolveException(request, response, handler, ex);
                }
                return null;
            }   

        };
    }

    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new SentryServletContextInitializer();
    }
}

答案 2

推荐