We have a microservices architecture and are trying to implement distributed tracing, with the below somewhat naive implementation:
I've got the following dependencies for micrometer and reactor:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
We generate a unique correlation id and store it in ThreadContext:
@Component
@RequiredArgsConstructor
@Log4j2
public class CorrelationIdFilter extends OncePerRequestFilter {
public static final String X_CORRELATION_ID = "X-Correlation-Id";
@Override
protected void doFilterInternal(
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String correlationId = request.getHeader(X_CORRELATION_ID);
if (Objects.isNull(correlationId)) {
correlationId = UUID.randomUUID().toString().split("-")[0];
log.info(
"Request does not include {} header. Generated value '{}'",
X_CORRELATION_ID,
correlationId);
}
ThreadContext.put(X_CORRELATION_ID, correlationId);
try {
filterChain.doFilter(request, response);
} finally {
threadContextFacade.remove(X_CORRELATION_ID);
}
}
Adding new X_CORRELATION_ID to log4j2.xml:
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} -- cId:%X{X-Correlation-Id} [%15.15t] %-40.40c{1.} : %m%n%ex
This all works like a treat, I can see correlation id in the logs.
Now I need to propagate this correlation-id to downstream service.
For that, I've added the following in the main metod of the Spring boot application:
public static void main(String[] args) {
Hooks.enableAutomaticContextPropagation();
SpringApplication.run(CompanyApplication.class, args);
}
But the downstream application never receives X-Correlation-Id
header.
What am I missing?