public abstract class RequestTracingFilter
extends java.lang.Object
implements javax.servlet.Filter
TraceHeaders.TRACE_ID on the response. This is designed to only run once per request.
NOTE: This is abstract because you'll need to override getUserIdHeaderKeys() if your service is expecting user ID header(s), and because the only-runs-once logic is
dependent on whether you're in an async request environment or not, and more particularly whether or not you're in the middle of an async dispatch. If you're not in an
async request environment then you can just use RequestTracingFilterNoAsync for your filter.| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
ERROR_REQUEST_URI_ATTRIBUTE
Corresponds to
RequestDispatcher.ERROR_REQUEST_URI. |
static java.lang.String |
FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE
This attribute key will be set to a value of true via
ServletRequest.setAttribute(String, Object) the first time this filter is run for any given request. |
static java.lang.String |
USER_ID_HEADER_KEYS_LIST_INIT_PARAM_NAME
The param name for the "list of user ID header keys" init param for this filter.
|
protected java.util.List<java.lang.String> |
userIdHeaderKeysFromInitParam |
| Constructor and Description |
|---|
RequestTracingFilter() |
| Modifier and Type | Method and Description |
|---|---|
void |
destroy() |
void |
doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain filterChain)
Wrapper around
doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain) to make sure this filter's logic is only executed once per request. |
protected void |
doFilterInternal(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain filterChain)
Performs the distributed tracing work for each request's overall span.
|
protected java.util.List<java.lang.String> |
getUserIdHeaderKeys()
The list of header keys that will be used to search the request headers for a user ID to set on the
Span for the request. |
void |
init(javax.servlet.FilterConfig filterConfig) |
protected abstract boolean |
isAsyncDispatch(javax.servlet.http.HttpServletRequest request)
The dispatcher type
javax.servlet.DispatcherType.ASYNC introduced in Servlet 3.0 means a filter can be invoked in more than one thread over
the course of a single request. |
protected boolean |
skipDispatch(javax.servlet.http.HttpServletRequest request) |
public static final java.lang.String FILTER_HAS_ALREADY_EXECUTED_ATTRIBUTE
ServletRequest.setAttribute(String, Object) the first time this filter is run for any given request.
This filter will then see this attribute on any subsequent executions with the same request and continue the filter chain without executing this filter's logic
to make sure this filter's logic is only executed once per request.public static final java.lang.String ERROR_REQUEST_URI_ATTRIBUTE
RequestDispatcher.ERROR_REQUEST_URI. This will be populated in the request attributes during an error dispatch.public static final java.lang.String USER_ID_HEADER_KEYS_LIST_INIT_PARAM_NAME
HttpSpanFactory.fromHttpServletRequest(HttpServletRequest, List) or HttpSpanFactory.getUserIdFromHttpServletRequest(HttpServletRequest, List).
The value for this init param is expected to be a comma-delimited list.protected java.util.List<java.lang.String> userIdHeaderKeysFromInitParam
public void init(javax.servlet.FilterConfig filterConfig)
throws javax.servlet.ServletException
init in interface javax.servlet.Filterjavax.servlet.ServletExceptionpublic void destroy()
destroy in interface javax.servlet.Filterpublic void doFilter(javax.servlet.ServletRequest request,
javax.servlet.ServletResponse response,
javax.servlet.FilterChain filterChain)
throws java.io.IOException,
javax.servlet.ServletException
doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain) to make sure this filter's logic is only executed once per request.doFilter in interface javax.servlet.Filterjava.io.IOExceptionjavax.servlet.ServletExceptionprotected void doFilterInternal(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
javax.servlet.FilterChain filterChain)
throws javax.servlet.ServletException,
java.io.IOException
javax.servlet.ServletExceptionjava.io.IOExceptionprotected boolean skipDispatch(javax.servlet.http.HttpServletRequest request)
doFilterInternal(HttpServletRequest, HttpServletResponse, FilterChain) should be skipped, false otherwise.protected abstract boolean isAsyncDispatch(javax.servlet.http.HttpServletRequest request)
javax.servlet.DispatcherType.ASYNC introduced in Servlet 3.0 means a filter can be invoked in more than one thread over
the course of a single request. This method should return true if the filter is currently executing within an asynchronous dispatch.request - the current requestprotected java.util.List<java.lang.String> getUserIdHeaderKeys()
Span for the request. The user ID header keys will be searched
in list order, and the first non-empty user ID header value found will be used as the Span.getUserId(). You can safely return null or an empty list for this method
if there is no user ID to extract; if you return null/empty then the request span's Span.getUserId() will be null.
By default this method will return the list specified via the USER_ID_HEADER_KEYS_LIST_INIT_PARAM_NAME init param, or null if that init param does not exist.Span for the request. This method may return null
or an empty list if there are no user IDs to search for.