我们已经设置了一个 spring-boot 服务,并且我们想通过异常类型绘制 4xx/5xx 的量。
下面是我们正在使用的 Prom-QL :-
sum by (exception,uri,status) (increase(http_server_requests_seconds_count{kubernetes_namespace=~"$namespace",uri!~".*health.*|.*actuator.*|.*error.*",status=~"5.*"})[1m])
我们遇到的问题是,异常类型我们总是以 None 的形式出现。在指标中。
控制器:-
@PostMapping(APIPath.Endpoint)
public ResponseEntity<Response> predictRTO(@RequestBody @Valid request, Errors errors) {
try {
if (errors.hasErrors()) {
throw new RequestParamsException(errors.getAllErrors());
}
Response response = service.getResponse(request);
return ResponseEntity.ok(response);
} catch (RequestParamsException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Response.builder().errors(e.getErrors()).build());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Response.builder().errors(Collections.singletonList(
CommonConstants.INTERNAL_SERVER_ERROR)).build());
}
}
我尝试过点击
actuator/prometheus
,我们也得到了 5xx/4xx,但 None
是一个例外。
http_server_requests_seconds_bucket{exception="None",method="POST",outcome="SERVER_ERROR",status="500",uri="/endpoint",le="0.111848106",} 0.0
http_server_requests_seconds_bucket{exception="None",method="POST",outcome="CLIENT_ERROR",status="400",uri="/v2/prod/predict-rto-preorder-prod",le="3.22122547",} 19.0
我们正在控制器层捕获所有异常,也许这些是 没有传播到微米,有办法解决这个问题吗?
您可能会遇到此问题:https://github.com/spring-projects/spring-framework/issues/29848
如果是这种情况,您应该在 Spring 端处理错误并在那里发出错误信号,例如:
@ControllerAdvice
public class CommonExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Throwable.class)
ProblemDetail onThrowable(HttpServletRequest request, Throwable error) {
ServerHttpObservationFilter.findObservationContext(request)
.ifPresent(context -> context.setError(error));
return ...;
}
}
CommonExceptionHandler
。
(如果这是您的用例,您也可以将视图作为 String
返回。)
此外,Spring MVC 的检测包含
outcome
标签,它直接映射到 HTTP 状态代码(请参阅 HttpStatus
):
INFORMATIONAL: 1xx
SUCCESSFUL: 2xx
REDIRECTION: 3xx
CLIENT_ERROR: 4xx
SERVER_ERROR: 5xx
因此您可以这样做并在不同的图表上看到不同的结果:
sum(rate(http_server_requests_seconds_count[5m])) by (outcome)