Class ServerHttpObservationFilter
- All Implemented Interfaces:
jakarta.servlet.Filter, Aware, BeanNameAware, DisposableBean, InitializingBean, EnvironmentAware, EnvironmentCapable, ServletContextAware
Filter that creates observations
for HTTP exchanges. This collects information about the execution time and
information gathered from the ServerRequestObservationContext.
web Frameworks can fetch the current context
as a request attribute and contribute
additional information to it.
The configured ServerRequestObservationConvention will use this context to collect
metadata and attach it to the observation.
- Since:
- 6.0
- Author:
- Brian Clozel
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringName of the request attribute holding theServerRequestObservationContextfor the current observation.Fields inherited from class OncePerRequestFilter
ALREADY_FILTERED_SUFFIXFields inherited from class GenericFilterBean
logger -
Constructor Summary
ConstructorsConstructorDescriptionServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry) Create anHttpRequestsObservationFilterthat records observations against the givenObservationRegistry.ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry, ServerRequestObservationConvention observationConvention) Create anHttpRequestsObservationFilterthat records observations against the givenObservationRegistrywith a custom convention. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) Same contract as fordoFilter, but guaranteed to be just invoked once per request within a single request thread.findObservationContext(jakarta.servlet.http.HttpServletRequest request) Get the currentobservation contextfrom the given request, if available.protected voidonScopeOpened(io.micrometer.observation.Observation.Scope scope, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Notify this filter that a newObservation.Scopeis opened for the observation that was just created.protected booleanThe dispatcher typejakarta.servlet.DispatcherType.ASYNCmeans a filter can be invoked in more than one thread over the course of a single request.Methods inherited from class OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterErrorDispatchMethods inherited from class GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
-
Field Details
-
CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE
Name of the request attribute holding theServerRequestObservationContextfor the current observation.
-
-
Constructor Details
-
ServerHttpObservationFilter
public ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry) Create anHttpRequestsObservationFilterthat records observations against the givenObservationRegistry. The defaultconventionwill be used.- Parameters:
observationRegistry- the registry to use for recording observations
-
ServerHttpObservationFilter
public ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry, ServerRequestObservationConvention observationConvention) Create anHttpRequestsObservationFilterthat records observations against the givenObservationRegistrywith a custom convention.- Parameters:
observationRegistry- the registry to use for recording observationsobservationConvention- the convention to use for all recorded observations
-
-
Method Details
-
shouldNotFilterAsyncDispatch
protected boolean shouldNotFilterAsyncDispatch()Description copied from class:OncePerRequestFilterThe dispatcher typejakarta.servlet.DispatcherType.ASYNCmeans a filter can be invoked in more than one thread over the course of a single request. Some filters only need to filter the initial thread (for example, request wrapping) while others may need to be invoked at least once in each additional thread for example for setting up thread locals or to perform final processing at the very end.Note that although a filter can be mapped to handle specific dispatcher types via
web.xmlor in Java through theServletContext, servlet containers may enforce different defaults with respect to dispatcher types. This flag enforces the design intent of the filter.The default return value is "true", which means the filter will not be invoked during subsequent async dispatches. If "false", the filter will be invoked during async dispatches with the same guarantees of being invoked only once during a request within a single thread.
- Overrides:
shouldNotFilterAsyncDispatchin classOncePerRequestFilter
-
doFilterInternal
protected void doFilterInternal(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, jakarta.servlet.FilterChain filterChain) throws jakarta.servlet.ServletException, IOException Description copied from class:OncePerRequestFilterSame contract as fordoFilter, but guaranteed to be just invoked once per request within a single request thread. SeeOncePerRequestFilter.shouldNotFilterAsyncDispatch()for details.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
- Specified by:
doFilterInternalin classOncePerRequestFilter- Throws:
jakarta.servlet.ServletExceptionIOException
-
onScopeOpened
protected void onScopeOpened(io.micrometer.observation.Observation.Scope scope, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Notify this filter that a newObservation.Scopeis opened for the observation that was just created.- Parameters:
scope- the newly opened observation scoperequest- the HTTP client requestresponse- the filter's response- Since:
- 6.2
-
findObservationContext
public static Optional<ServerRequestObservationContext> findObservationContext(jakarta.servlet.http.HttpServletRequest request) Get the currentobservation contextfrom the given request, if available.- Parameters:
request- the current request- Returns:
- the current observation context
-