Skip to content

A handled exception set on ServerRequestObservationContext does not mark the server span as failed #37658

Closed

Description

@codeconsole

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.

Activity

  1. added
    status: waiting-for-triage
    on Nov 7, 2026
  2. self-assigned this
    on Nov 7, 2026
  3. added
    type: bug
    in: web
    and removed
    status: waiting-for-triage
    on Nov 7, 2026
  4. added this to the 7.1.0 milestone on Nov 7, 2026
  5. bclozel commented on Nov 7, 2026

    @bclozel
    Member

    See #37384

  6. codeconsole commented on Nov 7, 2026

    @codeconsole
    Author

    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.

  7. added a commit that references this issue on Nov 10, 2026
    297c74d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions