Here is how I solved it (in kotlin):
@EnableAsync
@Configuration
class AsyncConfig(
private val taskExecutorBuilder: TaskExecutorBuilder,
private val meterRegistry: MeterRegistry) : AsyncConfigurer {
/**
* Add monitoring of executor using micrometer.
*/
override fun getAsyncExecutor(): Executor {
// create executor based on default spring-boot properties
val executor = taskExecutorBuilder.build()
// we need to initialize it before calling monitor
executor.initialize()
// monitor the executor (so it is available in metrics) (must be wrapped)
return ExecutorServiceMetrics.monitor(meterRegistry, executor.threadPoolExecutor, "AsyncExecutor", "async")
}
}
So basically:
- make use of the autowired
TaskExecutorBuilder
so the executor is built depending on the spring.task.execution.*
properties
- wrap the thread pool executor in
ExecutorServiceMetrics
(from io.micrometer.core
) to get the metrics
Note that for this to work, you must return the decorated executor !
In this example and since I gave a prefix (async
), the metrics available are:
- 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