千分尺从未更新

时间:2019-11-18 16:33:32

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

我正在尝试为千分尺建立一个自定义量规,该量规应监视自定义ThreadPoolTask​​Executor。问题是仪表值保持不变,并且自应用程序启动以来就从未更新过。但是,执行程序统计信息(在调试器中可以看到)显示更改的值。

这是我的仪表配置的班级。

@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());
   }
}

我在这里注册了多个量规,并希望每个请求或...对它们进行一次更新。 该方法是最后一次尝试找到监视和更新值的正确方法。

所以问题是:

  1. 仪表是否自动更新?如果可以-多久一次?
  2. 如果量表没有自动更新-那么如何更新它们 正确吗?
  3. 测量这样的测量是否正确的对象?我无法使用的AFAIK 计数器,因为某些值可以递减。
  4. 我毕竟需要做Metrics.addRegistry(meterRegistry)吗? 量表已创建?

2 个答案:

答案 0 :(得分:0)

问题是Gauge.builder()方法返回量规本身,而传递到MeterRegistry meterRegistry的{​​{1}}对象具有方法 @Override public void bindTo(MeterRegistry meterRegistry) 可以返回对对象值的引用:

meterRegistry.gauge()

然后可以使用预定方法进行修改。

完整的代码如下:

AtomicDouble executorCompletedTaskCount = meterRegistry.gauge("executor.completed.task.count", new AtomicDouble(completedTaskCount));

希望这是更新量规值的正确方法。

答案 1 :(得分:0)

尝试使用.strongReference(true)

示例:

Gauge.builder("executorTotalTaskCount", taskCount, Double::new)
    .strongReference(true)
    .description(String.format(" Returns the approximate total number of tasks that have ever been scheduled for execution by %s executor", threadNamePrefix.toLowerCase()))
    .register(meterRegistry);