Class TraceTokenContext

java.lang.Object
org.sdase.commons.shared.tracing.TraceTokenContext
All Implemented Interfaces:
Closeable, AutoCloseable

public class TraceTokenContext extends Object implements Closeable
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 Details

    • TRACE_TOKEN_HTTP_HEADER_NAME

      public static final String TRACE_TOKEN_HTTP_HEADER_NAME
      The name of the header containing the trace token in HTTP requests.
      See Also:
    • TRACE_TOKEN_MESSAGING_HEADER_NAME

      public static final String 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 of getOrCreateTraceTokenContext().
    • isReused

      public boolean isReused()
      Returns:
      if the "Trace-Token" of this TraceTokenContext has been reused from an existing TraceTokenContext for the caller of getOrCreateTraceTokenContext().
    • get

      public String get()
      Returns:
      the "Trace-Token" of this context.
    • closeIfCreated

      public void closeIfCreated()
      Closes this TraceTokenContext and removes all associated properties from the MDC only if this TraceTokenContext represents the initial creation of the context.
    • close

      public void close()
      Closes this TraceTokenContext and removes all associated properties from the MDC only if this TraceTokenContext represents the initial creation of the context.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getOrCreateTraceTokenContext

      public static TraceTokenContext 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 the MDC.
    • continueSynchronousTraceTokenContext

      public static TraceTokenContext continueSynchronousTraceTokenContext(String incomingTraceToken)
      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 be null, if no "Trace-Token" context was received.
      Returns:
      a TraceTokenContext either from the given incomingTraceToken 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 the MDC. In any case, this context will be considered as created and closeIfCreated() and close() will end the context.
    • createFromAsynchronousTraceTokenContext

      public static TraceTokenContext createFromAsynchronousTraceTokenContext(String incomingParentTraceToken)
      Creates a new TraceTokenContext 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 be null, if no "Parent-Trace-Token" context was received.
      Returns:
      a TraceTokenContext with a reference to the given incomingParentTraceToken as "Parent-Trace-Token" in the MDC. 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 the MDC.
    • isTraceTokenContextActive

      public static boolean isTraceTokenContextActive()
      Returns:
      if there is an active "Trace-Token" context in the current Thread.