spring-bootprometheusspring-micrometer

Spring Boot 3 micrometer http server requests metrics do not include exception and error tags


In Spring Boot 2 my spring default http requests metric would include exception and error tags like this:

http_server_requests_seconds_count{error="NoResourceFoundException",exception="NoResourceFoundException",method="POST",outcome="SERVER_ERROR",status="500",uri="/my-endpoint",} 4.0

Having upgraded to Spring Boot 3, I have found that api calls with errors or exceptions do not populate these tags.

How can I add these tags back into my metrics?


Solution

  • This functionality has been removed in Spring Boot 3 micrometer

    Since the Observability support has been rewritten in Spring Boot 3.0, we cannot support this request attribute anymore since observability support is built in Spring Framework directly (and ErrorAttributes lives in Spring Boot).

    https://github.com/spring-projects/spring-framework/issues/31514

    You can get this functionality back by adding some code into you exception handler

    @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);
    }