|
21 | 21 | import io.micrometer.observation.ObservationRegistry;
|
22 | 22 | import io.micrometer.observation.tck.TestObservationRegistry;
|
23 | 23 | import io.micrometer.observation.tck.TestObservationRegistryAssert;
|
| 24 | +import jakarta.servlet.AsyncContext; |
24 | 25 | import jakarta.servlet.AsyncEvent;
|
25 | 26 | import jakarta.servlet.AsyncListener;
|
26 | 27 | import jakarta.servlet.DispatcherType;
|
| 28 | +import jakarta.servlet.Filter; |
| 29 | +import jakarta.servlet.FilterChain; |
27 | 30 | import jakarta.servlet.RequestDispatcher;
|
28 | 31 | import jakarta.servlet.ServletException;
|
| 32 | +import jakarta.servlet.ServletRequest; |
| 33 | +import jakarta.servlet.ServletResponse; |
29 | 34 | import jakarta.servlet.http.HttpServlet;
|
30 | 35 | import jakarta.servlet.http.HttpServletRequest;
|
31 | 36 | import jakarta.servlet.http.HttpServletResponse;
|
@@ -139,6 +144,21 @@ void shouldCloseObservationAfterAsyncCompletion() throws Exception {
|
139 | 144 | assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS").hasBeenStopped();
|
140 | 145 | }
|
141 | 146 |
|
| 147 | + @Test |
| 148 | + void shouldRegisterListenerForAsyncStarts() throws Exception { |
| 149 | + CustomAsyncFilter customAsyncFilter = new CustomAsyncFilter(); |
| 150 | + this.mockFilterChain = new MockFilterChain(new NoOpServlet(), customAsyncFilter); |
| 151 | + this.request.setAsyncSupported(true); |
| 152 | + this.request.setDispatcherType(DispatcherType.REQUEST); |
| 153 | + this.filter.doFilter(this.request, this.response, this.mockFilterChain); |
| 154 | + customAsyncFilter.asyncContext.dispatch(); |
| 155 | + this.request.setDispatcherType(DispatcherType.ASYNC); |
| 156 | + AsyncContext newAsyncContext = this.request.startAsync(); |
| 157 | + newAsyncContext.complete(); |
| 158 | + |
| 159 | + assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS").hasBeenStopped(); |
| 160 | + } |
| 161 | + |
142 | 162 | @Test
|
143 | 163 | void shouldCloseObservationAfterAsyncError() throws Exception {
|
144 | 164 | this.request.setAsyncSupported(true);
|
@@ -187,4 +207,26 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
|
187 | 207 | }
|
188 | 208 | }
|
189 | 209 |
|
| 210 | + @SuppressWarnings("serial") |
| 211 | + static class NoOpServlet extends HttpServlet { |
| 212 | + |
| 213 | + @Override |
| 214 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 215 | + |
| 216 | + } |
| 217 | + |
| 218 | + } |
| 219 | + |
| 220 | + static class CustomAsyncFilter implements Filter { |
| 221 | + |
| 222 | + AsyncContext asyncContext; |
| 223 | + |
| 224 | + @Override |
| 225 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { |
| 226 | + this.asyncContext = servletRequest.startAsync(); |
| 227 | + filterChain.doFilter(servletRequest, servletResponse); |
| 228 | + } |
| 229 | + |
| 230 | + } |
| 231 | + |
190 | 232 | }
|
0 commit comments