带有ControllerAdvice的侦听/ zipkin无法正常工作

时间:2019-02-12 08:31:42

标签: spring-cloud-sleuth zipkin exceptionhandler

我发现有一个旧问题Sleuth/Zipkin tracing with @ControllerAdvice,但是我在最新版本(spring-cloud-starter-zipkin:2.1.0.RELEASE)中遇到了同样的问题,我对其进行了调试并发现错误为null,因此zipkin只能猜测statuscode。我必须再次抛出异常,以使zipkin通知异常

error is null

zipkin result

ControllerAdvice

throw the exception again, it works

1 个答案:

答案 0 :(得分:0)

完全是null是很有意义的。那是因为您可以控制捕获的异常的处理方式。在您的情况下,什么也不会导致您吞下该异常。

如果您想做得更好,只需通过SpanCustomizer手动添加错误标记。这样,您可以将异常添加到给定的跨度中。然后它将自动关闭并报告给Zipkin(当然,您还可以做其他事ex.toString()

@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ExceptionHanders {

    private final SpanCustomizer customizer;

    public ExceptionHanders(SpanCustomizer customizer) {
        this.customizer = customizer;
    }

    @ExceptionHandler({RuntimeException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleRuntimeException(Exception ex) throws Exception {
        this.customizer.tag("error", ex.toString());
        return "testabcd";
    }
}
相关问题