Package org.sdase.commons.shared.tracing
Class TraceTokenContext
java.lang.Object
org.sdase.commons.shared.tracing.TraceTokenContext
- All Implemented Interfaces:
Closeable,AutoCloseable
Manages the context for the
Trace-Token. The Trace-Token is used to correlate log
messages across services for a single synchronous process. It will be logged as MDC key "Trace-Token".-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes thisTraceTokenContextand removes all associated properties from theMDConly if thisTraceTokenContextrepresents the initial creation of the context.voidCloses thisTraceTokenContextand removes all associated properties from theMDConly if thisTraceTokenContextrepresents the initial creation of the context.static TraceTokenContextcontinueSynchronousTraceTokenContext(String incomingTraceToken) Continues the "Trace-Token" context of a synchronous process that started outside this service.static TraceTokenContextcreateFromAsynchronousTraceTokenContext(String incomingParentTraceToken) Creates a newTraceTokenContextwith a reference to a parent context.get()static TraceTokenContextbooleanbooleanisReused()static boolean
-
Field Details
-
TRACE_TOKEN_HTTP_HEADER_NAME
The name of the header containing the trace token in HTTP requests.- See Also:
-
TRACE_TOKEN_MESSAGING_HEADER_NAME
The name of the header containing the trace token in asynchronous messages. Note that the trace token context should not be continued when processing asynchronous messages. It should only be used to connect a new trace token with the received trace token from the producer.- See Also:
-
-
Method Details
-
isCreated
public boolean isCreated()- Returns:
- if the "Trace-Token" of this
TraceTokenContexthas been created for the caller ofgetOrCreateTraceTokenContext().
-
isReused
public boolean isReused()- Returns:
- if the "Trace-Token" of this
TraceTokenContexthas been reused from an existingTraceTokenContextfor the caller ofgetOrCreateTraceTokenContext().
-
get
- Returns:
- the "Trace-Token" of this context.
-
closeIfCreated
public void closeIfCreated()Closes thisTraceTokenContextand removes all associated properties from theMDConly if thisTraceTokenContextrepresents the initial creation of the context. -
close
public void close()Closes thisTraceTokenContextand removes all associated properties from theMDConly if thisTraceTokenContextrepresents the initial creation of the context.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
getOrCreateTraceTokenContext
- Returns:
- a
TraceTokenContexteither from the existing "Trace-Token" or a new random "Trace-Token". All received instances must be closed. Only the context that started this trace will actually remove the "Trace-Token" from theMDC.
-
continueSynchronousTraceTokenContext
Continues the "Trace-Token" context of a synchronous process that started outside this service. Must not be used for asynchronous processes!- Parameters:
incomingTraceToken- the "Trace-Token" received from an external synchronous process, e.g. via HTTP header "Trace-Token" of an incoming HTTP request. May benull, if no "Trace-Token" context was received.- Returns:
- a
TraceTokenContexteither from the givenincomingTraceTokenor a new random "Trace-Token". All received instances must be closed. Only the context that started this trace will actually remove the "Trace-Token" from theMDC. In any case, this context will be considered as created andcloseIfCreated()andclose()will end the context.
-
createFromAsynchronousTraceTokenContext
public static TraceTokenContext createFromAsynchronousTraceTokenContext(String incomingParentTraceToken) Creates a newTraceTokenContextwith a reference to a parent context. This should be used to keep track of asynchronous processes to have a connection from the producer process to the new consumer process(es).- Parameters:
incomingParentTraceToken- the "Parent-Trace-Token" received from an external asynchronous process, e.g. via message header "Parent-Trace-Token" of a consumed Kafka message. May benull, if no "Parent-Trace-Token" context was received.- Returns:
- a
TraceTokenContextwith a reference to the givenincomingParentTraceTokenas "Parent-Trace-Token" in theMDC. If there is a current trace token context that already has a "Parent-Trace-Token", the current context is not changed. All received instances must be closed. Only the context that started this trace will actually remove the "Trace-Token" from theMDC.
-
isTraceTokenContextActive
public static boolean isTraceTokenContextActive()- Returns:
- if there is an active "Trace-Token" context in the current Thread.
-