0

I am scheduling the spring scheduler with SchedulingConfigurer as follows. However, new traceid is not getting created every time the "ProcessJob" method is getting called.

Even following method always logs with the same traceid.

          log.info("Performing task");

What is the issue here? and how do i ensure new traceid everytime this job is triggered.

I have even tried wrapping "processJob" method call inside newSpan as follows: but no luck.

Fix 1: not working:

  private void setSchedule() {
    future =
        taskScheduler.schedule(
            () -> {

                Span newSpan = tracer.nextSpan().name("newSpan").start();
               try (SpanInScope ws = tracer.withSpanInScope(newSpan.start())) {
                            log.info("Performing task");
                            taskManager.processJob(); 
                  } finally {
                      newSpan.finish();
                  }
            },
            dynamicTrigger);
  }

Original class that needs fix:

public class SchedulerConfig
    implements SchedulingConfigurer, ApplicationListener<RefreshScopeRefreshedEvent> {

  private final DynamicTrigger dynamicTrigger;

  private final TaskManager taskManager;

  private TaskScheduler taskScheduler;
  private ScheduledFuture<?> future;

  @Bean(destroyMethod = "shutdown")
  public ExecutorService taskExecutor() {
    return Executors.newScheduledThreadPool(1);
  }

  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.setScheduler(taskExecutor());
    taskScheduler = taskRegistrar.getScheduler();
    setSchedule();
  }

  private void setSchedule() {
    future =
        taskScheduler.schedule(
            () -> {z
            log.info("Performing task");
              taskManager.processJob();
            },
            dynamicTrigger);
  }

  @Override
  public void onApplicationEvent(RefreshScopeRefreshedEvent event) {

    log.info("Rescheduling due to change in cron expression");
    future.cancel(false);
    setSchedule();
  }
QBrute
  • 4,405
  • 6
  • 34
  • 40
Alagesan Palani
  • 1,984
  • 4
  • 28
  • 53

1 Answers1

1

The way you start the span is not how you suppose to do it (e.g.: you call start twice). Please check the docs to see how to do it properly: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#using-creating-and-ending-spans

The easiest way to start a new span is using @NewSpan on a method that belongs to a Spring Bean, please see the docs: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#using-annotations-new-spans

For scheduling, I think it is way simpler using @Scheduled, see the docs: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#sleuth-async-scheduled-integration This is also instrumented out of the box by Sleuth so you don't need to do anything to start a new Span:

@Scheduled(fixedDelay = 1_000)
public void scheduled() {
    log.info("Hey, look what I'm doing");
}

If you don't want to use @Scheduled, you can use a TraceableScheduledExecutorService as your ExecutorService, docs: https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/htmlsingle/#sleuth-async-executor-service-integration

Jonatan Ivanov
  • 4,895
  • 2
  • 15
  • 30