'How to enable ExecutorServiceMetrics in SpringBoot 2.1.2?
How to enable ExecutorServiceMetrics listed here ?
SpringBoot version: 2.1.2.RELEASE
Under /actuator/metrics I can see jvm and some other outofbox auto configured metrics, but not executor metrics.
I have tried setting this, but no luck.
management:
metrics:
enable.executor: true
any help is appreciated.
Solution 1:[1]
I found that you had to do it manually if you want to lock it in with Spring Boot. I'm using Spring Boot 2.2.9.RELEASE.
Create a ExecutorServiceMetrics bean, using the "applicationTaskExecutor" bean (that way, you get whatever bean size has previously been configured). It will automatically get bound.
Something like:
@Bean
@ConditionalOnMissingBean
public ExecutorServiceMetrics executorServiceMetrics(@Qualifier("applicationTaskExecutor") ThreadPoolTaskExecutor applicationTaskExecutor) {
return new ExecutorServiceMetrics(applicationTaskExecutor.getThreadPoolExecutor(), "applicationTaskExecutor",
Collections.emptyList());
}
Solution 2:[2]
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
TaskExecutorBuilderso the executor is built depending on thespring.task.execution.*properties - wrap the thread pool executor in
ExecutorServiceMetrics(fromio.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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user944849 |
| Solution 2 | Derlin |
