首页 > 解决方案 > 如何通过勇敢的追踪打印 traceId 和 spanId?

问题描述

早些时候,我曾经用 spring boot 1.5.12 编写以下代码:-

 import org.springframework.cloud.sleuth.Span;

 import org.springframework.cloud.sleuth.Tracer;

@Autowired
Tracer tracer;



    Span span = this.tracer.getCurrentSpan();
    System.out.println(Span.idToHex(span.getSpanId()));
    System.out.println(Span.idToHex(span.getTraceId()));

但是这段代码不适用于 spring boot 2.2.6。我现在应该如何打印?

标签: spring-boottracespring-cloud-sleuth

解决方案


import brave.Tracer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class BravePrinter {

    @Autowired
    private Tracer tracer;

    public void print() {
        var span = this.tracer.currentSpan();
        System.out.println(span.context().traceIdString());
        System.out.println(span.context().spanIdString());
    }
}

推荐阅读