2

如何启用此处列出的 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.RELEASE

在 /actuator/metrics 下,我可以看到 jvm 和其他一些开箱即用的自动配置指标,但看不到执行程序指标。

我试过设置这个,但没有运气。

management:
  metrics:
    enable.executor: true

任何帮助表示赞赏。

4

3 回答 3

6

我能够ExecutorServiceMetrics在 Spring Boot 2.1.2.RELEASE 应用程序中获取报告指标,并且只需创建一个受监控的ExecutorServicebean 即可。我不需要添加任何东西来application.yml完成application.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"));
    }
}

然后,只需将您的executorServicebean 连接到您的组件中并将任务提交给该executorServicebean。

于 2019-05-21T00:47:48.383 回答
3

我发现如果你想用 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());
}
于 2020-08-21T05:03:08.520 回答
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(from io.micrometer.core) 中以获取指标

请注意,要使其正常工作,您必须返回已装饰的 executor !

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

  • 异步执行器
  • 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
于 2020-09-28T21:52:07.170 回答