Skip to content

Add metrics support for ThreadPoolTaskExecutor and ThreadPoolTaskScheduler #23818

Closed
@aheritier

Description

@aheritier

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions