Spring Cloud Sleuth- Get current traceId?

Dan*_*ton 7 spring-boot spring-cloud-sleuth

我正在使用 Sleuth,我想知道是否可以获取当前的 traceId?我不需要添加任何回复或任何东西。我只想要在某些情况下提醒开发团队的电子邮件的 traceId。

mad*_*fox 7

前任

import brave.Span;
import brave.Tracer;

@Service
public class TraceService {

    Tracer tracer;

    public TraceService(Tracer tracer) {
        this.tracer = tracer;
    }

    public void printTraceId() {
        Span span = tracer.currentSpan();
        String traceId = span.context().traceIdString();
        System.out.println(traceId);
    }

}
Run Code Online (Sandbox Code Playgroud)


Mar*_*zak 5

注入 tracer bean 并调用 currentSpan() 以获取当前跨度。从那里您可以获取跟踪 ID。