Add metrics support for ThreadPoolTaskExecutor and ThreadPoolTaskScheduler

See original GitHub issue

Micrometer allows to monitor the executor states using ExecutorServiceMetrics

ref: https://github.com/micrometer-metrics/micrometer/blob/master/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jvm/ExecutorServiceMetrics.java

It allows to monitor the usage of executors with metrics like:

    "async.executor",
    "async.executor.active",
    "async.executor.completed",
    "async.executor.idle",
    "async.executor.pool.core",
    "async.executor.pool.max",
    "async.executor.pool.size",
    "async.executor.queue.remaining",
    "async.executor.queued",

To achieve that we have to wrap our executors with something like this:

    /**
     * <p>A default task executor.</p>
     * <p>The {@link Executor} instance to be used when processing async
     * method invocations.</p>
     */
    @Bean(DEFAULT_TASK_EXECUTOR_BEAN_NAME)
    @Override
    public Executor getAsyncExecutor() {
        // Using a thread-pooling TaskExecutor implementation,
        // in particular for executing a large number of short-lived tasks.
        final ThreadPoolTaskExecutor executor = new TaskExecutorBuilder()
            .corePoolSize(100) // With unlimited queue
            .allowCoreThreadTimeOut(true)
            .threadNamePrefix("task-")
            .build();
        executor.initialize();
        return ExecutorServiceMetrics.monitor(
            meterRegistry,
            executor.getThreadPoolExecutor(),
            "AsyncExecutor",
            "async",
            tags);
    }

(I am creating the tags from MetricsProperties#getTags)

It might be great if an AutoConfiguration could do it magically (it’s why we love Spring-Boot). Not sure how it could be implemented in a Spring-Boot way properly.

We can find such request in various places:

cc @snicoll

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
snicollcommented, Apr 21, 2021

I’ve added a proposal in 910e14f. Unfortunately, it is too late for 2.5.x but I’ll do my best to get that in for 2.6.0-M1. It handles both task executors and task schedulers using Micrometer’s ExecutorServiceMetrics.

1reaction
snicollcommented, Oct 29, 2020

@aheritier yes, we’re not going to implement that feature this way. Spring Boot rather injects the component (i.e. thread pool) later on and register metrics on it, which avoids causing unnecessary early initialisations due to the dependency on the MeterRegistry. If you have questions about registering custom metrics, please follow-up on StackOverflow or come chat with the community on Gitter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Metrics
Calling collectionSize on our meter registry adds a Gauge time series named population that changes when observed by a metrics backend or exporter....
Read more >
Spring framework monitoring ThreadPoolTaskExecutor queue ...
If I use jmx to monnitor ThreadPoolTaskExecutor, how can I monitor queue size level to make sure it is healthy?
Read more >
A Guide to the Spring Task Scheduler - Baeldung
A quick and practical guide to scheduling in Spring with Task Scheduler.
Read more >
Spring Boot Actuator | SpringerLink
Add a dependency for the spring-boot-starter-actuator to your project and the health and metrics will be enabled and exposed for your ...
Read more >
Index (spring-metrics 0.5.0.RELEASE API) - javadoc.io
Record metrics on the use of a ThreadPoolTaskExecutor . monitor(MeterRegistry, ThreadPoolTaskScheduler, String, Iterable<Tag>) - Static method in class ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found