admin管理员组文章数量:1244316
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();
}
本文标签:
版权声明:本文标题:Micrometer tracing: Propagate context to spring batch (spring boot 3) SimpleAsyncTaskExecutor tasks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740121587a2228085.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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