Package com.shri.restinpeace.interceptor
Class CorrelationIdInterceptor
java.lang.Object
com.shri.restinpeace.interceptor.CorrelationIdInterceptor
- All Implemented Interfaces:
RequestInterceptor
Pre-built interceptor that attaches a fresh correlation/request ID to
every request - useful for tracing a call across service boundaries or
correlating client-side logs with server-side logs. Defaults to a random
UUID per call sent under the
X-Request-Id header; both the header
name and the ID generator are configurable.
The generated ID is also stashed on the RequestContext under
ID_ATTRIBUTE, so another interceptor registered alongside this
one (e.g. a logging or metrics interceptor) can read it back in
beforeRequest/afterResponse to correlate its own output.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe header used when no header name is given explicitly.static final StringRequestContextattribute key the generated ID is stashed under. -
Constructor Summary
ConstructorsConstructorDescriptionGenerates a random UUID per call, sent underDEFAULT_HEADER_NAME.CorrelationIdInterceptor(String headerName) Generates a random UUID per call, sent under a custom header name.CorrelationIdInterceptor(String headerName, Supplier<String> idGenerator) Uses a custom ID generator instead of a random UUID. -
Method Summary
Modifier and TypeMethodDescriptionvoidbeforeRequest(RequestContext context) Called before the request is sent.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.shri.restinpeace.interceptor.RequestInterceptor
afterResponse, shortCircuit
-
Field Details
-
DEFAULT_HEADER_NAME
The header used when no header name is given explicitly.- See Also:
-
ID_ATTRIBUTE
RequestContextattribute key the generated ID is stashed under.- See Also:
-
-
Constructor Details
-
CorrelationIdInterceptor
public CorrelationIdInterceptor()Generates a random UUID per call, sent underDEFAULT_HEADER_NAME. -
CorrelationIdInterceptor
Generates a random UUID per call, sent under a custom header name.- Parameters:
headerName- the header to send the generated ID under
-
CorrelationIdInterceptor
Uses a custom ID generator instead of a random UUID.- Parameters:
headerName- the header to send the generated ID underidGenerator- supplies a fresh ID for each request
-
-
Method Details
-
beforeRequest
Description copied from interface:RequestInterceptorCalled before the request is sent. Can add headers viaRequestContext.addHeader(java.lang.String, java.lang.String), or abort the call by throwing.- Specified by:
beforeRequestin interfaceRequestInterceptor- Parameters:
context- the request being made
-