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 TypeMethodDescriptionvoid
close()
Closes thisTraceTokenContext
and removes all associated properties from theMDC
only if thisTraceTokenContext
represents the initial creation of the context.void
Closes thisTraceTokenContext
and removes all associated properties from theMDC
only if thisTraceTokenContext
represents the initial creation of the context.static TraceTokenContext
continueSynchronousTraceTokenContext
(String incomingTraceToken) Continues the "Trace-Token" context of a synchronous process that started outside this service.static TraceTokenContext
createFromAsynchronousTraceTokenContext
(String incomingParentTraceToken) Creates a newTraceTokenContext
with a reference to a parent context.get()
static TraceTokenContext
boolean
boolean
isReused()
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
TraceTokenContext
has been created for the caller ofgetOrCreateTraceTokenContext()
.
-
isReused
public boolean isReused()- Returns:
- if the "Trace-Token" of this
TraceTokenContext
has been reused from an existingTraceTokenContext
for the caller ofgetOrCreateTraceTokenContext()
.
-
get
- Returns:
- the "Trace-Token" of this context.
-
closeIfCreated
public void closeIfCreated()Closes thisTraceTokenContext
and removes all associated properties from theMDC
only if thisTraceTokenContext
represents the initial creation of the context. -
close
public void close()Closes thisTraceTokenContext
and removes all associated properties from theMDC
only if thisTraceTokenContext
represents the initial creation of the context.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
getOrCreateTraceTokenContext
- Returns:
- a
TraceTokenContext
either 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
TraceTokenContext
either from the givenincomingTraceToken
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
. 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 newTraceTokenContext
with 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
TraceTokenContext
with a reference to the givenincomingParentTraceToken
as "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.
-