我发现有一个老问题Sleuth/Zipkin tracing with @ControllerAdvice,但我遇到了与最新版本相同的问题(spring-cloud-starter-zipkin:2.1.0.RELEASE),我调试它并发现错误为空,所以zipkin只是猜测带状态码。我必须再次抛出异常以使zipkin通知异常
这是完美的意义,它是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";
}
}