Micrometer Tracing In Spring Boot — Context Propagation For @Async, @Scheduled, @NewSpan, @ContinueSpan And @SpanTag
Prequel: Please check my other blog(s) for setup required to enable Micrometer Tracing in a Spring Boot App (depending on your Tech Stack):
1. OpenTelemetry + Jaeger
2. gRPC + OpenTelemetry + Jaeger
3. Brave + Zipkin
4. gRPC + Brave + Zipkin
5. OpenTelemetry + Zipkin
6. gRPC + OpenTelemetry + Zipkin
In this blog, we will focus on extending the tracing context propagation to Spring Asynchronous methods, Spring Scheduled methods and using Micrometer @NewSpan, @CurrentSpan and @SpanTag to customize Span tracing for specific methods.
@Async
Micrometer offers Context Propagation library, which comes as a transitive dependency to Micrometer Tracing library, which automatically propagates thread locals / context objects to child threads. All we have to do is wrap our Spring ThreadPoolExecutors with Micrometer ContextExecutorService
.
There are two ways to approaching this, either by using Spring AsyncConfigurer or Spring TaskDecorator, depending on your use case of whether you have one or multiple thread pools in your app.