Package org.springframework.web.filter

# Class ServerHttpObservationFilter

java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.web.filter.ServerHttpObservationFilter

All Implemented Interfaces:
  `Filter`, `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.DisposableBean`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.EnvironmentAware`, `org.springframework.core.env.EnvironmentCapable`, `ServletContextAware`

public class ServerHttpObservationFilter extends OncePerRequestFilter
`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

Fields
Modifier and Type
Field
Description
`static final String`
`CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE`
Name of the request attribute holding the `context` for the current observation.

### Fields inherited from class org.springframework.web.filter.OncePerRequestFilter

`ALREADY_FILTERED_SUFFIX`

### Fields inherited from class org.springframework.web.filter.GenericFilterBean

`logger`

## Constructor Summary

Constructors
Constructor
Description
`ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry)`
Create an `HttpRequestsObservationFilter` that records observations against the given `ObservationRegistry`.
`ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry, ServerRequestObservationConvention observationConvention)`
Create an `HttpRequestsObservationFilter` that records observations against the given `ObservationRegistry` with a custom convention.

## Method Summary

Modifier and Type
Method
Description
`protected void`
`doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread.
`static Optional<ServerRequestObservationContext>`
`findObservationContext(HttpServletRequest request)`
Get the current `observation context` from the given request, if available.
`protected void`
`onScopeOpened(io.micrometer.observation.Observation.Scope scope, HttpServletRequest request, HttpServletResponse response)`
Notify this filter that a new `Observation.Scope` is opened for the observation that was just created.
`protected boolean`
`shouldNotFilterAsyncDispatch()`
The dispatcher type `jakarta.servlet.DispatcherType.ASYNC` means a filter can be invoked in more than one thread over the course of a single request.

### Methods inherited from class org.springframework.web.filter.OncePerRequestFilter

`doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterErrorDispatch`

### Methods inherited from class org.springframework.web.filter.GenericFilterBean

`addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE

public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE
Name of the request attribute holding the `context` for the current observation.

## Constructor Details

### ServerHttpObservationFilter

public ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry)
Create an `HttpRequestsObservationFilter` that records observations against the given `ObservationRegistry`. The default `convention` will be used.

Parameters:
  `observationRegistry` - the registry to use for recording observations

### ServerHttpObservationFilter

public ServerHttpObservationFilter(io.micrometer.observation.ObservationRegistry observationRegistry, ServerRequestObservationConvention observationConvention)
Create an `HttpRequestsObservationFilter` that records observations against the given `ObservationRegistry` with a custom convention.

Parameters:
  `observationRegistry` - the registry to use for recording observations
  `observationConvention` - the convention to use for all recorded observations

## Method Details

### findObservationContext

public static Optional<ServerRequestObservationContext> findObservationContext(HttpServletRequest request)
Get the current `observation context` from the given request, if available.

Parameters:
  `request` - the current request
Returns:
  the current observation context

### shouldNotFilterAsyncDispatch

protected boolean shouldNotFilterAsyncDispatch()
Description copied from class: `OncePerRequestFilter`
The dispatcher type `jakarta.servlet.DispatcherType.ASYNC` means 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.xml` or in Java through the `ServletContext`, 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:
  `shouldNotFilterAsyncDispatch` in class `OncePerRequestFilter`

### doFilterInternal

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
Description copied from class: `OncePerRequestFilter`
Same contract as for `doFilter`, but guaranteed to be just invoked once per request within a single request thread. See `OncePerRequestFilter.shouldNotFilterAsyncDispatch()` for details.

Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.

Specified by:
  `doFilterInternal` in class `OncePerRequestFilter`
Throws:
  `ServletException`
  `IOException`

### onScopeOpened

protected void onScopeOpened(io.micrometer.observation.Observation.Scope scope, HttpServletRequest request, HttpServletResponse response)
Notify this filter that a new `Observation.Scope` is opened for the observation that was just created.

Parameters:
  `scope` - the newly opened observation scope
  `request` - the HTTP client request
  `response` - the filter's response
Since:
  6.2