Reputation: 305
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.
Upvotes: 7
Views: 12420
Reputation: 856
If you don't know whether a meterRegistry
bean exists, you could try to use ObjectProvider
@Configuration
public class ExecutorConfig {
@Bean
public ExecutorService executorService(ObjectProvider<MeterRegistry> meterRegistryProvider) {
ExecutorService executorService = Executors.newFixedThreadPool(20);
meterRegistryProvider.ifAvailable(registry -> ExecutorServiceMetrics.monitor(registry, executorService, "my executor", Tags.of("key", "value")));
return executorService;
}
}
Upvotes: 1
Reputation: 173
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());
}
Upvotes: 4
Reputation: 9881
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:
TaskExecutorBuilder
so the executor is built depending on the spring.task.execution.*
propertiesExecutorServiceMetrics
(from io.micrometer.core
) to get the metricsNote 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:
Upvotes: 0
Reputation: 771
I was able to get ExecutorServiceMetrics
reporting metrics in a Spring Boot 2.1.2.RELEASE app and didn't have to do any more than create a monitored ExecutorService
bean. I didn't have to add anything to my application.yml
or application.properties
to make this work.
Example:
@Configuration
public class ExecutorConfig {
@Bean
public ExecutorService executorService(final MeterRegistry registry) {
return ExecutorServiceMetrics.monitor(registry, Executors.newFixedThreadPool(20), "my executor", Tags.of("key", "value"));
}
}
Then, just wire your executorService
bean into your components and submit tasks to that executorService
bean.
Upvotes: 8