Spring Boot 千分尺从未更新

lndjwyie  于 2023-01-05  发布在  Spring
关注(0)|答案(3)|浏览(237)

我正在尝试为千分尺构建一个自定义的量规,它应该监视自定义的ThreadPoolTaskExecutor。问题是量规值保持不变,并且自应用程序启动以来从未更新过。但是,执行程序统计信息(可以在调试器中看到)显示了变化的值。
这是我的类与 Jmeter 配置。

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

我在这里注册了几个 Jmeter ,并希望他们被更新一次,每次请求或...方法措施是我最后一次尝试找到正确的方法来监测和更新值。
所以问题是:

  1. Jmeter 是否自动更新?如果是-多久更新一次?
    1.如果 Jmeter 不能自动更新-那么如何正确更新它们?
    1.量规是否是此类测量的正确对象?AFAIK I无法使用计数器,因为某些值可能会递减。
    1.在创建所有量表之后,我需要执行Metrics.addRegistry(meterRegistry)吗?
9jyewag0

9jyewag01#

问题是Gauge.builder()方法返回 Jmeter 本身,而传递给@Override public void bindTo(MeterRegistry meterRegistry)MeterRegistry meterRegistry对象具有方法meterRegistry.gauge(),可以返回对对象值的引用:

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

然后可以以预定的方法来修改。
完整的代码如下所示:

@Component
public class ThreadPoolMetricProbe implements MeterBinder {

@Inject
private Executor taskExecutor;

private AtomicDouble executorCompletedTaskCount;
private AtomicDouble executorMaximumPoolSize;
private AtomicDouble executorLargestThreadsNumberEver;
private AtomicDouble executorCurrentNumberOfThreads;
private AtomicDouble executorTotalTaskCount;
private AtomicDouble executorActiveThreadsCount;

@Override
public void bindTo(MeterRegistry meterRegistry) {
    ThreadPoolTaskExecutor exec = (ThreadPoolTaskExecutor) taskExecutor;
    long completedTaskCount = exec.getThreadPoolExecutor().getCompletedTaskCount();
    int largestPoolSizeEver = exec.getThreadPoolExecutor().getLargestPoolSize();
    int maximumPoolSize = exec.getThreadPoolExecutor().getMaximumPoolSize();
    int currentNumberOfThreads = exec.getThreadPoolExecutor().getPoolSize();
    long totalTaskCountEver = exec.getThreadPoolExecutor().getTaskCount();
    int activeThreadsCount = exec.getThreadPoolExecutor().getActiveCount();

    executorCompletedTaskCount = meterRegistry.gauge("executor.completed.task.count", new AtomicDouble(completedTaskCount));
    executorMaximumPoolSize = meterRegistry.gauge("executor.maximum.pool.size", new AtomicDouble(maximumPoolSize));
    executorLargestThreadsNumberEver = meterRegistry.gauge("executor.largest.threads.number.ever", new AtomicDouble(largestPoolSizeEver));
    executorCurrentNumberOfThreads = meterRegistry.gauge("executor.current.number.of.threads", new AtomicDouble(currentNumberOfThreads));
    executorTotalTaskCount = meterRegistry.gauge("executor.total.tasks.ever.countunt", new AtomicDouble(totalTaskCountEver));
    executorActiveThreadsCount = meterRegistry.gauge("executor.active.threads.count", new AtomicDouble(activeThreadsCount));

}

@Scheduled(fixedDelay = 5000)
public void measure(){
    ThreadPoolExecutor threadPoolExecutor = ((ThreadPoolTaskExecutor) taskExecutor).getThreadPoolExecutor();
    executorCompletedTaskCount.set(threadPoolExecutor.getCompletedTaskCount());
    executorMaximumPoolSize.set(threadPoolExecutor.getMaximumPoolSize());
    executorLargestThreadsNumberEver.set(threadPoolExecutor.getLargestPoolSize());
    executorCurrentNumberOfThreads.set(threadPoolExecutor.getPoolSize());
    executorTotalTaskCount.set(threadPoolExecutor.getTaskCount());
    executorActiveThreadsCount.set(threadPoolExecutor.getActiveCount());
}

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

q7solyqu

q7solyqu2#

meterRegistry.gauge("key_name", double)

这应该会给予你一个计数器。2你可以在主处理发生之前和之后增加和减少这个计数器。

li9yvcax

li9yvcax3#

请尝试使用.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);

相关问题