Listen to multiple async operations in ServerHttpObservationFilter
Prior to this commit, the `ServerHttpObservationFilter` was fixed to re-enable instrumentation for async dispatches. This fix involves using an AsyncListener to be notified of exchange completion. This change was incomplete, as this would not work in some cases. If another filter starts the async mode and initiates an ASYNC dispatch, before async handling at the controller level, the async listener is not registered against subsequent async starts. This commit not only ensures that the async listener registers against new async starts, but also ensure that the initial creation and registration only happens during the initial REQUEST dispatch. Fixes gh-33451
This commit is contained in:
@@ -118,13 +118,13 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
throw ex;
|
||||
}
|
||||
finally {
|
||||
// If async is started, register a listener for completion notification.
|
||||
if (request.isAsyncStarted()) {
|
||||
// If async is started during the first dispatch, register a listener for completion notification.
|
||||
if (request.isAsyncStarted() && request.getDispatcherType() == DispatcherType.REQUEST) {
|
||||
request.getAsyncContext().addListener(new ObservationAsyncListener(observation));
|
||||
}
|
||||
// scope is opened for ASYNC dispatches, but the observation will be closed
|
||||
// by the async listener.
|
||||
else if (request.getDispatcherType() != DispatcherType.ASYNC){
|
||||
else if (!isAsyncDispatch(request)) {
|
||||
Throwable error = fetchException(request);
|
||||
if (error != null) {
|
||||
observation.error(error);
|
||||
@@ -168,6 +168,7 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void onStartAsync(AsyncEvent event) {
|
||||
event.getAsyncContext().addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user