Class TraceKit

java.lang.Object
com.iohao.net.common.kit.trace.TraceKit

public final class TraceKit extends Object
Distributed tracing utilities using SLF4J MDC for trace ID propagation.
Author:
渔民小镇
date:
2023-12-20
  • Field Details

  • Method Details

    • newTraceId

      public static String newTraceId()
      Generate a new trace ID using the configured TraceIdSupplier.
      Returns:
      a new trace ID string
    • getTraceId

      public static String getTraceId()
      Get the current trace ID from the SLF4J MDC context.
      Returns:
      the current trace ID, or null if none is set
    • putCloseable

      public static org.slf4j.MDC.MDCCloseable putCloseable(String traceId)
      Put the given trace ID into the MDC context, returning a closeable handle that removes it when closed.
      Parameters:
      traceId - the trace ID to set
      Returns:
      a closeable that removes the trace ID from MDC on close
    • decorator

      public static Runnable decorator(String traceId, Runnable command)
      Decorate a Runnable so that the given trace ID is set in MDC during its execution.
      Parameters:
      traceId - the trace ID to propagate
      command - the runnable to decorate
      Returns:
      a new runnable that sets and clears the trace ID around execution
    • decorator

      public static <T> Consumer<T> decorator(String traceId, Consumer<T> action)
      Decorate a Consumer so that the given trace ID is set in MDC during its execution.
      Type Parameters:
      T - the type of the consumer input
      Parameters:
      traceId - the trace ID to propagate
      action - the consumer to decorate
      Returns:
      a new consumer that sets and clears the trace ID around execution
    • setTraceIdSupplier

      public static void setTraceIdSupplier(TraceIdSupplier traceIdSupplier)