I am using Sleuth and I am wondering is it possible to get the current traceId? I dont need to add it any responses or anything. I just want the traceId for emails alerting the development team in certain situations.
Inject the Tracer bean and call tracer. currentSpan() to get the current span. From there you can get the trace id.
To get the current trace id as String you can use the traceIdString() method like this: tracer. currentSpan(). context(). traceIdString() .
TraceId – This is an id that is assigned to a single request, job, or action. Something like each unique user initiated web request will have its own traceId. SpanId – Tracks a unit of work. Think of a request that consists of multiple steps. Each step could have its own spanId and be tracked individually.
spring-cloud-starter-zipkin is deprecated, you should not use it anymore. You can use spring-cloud-starter-sleuth and spring-cloud-sleuth-zipkin ( 3. x ).
This Spring Cloud GCP starter can forward Spring Cloud Sleuth traces to Stackdriver Trace without an intermediary Zipkin server. You must enable Stackdriver Trace API from the Google Cloud Console in order to capture traces. Navigate to the Stackdriver Trace API for your project and make sure it’s enabled.
Specifically, Spring Cloud Sleuth… Adds trace and span ids to the Slf4J MDC, so you can extract all the logs from a given trace or span in a log aggregator. Instruments common ingress and egress points from Spring applications (servlet filter, rest template, scheduled actions, message channels, feign client).
Starting with version 2.0.0, Spring Cloud Sleuth uses Brave as the tracing library that adds unique ids to each web request that enters our application. Furthermore, the Spring team has added support for sharing these ids across thread boundaries. Traces can be thought of like a single request or job that is triggered in an application.
If you're using jersey One approach is to add jersey response filter and use Trace (autowired) from spring sleuth org.springframework.cloud.sleuth.Tracer We added this to our API gateway to avoid having to change each and every microservice
Ex
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);
}
}
Inject the Tracer
bean and call tracer.currentSpan()
to get the current span. From there you can get the trace id.
Please use Sleuth's API - that way regardless of which Tracer library you're using (Brave / OTel) your code will remain the same.
Example:
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
@Component
public class TraceService {
private final Tracer tracer;
public TraceService(Tracer tracer) {
this.tracer = tracer;
}
public String traceId() {
Span span = tracer.currentSpan();
String traceId = span.context().traceId();
System.out.println(traceId);
return traceId;
}
}
If there is no current trace in progress, tracer.currentSpan()
will return null
and hence tracer.currentSpan().context()
will throw a NPE. If you are unsure if there is a current trace and want to create one if none does exist, you should use
var span = tracer.startScopedSpan("fancyTitle");
try {
var traceId = span.context().traceIdString();
// use traceId ...
} finally {
span.finish(); // clean up after yourself
}
Note that this will create a new span in an existing trace, i.e. always generate a new spanId
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With