Class TraceContext
java.lang.Object
tech.riemann.nif.trace.TraceContext
链路追踪上下文
支持try-with-resources自动清理,避免ThreadLocal内存泄漏
- Author:
- Riemann
创建日期: 2026/03/27 10:14:44
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classTrace作用域,用于自动清理ThreadLocal 实现AutoCloseable接口,支持try-with-resources语法 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TraceContext.Scope创建一个新的TraceScope,使用新生成的traceIdstatic TraceContext.ScopebeginScope(String traceId) 创建一个新的TraceScope,支持try-with-resources自动清理 使用方式:static voidclear()清除当前线程的链路IDstatic String生成一个新的链路IDstatic String获取当前线程的链路IDstatic voidsetTraceId(String traceId) 设置当前线程的链路IDstatic Runnable包装一个Runnable,在执行时保持trace上下文static <T> Callable<T> 包装一个Callable,在执行时保持trace上下文static <T> Supplier<T> 包装一个Supplier,在执行时保持trace上下文
-
Constructor Details
-
TraceContext
public TraceContext()
-
-
Method Details
-
getTraceId
-
setTraceId
设置当前线程的链路IDM15-低级: MDC 不接受 null 值(slf4j 2.x / logback 对 null value 抛异常)——null 时从 MDC 移除而非 put
- Parameters:
traceId- 链路ID
-
clear
public static void clear()清除当前线程的链路ID -
generateTraceId
-
beginScope
创建一个新的TraceScope,支持try-with-resources自动清理 使用方式:try (TraceContext.Scope scope = TraceContext.beginScope("new-trace-id")) { // 业务逻辑 } // 自动清理- Parameters:
traceId- 链路ID- Returns:
- TraceScope实例
-
beginScope
-
wrap
-
wrap
-
wrap
-