3

I want to use Guage metric from DropWizard to monitor my thread pool size.

        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;

From what I understand, this is done automatically in an interval time. But it seems that I don't get any monitoring data for my registered metrics. Although other metrics in my application such as counter and timer work just fine.

Can someone help me where I got it wrong?

Thank you.

Xitrum
  • 7,765
  • 26
  • 90
  • 126

2 Answers2

2

You should register a class which implements the MetricSet interface like (this on has a few spring annotation you could ignore):

@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 );
stacker
  • 68,052
  • 28
  • 140
  • 210
0

Your code should already be correct. You might do following to debug it:

  • Check whether your metrics appears at <admin_url>/metrics (if you're running on your machine it's default to localhost:8081/metrics). Usually there is some filtering if you send your metrics to 3rd party like prometheus or datadog.
  • Make sure to use the correct MetricRegistry instance for registering your metrics. Usually just use environment.metrics().
  • You might want to give a shorter name to your metrics temporarily to help debugging. The first parameter of register accepts plain string.
cakraww
  • 2,493
  • 28
  • 30