You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A handled exception set on ServerRequestObservationContext does not mark the server span as failed #37658
ServerHttpObservationFilter.findObservationContext(request) lets code that handles an exception record it on the request's observation, and the reference documentation shows that for an exception an @ExceptionHandler method handles. Spring MVC's own resolvers do the same, for example ExceptionHandlerExceptionResolver, DefaultHandlerExceptionResolver and ResponseStatusExceptionResolver.
Setting the error on the context reaches the metrics, because DefaultMeterObservationHandler reads it when the observation stops. It doesn't reach the other handlers: only Observation.error(Throwable) calls ObservationHandler.onError, and that is where Micrometer Tracing's TracingObservationHandler marks the span as failed. Its onStop doesn't look at the context's error. So a handled server error is tagged on http.server.requests, while the server span ends with no error status.
I measured that in Apache Grails, whose exception resolver recorded errors this way, with the OpenTelemetry bridge, on Spring Framework 7.0.10 and 7.1.0-RC2: the span ended UNSET with no exception event. I haven't measured Spring MVC's own resolvers, but they set the error the same way.
Code outside the filter can't call Observation.error reliably. The filter keeps the Observation in a private request attribute. Finding it through ObservationRegistry.getCurrentObservationScope() needs the registry the filter was created with, which isn't necessarily a bean, and the server observation isn't necessarily the innermost scope.
Two ways this could be solved:
Expose the observation as the context is, for example public static Optional<Observation> findObservation(HttpServletRequest request), so that a resolver can call error on it.
Have the filter pass an error set on the context to Observation.error before it stops the observation, which would cover the documented pattern and the existing resolvers without new API.
Apache Grails currently reads the private attribute, ServerHttpObservationFilter.class.getName() + ".observation", to get this right, with specs that fail if it is renamed: apache/grails-core#16879.
Thanks. I've reproduced it with a plain Spring Boot application, with no Grails, using the OpenTelemetry bridge and a SpanProcessor that records the server spans:
Request
Spring Boot 4.2.0-RC2 (Framework 7.1.0-RC2)
4.1.2 (7.0.10)
ResponseStatusException of 500, from ResponseStatusExceptionResolver
metric error=ResponseStatusException, span UNSET
metric error=none, span UNSET
@ExceptionHandler that calls findObservationContext(request).ifPresent(c -> c.setError(ex)), as the reference documentation shows
metric has the error, span UNSET
same
An exception nothing handles (control)
span ERROR, with an exception event
same
So the 5xx recording added in #37384 reaches the metrics but not the span.
A correction to the description: Spring MVC's resolvers don't record what an application's own @ExceptionHandler answers, only the 5xx errors Spring MVC produces itself, as #37384 intends.
If the fix goes into the filter, passing the context's error to Observation.error when it stops the observation, then code that already calls Observation.error itself would be reported twice, with two exception events on the span. Grails does that now, through the private attribute.
Activity
bclozel commentedon Nov 7, 2026
See #37384
codeconsole commentedon Nov 7, 2026
Thanks. I've reproduced it with a plain Spring Boot application, with no Grails, using the OpenTelemetry bridge and a
SpanProcessorthat records the server spans:ResponseStatusExceptionof 500, fromResponseStatusExceptionResolvererror=ResponseStatusException, spanUNSETerror=none, spanUNSET@ExceptionHandlerthat callsfindObservationContext(request).ifPresent(c -> c.setError(ex)), as the reference documentation showsUNSETERROR, with an exception eventSo the 5xx recording added in #37384 reaches the metrics but not the span.
A correction to the description: Spring MVC's resolvers don't record what an application's own
@ExceptionHandleranswers, only the 5xx errors Spring MVC produces itself, as #37384 intends.If the fix goes into the filter, passing the context's error to
Observation.errorwhen it stops the observation, then code that already callsObservation.erroritself would be reported twice, with two exception events on the span. Grails does that now, through the private attribute.Report handled exceptions to the observation