I want to add sleuth traceId
and spanId
to response headers. And I try to do it in WebFilter
, similar to suggestions in the documentation. But in filter currentSpan
is yet null, latter in controller traceId
and spanId
I are present in logs. How can I add traceId
and spanId
to response headers?
Here is my code:
@Component
@Order(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER + 1)
@Slf4j
public class SleuthFilter implements WebFilter {
@Autowired
private Tracer tracer;
@Override
public Mono<Void> filter(ServerWebExchange serverWebExchange,
WebFilterChain webFilterChain) {
log.info("Filter executions"); // this log will not any traceId or spanId
Span currentSpan = tracer.currentSpan();
if (currentSpan != null) {
serverWebExchange.getResponse().getHeaders().add("ZIPKIN-TRACE-ID", currentSpan.context().traceIdString());
}
return webFilterChain.filter(serverWebExchange);
}
}