Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task executor metrics are not registered when using lazy initialization #37832

Closed
joshiste opened this issue Oct 11, 2023 · 1 comment
Closed
Assignees
Labels
type: bug A general bug
Milestone

Comments

@joshiste
Copy link
Contributor

joshiste commented Oct 11, 2023

Using a custom ThreadPoolTaskExecutor bean I'd expect it to be picked up by TaskExecutorMetricsAutoConfiguration to provide me with the metrics.

But when using the spring.main.lazy-initialization=true it doesn't bind the metrics. Also marking the bean with @Lazy(false) doesn't help.

Using Spring Boot 3.1.4 (but same issue on 3.0.x)

Code to reproduce:

@SpringBootApplication
public class DemoApplication {
    @Bean(name = "custom")
    ThreadPoolTaskExecutor webhookTaskExecutor(TaskExecutorBuilder builder) {
        builder.awaitTermination(false);
        return builder.build();
    }
    @Bean
    ApplicationRunner applicationRunner(ThreadPoolTaskExecutor executor) {
        return args -> {
            executor.submit(() -> System.out.println("Hello World")).get();
        };
    }
    public static void main(String[] args) {
        var app = new SpringApplication(DemoApplication.class);
        app.setDefaultProperties(Map.of("spring.main.lazy-initialization","true"));
        var ctx = app.run(args);

        System.out.println(ctx.getBean(MeterRegistry.class).get("executor.completed").tags("name", "custom").meters().size());
    }
}

It fails with

Exception in thread "main" io.micrometer.core.instrument.search.MeterNotFoundException: Unable to find a meter that matches all the requirements at once. Here's what was found:
   FAIL: No meter with name 'executor.completed' was found.
   FAIL: No meters have the required tag 'name'.
	at io.micrometer.core.instrument.search.MeterNotFoundException$FromRequiredSearch.build(MeterNotFoundException.java:245)
	at io.micrometer.core.instrument.search.MeterNotFoundException$FromRequiredSearch.access$100(MeterNotFoundException.java:49)
	at io.micrometer.core.instrument.search.MeterNotFoundException.forSearch(MeterNotFoundException.java:46)
	at io.micrometer.core.instrument.search.RequiredSearch.meters(RequiredSearch.java:220)
	at com.example.demo.DemoApplication.main(DemoApplication.java:37)
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 11, 2023
@wilkinsona wilkinsona changed the title TaskExecutorMetricsAutoConfiguration won't pickup custom thread pools when lazy initialization is used. Task executor metrics are not registered when using lazy initialization Oct 12, 2023
@wilkinsona
Copy link
Member

Thanks for the report. As far as I can tell, this isn't specific to custom executors and affects the auto-configured executor as well.

You can work around the problem by excluding TaskExecutorMetricsAutoConfiguration from lazy initialization:

@Bean
static LazyInitializationExcludeFilter eagerTaskExecutorMetrics() {
	return LazyInitializationExcludeFilter.forBeanTypes(TaskExecutorMetricsAutoConfiguration.class);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants