Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Sleuth- Get current traceId?

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.

like image 964
Daniel Haughton Avatar asked Aug 09 '19 09:08

Daniel Haughton


People also ask

How can I get TraceId from spring sleuth?

Inject the Tracer bean and call tracer. currentSpan() to get the current span. From there you can get the trace id.

How do I find my current trace ID?

To get the current trace id as String you can use the traceIdString() method like this: tracer. currentSpan(). context(). traceIdString() .

What is TraceId and SpanId in sleuth?

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.

Is zipkin deprecated?

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 ).

How to Forward Spring Cloud sleuth traces to Stackdriver trace without Zipkin?

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.

What is Spring Cloud sleuth?

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).

How does Spring Cloud sleuth use brave for tracing?

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.

How do I trace a response in spring sleuth using Jersey?

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


3 Answers

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);
    }

}
like image 160
mad_fox Avatar answered Oct 16 '22 16:10

mad_fox


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;
    }

}
like image 20
Marcin Grzejszczak Avatar answered Oct 16 '22 16:10

Marcin Grzejszczak


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.

like image 4
Yasammez Avatar answered Oct 16 '22 16:10

Yasammez