【问题标题】:Monitoring threadpool with gauge from dropWizard metric使用 dropWizard 指标中的仪表监控线程池
【发布时间】:2016-10-27 08:53:34
【问题描述】:

我想使用 DropWizard 中的 Guage 指标来监控我的线程池大小。

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            2,
            2,
            1,
            TimeUnit.MINUTES,
            new ArrayBlockingQueue<Runnable>(100),
            new ThreadPoolExecutor.DiscardPolicy());

    metricRegistry.register(name(ThreadPoolExecutor.class, "ThreadPoolRemainingCapacity"), (Gauge<Integer>) () -> threadPoolExecutor.getQueue().remainingCapacity());
    metricRegistry.register(name(ThreadPoolExecutor.class, "ThreadPoolOccupiedCapacity"), (Gauge<Integer>) () -> threadPoolExecutor.getQueue().size());

    return threadPoolExecutor;

据我了解,这是在间隔时间内自动完成的。但似乎我没有得到我注册指标的任何监控数据。尽管我的应用程序中的其他指标(例如计数器和计时器)工作正常。

谁能帮我弄错地方?

谢谢。

【问题讨论】:

  • 您的指标寄存器来自哪里?您如何报告?

标签: java dropwizard gauge


【解决方案1】:

您应该注册一个实现 MetricSet 接口的类,例如(这里有一些您可以忽略的 spring 注释):

@Component
public class ThreadPoolGaugeSet implements MetricSet {

    @Autowired 
    @Qualifier( "taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired 
    @Qualifier( "taskExecutorLowPrio")
    private ThreadPoolTaskExecutor lowPriotaskExecutor;

    @Override
    public Map<String, Metric> getMetrics() {
        final Map<String, Metric> gauges = new HashMap<>();

        gauges.put( "highPrioActive", (Gauge<Long>) () -> (long) taskExecutor.getActiveCount() );
        gauges.put( "lowPrioActive",  (Gauge<Long>) () -> (long) lowPriotaskExecutor.getActiveCount() );
        return gauges;
    }
}

...

metricRegistry.register( "threadpool", poolGauge );

【讨论】:

    【解决方案2】:

    您的代码应该已经正确。你可以做以下调试:

    • 检查您的指标是否显示在&lt;admin_url&gt;/metrics(如果您在计算机上运行,​​则默认为localhost:8081/metrics)。如果您将指标发送给 prometheus 或 datadog 等第三方,通常会有一些过滤。
    • 确保使用正确的MetricRegistry 实例来注册您的指标。通常只使用environment.metrics()
    • 您可能希望暂时为您的指标提供一个较短的名称以帮助调试。 register 的第一个参数接受纯字符串。

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      相关资源
      最近更新 更多