spring-boot - 如何通过勇敢的追踪打印 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。我现在应该如何打印?
解决方案
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());
}
}
推荐阅读
- python - Airflow macro not being read as expected
- java - How to pass current record as parameter to a routine?
- java - How to save a string(list) from a MySQL query in Java
- c - Can I bridge C inline functions to Swift?
- javascript - Logic to check if an object property exists in React?
- javascript - A-frame play specific animations from sketch fab
- json - Select - Oracle JSON Object - Join
- xml - Code Completion Error in Camel blueprint.xml
- node.js - why my localhost is loading continously? I have a weather app by fetching the result from APi
- react-native - Need some help handling the response I'm getting from json server