Class RequestIdHandler
java.lang.Object
org.gcube.smartgears.handlers.AbstractHandler
org.gcube.smartgears.handlers.application.RequestHandler
org.gcube.smartgears.handlers.application.request.RequestIdHandler
- All Implemented Interfaces:
ApplicationHandler<RequestHandler>,Handler<ApplicationEvent<RequestHandler>>
A
RequestHandler that manages a per-request correlation ID ("X-Request-ID").
On every incoming request the handler:
- reads the
X-Request-IDHTTP header; - if absent or blank, generates a short random identifier (first 8 chars of a UUID, upper-cased);
- if present but longer than 128 characters, truncates it;
- stores the value in the SLF4J MDC under the key "requestId" so that it appears in every log line of the current thread;
- stores the value in a thread-local so that it can be retrieved at any point via
currentRequestId(); - echoes the value back to the client immediately via the
X-Request-IDHTTP response header, before the response is committed.
On the response the handler cleans up both the MDC entry and the thread-local.
The handler is unfiltrable: it runs for every request regardless of the
include/exclude rules configured in application.yaml.
- Author:
- Luca Frosini (ISTI-CNR)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringReturns the request ID associated with the current thread, ornullif none is set (i.e. the call is not going through the SmartGears request pipeline).getName()voidInvoked when the container receives a request for a servlet of a managed app.voidInvoked when a servlet of a managed app has produced a response to a request.booleanThis handler must run even on paths that are excluded from other handlers so that every request gets a correlation ID in the logs.toString()Methods inherited from class org.gcube.smartgears.handlers.application.RequestHandler
onEvent, start, stopMethods inherited from class org.gcube.smartgears.handlers.AbstractHandler
equals
-
Field Details
-
MDC_KEY
MDC key used to store the request ID in the logging context.- See Also:
-
-
Constructor Details
-
RequestIdHandler
public RequestIdHandler()
-
-
Method Details
-
currentRequestId
Returns the request ID associated with the current thread, ornullif none is set (i.e. the call is not going through the SmartGears request pipeline).- Returns:
- the current request ID, or
null
-
getName
- Specified by:
getNamein classRequestHandler
-
isUnfiltrable
public boolean isUnfiltrable()This handler must run even on paths that are excluded from other handlers so that every request gets a correlation ID in the logs.- Overrides:
isUnfiltrablein classRequestHandler
-
handleRequest
Description copied from class:RequestHandlerInvoked when the container receives a request for a servlet of a managed app.- Overrides:
handleRequestin classRequestHandler- Parameters:
e- the request event
-
handleResponse
Description copied from class:RequestHandlerInvoked when a servlet of a managed app has produced a response to a request.- Overrides:
handleResponsein classRequestHandler- Parameters:
e- the response event
-
toString
- Overrides:
toStringin classAbstractHandler
-