|
|
|
@ -55,19 +55,23 @@ import org.springframework.http.server.observation.ServerRequestObservationConve |
|
|
|
public class ServerHttpObservationFilter extends OncePerRequestFilter { |
|
|
|
public class ServerHttpObservationFilter extends OncePerRequestFilter { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Name of the request attribute holding the {@link ServerRequestObservationContext context} for the current observation. |
|
|
|
* Name of the request attribute holding the {@link ServerRequestObservationContext} for the current observation. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ServerHttpObservationFilter.class.getName() + ".context"; |
|
|
|
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = |
|
|
|
|
|
|
|
ServerHttpObservationFilter.class.getName() + ".context"; |
|
|
|
|
|
|
|
|
|
|
|
private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = new DefaultServerRequestObservationConvention(); |
|
|
|
private static final String CURRENT_OBSERVATION_ATTRIBUTE = |
|
|
|
|
|
|
|
ServerHttpObservationFilter.class.getName() + ".observation"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String CURRENT_OBSERVATION_ATTRIBUTE = ServerHttpObservationFilter.class.getName() + ".observation"; |
|
|
|
private static final ServerRequestObservationConvention DEFAULT_OBSERVATION_CONVENTION = |
|
|
|
|
|
|
|
new DefaultServerRequestObservationConvention(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final ObservationRegistry observationRegistry; |
|
|
|
private final ObservationRegistry observationRegistry; |
|
|
|
|
|
|
|
|
|
|
|
private final ServerRequestObservationConvention observationConvention; |
|
|
|
private final ServerRequestObservationConvention observationConvention; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create an {@code HttpRequestsObservationFilter} that records observations |
|
|
|
* Create an {@code HttpRequestsObservationFilter} that records observations |
|
|
|
* against the given {@link ObservationRegistry}. The default |
|
|
|
* against the given {@link ObservationRegistry}. The default |
|
|
|
@ -89,14 +93,6 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter { |
|
|
|
this.observationConvention = observationConvention; |
|
|
|
this.observationConvention = observationConvention; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Get the current {@link ServerRequestObservationContext observation context} from the given request, if available. |
|
|
|
|
|
|
|
* @param request the current request |
|
|
|
|
|
|
|
* @return the current observation context |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request) { |
|
|
|
|
|
|
|
return Optional.ofNullable((ServerRequestObservationContext) request.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected boolean shouldNotFilterAsyncDispatch() { |
|
|
|
protected boolean shouldNotFilterAsyncDispatch() { |
|
|
|
@ -150,8 +146,9 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter { |
|
|
|
Observation observation = (Observation) request.getAttribute(CURRENT_OBSERVATION_ATTRIBUTE); |
|
|
|
Observation observation = (Observation) request.getAttribute(CURRENT_OBSERVATION_ATTRIBUTE); |
|
|
|
if (observation == null) { |
|
|
|
if (observation == null) { |
|
|
|
ServerRequestObservationContext context = new ServerRequestObservationContext(request, response); |
|
|
|
ServerRequestObservationContext context = new ServerRequestObservationContext(request, response); |
|
|
|
observation = ServerHttpObservationDocumentation.HTTP_SERVLET_SERVER_REQUESTS.observation(this.observationConvention, |
|
|
|
observation = ServerHttpObservationDocumentation.HTTP_SERVLET_SERVER_REQUESTS.observation( |
|
|
|
DEFAULT_OBSERVATION_CONVENTION, () -> context, this.observationRegistry).start(); |
|
|
|
this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> context, this.observationRegistry) |
|
|
|
|
|
|
|
.start(); |
|
|
|
request.setAttribute(CURRENT_OBSERVATION_ATTRIBUTE, observation); |
|
|
|
request.setAttribute(CURRENT_OBSERVATION_ATTRIBUTE, observation); |
|
|
|
if (!observation.isNoop()) { |
|
|
|
if (!observation.isNoop()) { |
|
|
|
request.setAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observation.getContext()); |
|
|
|
request.setAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE, observation.getContext()); |
|
|
|
@ -160,14 +157,32 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter { |
|
|
|
return observation; |
|
|
|
return observation; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static @Nullable Throwable unwrapServletException(Throwable ex) { |
|
|
|
|
|
|
|
return (ex instanceof ServletException) ? ex.getCause() : ex; |
|
|
|
/** |
|
|
|
|
|
|
|
* Get the current {@link ServerRequestObservationContext observation context} from the given request, if available. |
|
|
|
|
|
|
|
* @param request the current request |
|
|
|
|
|
|
|
* @return the current observation context |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request) { |
|
|
|
|
|
|
|
return Optional.ofNullable( |
|
|
|
|
|
|
|
(ServerRequestObservationContext) request.getAttribute(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static @Nullable Throwable fetchException(ServletRequest request) { |
|
|
|
static @Nullable Throwable fetchException(ServletRequest request) { |
|
|
|
return (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); |
|
|
|
return (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Throwable unwrapServletException(Throwable ex) { |
|
|
|
|
|
|
|
if (ex instanceof ServletException) { |
|
|
|
|
|
|
|
Throwable cause = ex.getCause(); |
|
|
|
|
|
|
|
if (cause != null) { |
|
|
|
|
|
|
|
return cause; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return ex; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class ObservationAsyncListener implements AsyncListener { |
|
|
|
private static class ObservationAsyncListener implements AsyncListener { |
|
|
|
|
|
|
|
|
|
|
|
private final Observation currentObservation; |
|
|
|
private final Observation currentObservation; |
|
|
|
@ -195,7 +210,6 @@ public class ServerHttpObservationFilter extends OncePerRequestFilter { |
|
|
|
public void onError(AsyncEvent event) { |
|
|
|
public void onError(AsyncEvent event) { |
|
|
|
this.currentObservation.error(unwrapServletException(event.getThrowable())); |
|
|
|
this.currentObservation.error(unwrapServletException(event.getThrowable())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|