Is there a way to monitor the resilience4j thread pool?

Viewed 204

I'm experimenting with resilience4j and tweaking configurations to optimize performance under load. One of the features I'm taking advantage of is using a Thread Pool Bulkhead and executing functions asynchronously using said thread pool. Is there a way that I can monitor the thread pool state? I'm looking for something to the effect of:

logger.debug(threadPoolBulkhead.getNumActiveThreads());

logger.debug(threadPoolBulkhead.getNumQueuedThreads());

Is something like this possible?

2 Answers

Metrics, programmatically

You can get the required metrics programmatically on your own: ThreadPoolBulkhead#getMetrics.

Metrics via Micrometer or Dropwizard Metrics

You can monitor the current state via metrics as resilience4j natively supports Micrometer and provides exactly the metrics you're interested in, even though this is not explicitly mentioned in the docs. Proof here.

MeterRegistry meterRegistry = new SimpleMeterRegistry();
ThreadPoolBulkheadRegistry bulkheadRegistry = ThreadPoolBulkheadRegistry.ofDefaults();
ThreadPoolBulkhead bulkhead = bulkheadRegistry.bulkhead("backendA");

// Register all retries at once
TaggedThreadPoolBulkheadMetrics
  .ofThreadPoolBulkheadRegistry(bulkheadRegistry)
  .bindTo(meterRegistry);

Events

You can register listeners for bulkhead events to get notified whenever a call is executed/finished: https://resilience4j.readme.io/docs/bulkhead#consume-emitted-bulkheadevents

bulkhead.getEventPublisher()
    .onCallPermitted(event -> logger.info(...))
    .onCallRejected(event -> logger.info(...))
    .onCallFinished(event -> logger.info(...));

Possible with the vendor neutral plugin Micrometer. With the help of this, we can push the metrics to our choice of a system like New Relic etc

Visit: https://micrometer.io/

Related