在 Spring Boot 2 中,我的 spring 默认 http 请求指标将包括如下异常和错误标签:
http_server_requests_seconds_count{error="NoResourceFoundException",exception="NoResourceFoundException",method="POST",outcome="SERVER_ERROR",status="500",uri="/my-endpoint",} 4.0
升级到 Spring Boot 3 后,我发现带有错误或异常的 api 调用不会填充这些标签。
如何将这些标签添加回我的指标中?
此功能已在 Spring Boot 3 micrometer 中删除
由于可观察性支持已在 Spring Boot 3.0 中重写,我们无法再支持此请求属性,因为可观察性支持是直接在 Spring Framework 中构建的(并且 ErrorAttributes 存在于 Spring Boot 中)。
https://github.com/spring-projects/spring-framework/issues/31514
您可以通过在异常处理程序中添加一些代码来恢复此功能
@ExceptionHandler
public ResponseEntity<MyResponse> handle(final HttpServletRequest request, final Exception exception) {
// This will add exception tags to your metrics
ServerHttpObservationFilter.findObservationContext(request).ifPresent(context -> context.setError(exception));
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}