0

After migrating to spring boot 3 I added a bean BatchObservabilityBeanPostProcessor as it is described here: https://docs.spring.io/spring-batch/docs/current/reference/html/monitoring-and-metrics.html#tracing

I've noticed that spanId and traceId are not propageted in JobExcetionListener methods: afterJob and beforeJob.

In spring boot 2 tracing worked without any additional configuration and it was propagated on all levels (readers, processors, writers, listeners).

Is it expected behavour?

UPDATE CODE EXAMPLE:

Observablity Config

public class BatchObservabilityConfig {

    @Bean
    public static BatchObservabilityBeanPostProcessor batchObservabilityBeanPostProcessor() {
        return new BatchObservabilityBeanPostProcessor();
    }
}

Spring Batch Config:

@Bean
    public Job job() throws IOException {
        return new JobBuilder("job", jobRepository)
            .incrementer(new RunIdIncrementer())
            .listener(new CustomListener())
            .start(step())
            .build();
    }

    @Bean
    public Step step() throws IOException {
        return new StepBuilder("step", jobRepository)
            .<Object, Object>chunk(chunkSize, transactionManager)
            .reader(reader())
            .processor(processor())
            .writer(writer)
            .build();
    }

    @Bean
    @StepScope
    public ItemReader<Object> reader()
        throws IOException {
        logger.info("Log in reader"); // here TraceContext is available and TraceId and SpanId is generated using Tracer from micrometer
    }

    @Bean
    @StepScope
    public ItemProcessor<Object, Object> processor() {
        logger.info("Log in processor"); // here TraceContext is available and TraceId and SpanId is generated using Tracer from micrometer

    }

    @Bean
    @StepScope
    public ItemWriter<Object> writer() {
        logger.info("Log in writer"); // here TraceContext is available and TraceId and SpanId is generated using Tracer from micrometer

    }

JobExecutionListener implementation:

public class CustomListener extends JobExecutionListenerSupport {
   @Override
    public void afterJob(JobExecution jobExecution) {
                logger.info("Log in Listener"); // here NO TraceContext is available and NO TraceId and NO SpanId is generated using Tracer from micrometer
    }

}
cloud_7
  • 39
  • 1
  • 7
  • Can you please elaborate on this: `spanId and traceId are not propageted in JobExcetionListener`? What do you mean by propagated? Please share a code example and show where the info is missing to understand your issue. – Mahmoud Ben Hassine Jun 05 '23 at 07:21
  • I implemented a custom JobExecutionListener. I log some information regarding batch process after the job is done. I output logs through elastic search and each log contains traceId and spanId (they are generated from micrometer). The issue is that, there is no span context from micrometer within my custom implementation of JobExecutionListener and as a result the traceId and spanId are nulls. Within my implementation of ItemReader, ItemProcessor and ItemWriter the span context exists and is propagated across all steps. In my opinion, the span context should also be available in my custom impl – cloud_7 Jun 05 '23 at 07:42
  • Sorry, I still don't understand what you mean by "propagated" here: `Within my implementation of ItemReader, ItemProcessor and ItemWriter the span context exists and is propagated across all steps.`. Please share a code example to be able to help you efficiently. – Mahmoud Ben Hassine Jun 05 '23 at 10:38
  • Sorry, but the code is too extensive to paste it here, but the process looks more or less like that: 1) ItemReader -> Trace Context is available and I have TraceId = x and SpanId = y 2) ItemProcessor -> Trace Context is available and I have TraceId = x and SpanId = y 3) ItemWriter -> Trace Context is available and I have TraceId = x and SpanId = y 4) JobExecutionListener -> No Trace Context is available When I wrote propagated I mean, that I have the same TraceId across all steps – cloud_7 Jun 05 '23 at 11:37
  • No need for the entire code, just a minimal example that reproduces the issue https://stackoverflow.com/help/minimal-reproducible-example. Without that, I can't really help as I don't understand the issue nor able to reproduce it. – Mahmoud Ben Hassine Jun 05 '23 at 12:20
  • Please see the updated descritpion of the question. – cloud_7 Jun 05 '23 at 12:56
  • If you say this works with Boot 2 but not with Boot 3, then please open an issue on Github and we will dig deeper. On Github, attach a zip with a minimal example that we can run and reproduce the issue, not just code snippets. Thank you. – Mahmoud Ben Hassine Jun 06 '23 at 06:39
  • I found a workaround: https://stackoverflow.com/a/76385325/16798827 – cloud_7 Jun 07 '23 at 09:02

0 Answers0