Skip to content

Commit 088fb71

Browse files
committed
Ignore container error attribute on async completion
Prior to this commit, `ServerHttpObservationFilter` read the `jakarta.servlet.error.exception` request attribute when an async request completed and reported it to the observation. An exception that escapes an async dispatch has already been reported by the filter or by `AsyncListener#onError`, and the container exposes it wrapped in a `ServletException`. The error was therefore reported once more, and the "exception" key value changed from the root cause to `ServletException`. This commit only reports the exception recorded with `recordHandledException` when the async request completes. Closes gh-37731
1 parent 70e1423 commit 088fb71

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎spring-web/src/main/java/org/springframework/web/filter/ServerHttpObservationFilter.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ public void onTimeout(AsyncEvent event) {
232232

233233
@Override
234234
public void onComplete(AsyncEvent event) {
235-
recordError(this.currentObservation, this.request);
235+
// An exception that escaped was reported by the filter or onError already
236+
Throwable error = (Throwable) this.request.getAttribute(HANDLED_EXCEPTION_ATTRIBUTE);
237+
if (error != null) {
238+
this.currentObservation.error(error);
239+
}
236240
this.currentObservation.stop();
237241
}
238242

‎spring-web/src/test/java/org/springframework/web/filter/ServerHttpObservationFilterTests.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ void shouldNotCloseObservationOnAsyncTimeout() throws Exception {
200200
assertThatHttpObservation().hasLowCardinalityKeyValue("status", "503").hasBeenStopped();
201201
}
202202

203+
@Test
204+
void shouldNotReportErrorAttributeAfterAsyncDispatchFailure() throws Exception {
205+
IllegalStateException error = new IllegalStateException("test error");
206+
ServletException wrapper = new ServletException(error);
207+
this.request.setAsyncSupported(true);
208+
this.request.startAsync();
209+
this.filter.doFilter(this.request, this.response, this.mockFilterChain);
210+
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
211+
212+
this.request.setDispatcherType(DispatcherType.ASYNC);
213+
assertThatThrownBy(() -> this.filter.doFilter(this.request, this.response, (request, response) -> {
214+
throw wrapper;
215+
})).isSameAs(wrapper);
216+
// the container exposes the exception that escaped before completing
217+
this.request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, wrapper);
218+
asyncContext.complete();
219+
220+
assertThatHttpObservation().hasError(error)
221+
.hasLowCardinalityKeyValue("exception", "IllegalStateException");
222+
}
223+
203224
@Test
204225
void shouldNotCloseObservationDuringAsyncDispatch() throws Exception {
205226
this.mockFilterChain = new MockFilterChain(new ScopeCheckingServlet(this.observationRegistry));

0 commit comments

Comments
 (0)