我正在尝试为应监视自定义 ThreadPoolTaskExecutor 的千分尺构建自定义仪表。问题是仪表值保持不变并且自应用程序启动以来从未更新。但是执行器统计信息(可以在调试器中看到)显示变化的值。
这是我的仪表配置课程。
@Component
public class ThreadPoolMetricProbe implements MeterBinder {
@Inject
private Executor taskExecutor;
@Override
public void bindTo(MeterRegistry meterRegistry) {
ThreadPoolTaskExecutor exec = (ThreadPoolTaskExecutor) taskExecutor;
long completedTaskCount = exec.getThreadPoolExecutor().getCompletedTaskCount();
int largestPoolSize = exec.getThreadPoolExecutor().getLargestPoolSize();
int maximumPoolSize = exec.getThreadPoolExecutor().getMaximumPoolSize();
int poolSize = exec.getThreadPoolExecutor().getPoolSize();
long taskCount = exec.getThreadPoolExecutor().getTaskCount();
int activeCount = exec.getThreadPoolExecutor().getActiveCount();
String threadNamePrefix = exec.getThreadNamePrefix();
Gauge.builder("executorCompletedTaskCount", completedTaskCount, Double::new)
.description(String.format("Returns the approximate total number of tasks that have completed execution of %s executor", threadNamePrefix.toLowerCase()))
.register(meterRegistry);
Gauge.builder("executorLargestThreadsNumber", largestPoolSize, Double::new)
.description(String.format(" Returns the largest number of threads that have ever simultaneously been in the pool of %s executor", threadNamePrefix.toLowerCase()))
.register(meterRegistry);
Gauge.builder("executorMaximumAllowedThreadsNumber", maximumPoolSize, Double::new)
.description(String.format("Returns the maximum allowed number of threads of %s executor", threadNamePrefix.toLowerCase()))
.register(meterRegistry);
Gauge.builder("executorCurrentNumberOfThreads", poolSize, Double::new)
.description(String.format("Returns the current number of threads in the %s executor pool", threadNamePrefix.toLowerCase()))
.register(meterRegistry);
Gauge.builder("executorTotalTaskCount", taskCount, Double::new)
.description(String.format(" Returns the approximate total number of tasks that have ever been scheduled for execution by %s executor", threadNamePrefix.toLowerCase()))
.register(meterRegistry);
Gauge.builder("executorActiveThreadsCount", activeCount, Double::new)
.description(String.format("Returns the approximate number of threads that are actively executing tasks by %s executor", threadNamePrefix.toLowerCase()))
.register(meterRegistry);
Metrics.addRegistry(meterRegistry);
}
@Scheduled(fixedDelay = 5000)
public void measure(){
Metrics.gauge("executorCompletedTaskCount",((ThreadPoolTaskExecutor) taskExecutor).getThreadPoolExecutor().getCompletedTaskCount());
Metrics.gauge("executorTotalTaskCount",((ThreadPoolTaskExecutor) taskExecutor).getThreadPoolExecutor().getTaskCount());
Metrics.gauge("executorCurrentNumberOfThreads",((ThreadPoolTaskExecutor) taskExecutor).getThreadPoolExecutor().getPoolSize());
}
}
我在这里注册了几个仪表,并期望它们在每个请求中更新一次,或者......方法度量是最后一次尝试找到正确的方法来监控和更新值。
所以问题是:
- 仪表会自动更新吗?如果是 - 多久一次?
- 如果仪表没有自动更新 - 那么如何正确更新它们?
- 量规是否适合此类测量?AFAIK 我不能使用计数器,因为某些值可以递减。
- 创建所有仪表后,我是否需要执行 Metrics.addRegistry(meterRegistry) ?