Adding the traceId from Spring Cloud Sleuth to response

Anoop picture Anoop · Dec 19, 2016 · Viewed 13.9k times · Source

I am currently implementing Spring Cloud Sleuth in our project. I have a requirement to add the traceId to the response headers. Is there a way that this can be achieved?

Thanks,
Anoop

Answer

Níkolas La Porta picture Níkolas La Porta · Nov 3, 2019

Using raj-kumar-bhakthavachalam example but using springframework.cloud.sleuth version 2.1.1 you may use as the following steps:

1. Autowired brave.Tracer

@Autowired
Tracer tracer

2. CurrentSpan returns TraceContext, get traceIdString

tracer.currentSpan().context().traceIdString()