I am using spring boot 3 / spring batch and I would like to propagate the context across the spring batch SimpleAsyncTaskExecutor steps for distributed tracing (micrometer). In my case, the main flow splits into parallel flow and back to main flow. I would like the trace id to be the same across all the parent and child flows. I noticed the steps in the main flow have the same trace id, but the parallel flows have a different trace id.
Please advise. Thanks
ps Took similar approach to answer below:
@Bean
public TaskDecorator taskFlowDecorator() {
return new ContextPropagatingTaskDecorator();
}
@Bean("taskFlowExecutor")
public TaskExecutor taskFlowExecutor(TaskDecorator taskFlowDecorator) {
return new SimpleAsyncTaskExecutorBuilder()
.threadNamePrefix("work-flow-")
.taskDecorator(taskFlowDecorator)
.build();
}
I am using spring boot 3 / spring batch and I would like to propagate the context across the spring batch SimpleAsyncTaskExecutor steps for distributed tracing (micrometer). In my case, the main flow splits into parallel flow and back to main flow. I would like the trace id to be the same across all the parent and child flows. I noticed the steps in the main flow have the same trace id, but the parallel flows have a different trace id.
Please advise. Thanks
ps Took similar approach to answer below:
@Bean
public TaskDecorator taskFlowDecorator() {
return new ContextPropagatingTaskDecorator();
}
@Bean("taskFlowExecutor")
public TaskExecutor taskFlowExecutor(TaskDecorator taskFlowDecorator) {
return new SimpleAsyncTaskExecutorBuilder()
.threadNamePrefix("work-flow-")
.taskDecorator(taskFlowDecorator)
.build();
}
Share
edited 2 days ago
user518066
asked Feb 17 at 23:07
user518066user518066
1,4295 gold badges27 silver badges41 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 2The approach described in this answer could be adapted to propagate context (e.g. thread-local variables) to Spring Batch child flows or steps.
If you want to have one traceId
in all steps or flows of the Spring Batch job, then
- set
ContextPropagatingTaskDecorator
toSimpleAsyncTaskExecutor
:
@Bean
public TaskDecorator taskDecorator() {
return new ContextPropagatingTaskDecorator();
}
@Bean
public TaskExecutor taskExecutor(TaskDecorator taskDecorator) {
var taskExecutor = new SimpleAsyncTaskExecutor();
executor.setTaskDecorator(taskDecorator);
return taskExecutor;
}
- use the decorated executor in your flows/steps:
// example of flow
FlowBuilder<SimpleFlow>("parallelFlow")
.split(taskExecutor)
// example of step
StepBuilder("mainStep", jobRepository)
...
.taskExecutor(taskExecutor)
Another approach how to create a decorated executor suggested by the question's author:
@Bean
public TaskExecutor taskExecutor(TaskDecorator taskDecorator) {
return new SimpleAsyncTaskExecutorBuilder()
.threadNamePrefix("work-flow-")
.taskDecorator(taskDecorator)
.build();
}
TaskDecorator
as suggested in the answer above It has helped - all steps/flows of one job share the same trace id. So, I believe the answer I provided above should help you. If not, please share a minimal reproducible example. – Geba Commented 2 days agoSimpleAsyncTaskExecutorBuilder
in my answer. In order to keep answer and question in correct places. Also I will clear comment section later. Thanks! – Geba Commented 2 days ago