如何在SpringBoot 2.1.2中启用ExecutorServiceMetrics?

时间:2019-05-16 21:30:50

标签: spring-boot spring-boot-actuator spring-micrometer

如何启用此处列出的ExecutorServiceMetrics?

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

SpringBoot版本:2.1.2。发布

在/ actuator / metrics下,我可以看到jvm和其他一些现成的自动配置的指标,但看不到执行者指标。

我尝试过设置,但是没有运气。

management:
  metrics:
    enable.executor: true

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

我能够在Spring Boot 2.1.2.RELEASE应用程序中获取ExecutorServiceMetrics报告指标,除了创建受监视的ExecutorService bean之外,无需做其他任何事情。我无需在application.ymlapplication.properties上添加任何内容即可完成这项工作。

示例:

@Configuration
public class ExecutorConfig {

    @Bean
    public ExecutorService executorService(final MeterRegistry registry) {
        return ExecutorServiceMetrics.monitor(registry, Executors.newFixedThreadPool(20), "my executor", Tags.of("key", "value"));
    }
}

然后,只需将executorService bean连接到组件中,然后将任务提交到该executorService bean。

答案 1 :(得分:0)

我发现,如果要使用Spring Boot锁定它,则必须手动进行操作。我正在使用Spring Boot 2.2.9.RELEASE。

使用“ applicationTaskExecutor” Bean创建一个ExecutorServiceMetrics Bean(这样,您将获得之前已配置的任何Bean大小)。它会自动绑定。

类似的东西:

@Bean
@ConditionalOnMissingBean
public ExecutorServiceMetrics executorServiceMetrics(@Qualifier("applicationTaskExecutor")                                                             ThreadPoolTaskExecutor applicationTaskExecutor) {
    return new ExecutorServiceMetrics(applicationTaskExecutor.getThreadPoolExecutor(), "applicationTaskExecutor",
            Collections.emptyList());
}

答案 2 :(得分:0)

这是我如何解决的(在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")
    }

}

所以基本上:

  • 利用自动连接的TaskExecutorBuilder,以便根据spring.task.execution.*属性构建执行器
  • ExecutorServiceMetrics中包装线程池执行程序(来自io.micrometer.core)以获取指标

请注意,要使其正常工作,您必须返回装饰好的执行器!

在此示例中,由于我给了前缀(async),因此可用的指标为:

  • 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