Spring Cloud Sleuth 获取当前 Trace ID-元一软件

未分类2个月前更新 元一软件
116 0
Span span = tracer.currentSpan();

if (span != null) {

    logger.info("Span ID hex {}", span.context().spanIdString());

    logger.info("Span ID decimal {}", span.context().spanId());

}

这两个值代表相同的数值,输出结果类似于如下:

INFO [Baeldung Sleuth Tutorial,0de46b6fcbc8da83,0de46b6fcbc8da83] 8648 --- [nio-8080-exec-3] c.b.s.traceid.SleuthTraceIdController    : Span ID hex 0de46b6fcbc8da83

INFO [Baeldung Sleuth Tutorial,0de46b6fcbc8da83,0de46b6fcbc8da83] 8648 --- [nio-8080-exec-3] c.b.s.traceid.SleuthTraceIdController    : Span ID decimal 1001043145087572611

同样,这也适用于 Trace ID。traceIdString 返回十六进制值,而 traceId 返回十进制值:

开发工具

logger.info("Trace ID hex {}", span.context().traceIdString());

logger.info("Trace ID decimal {}", span.context().traceId());

输出结果和上面类似。首先是十六进制的 Trace ID,然后是十进制:

INFO [Baeldung Sleuth Tutorial,34ec0b8ac9d65e91,34ec0b8ac9d65e91] 7384 --- [nio-8080-exec-1] c.b.s.traceid.SleuthTraceIdController    : Trace ID hex 34ec0b8ac9d65e91

INFO [Baeldung Sleuth Tutorial,34ec0b8ac9d65e91,34ec0b8ac9d65e91] 7384 --- [nio-8080-exec-1] c.b.s.traceid.SleuthTraceIdController    : Trace ID decimal 3813435675195629201

6、总结

本文介绍了如何通过 Spring Cloud Sleuth 追踪 Spring Boot 中的事件,以帮助 Debug。


参考:https://www.baeldung.com/spring-cloud-sleuth-get-trace-id

© 版权声明