public static void attach(
      final Registry registry,
      final ThreadPoolExecutor threadPool,
      final String threadPoolName) {

    Preconditions.checkNotNull(registry, "registry");
    Preconditions.checkNotNull(threadPool, "threadPool");

    final String idValue;
    if (threadPoolName == null || threadPoolName.isEmpty()) {
      idValue = DEFAULT_ID;
    } else {
      idValue = threadPoolName;
    }

    final Tag idTag = new BasicTag(ID_TAG_NAME, idValue);

    PolledMeter.using(registry)
        .withName(TASK_COUNT)
        .withTag(idTag)
        .monitorMonotonicCounter(threadPool, ThreadPoolExecutor::getTaskCount);
    PolledMeter.using(registry)
        .withName(COMPLETED_TASK_COUNT)
        .withTag(idTag)
        .monitorMonotonicCounter(threadPool, ThreadPoolExecutor::getCompletedTaskCount);
    PolledMeter.using(registry)
        .withName(CURRENT_THREADS_BUSY)
        .withTag(idTag)
        .monitorValue(threadPool, ThreadPoolExecutor::getActiveCount);
    PolledMeter.using(registry)
        .withName(MAX_THREADS)
        .withTag(idTag)
        .monitorValue(threadPool, ThreadPoolExecutor::getMaximumPoolSize);
    PolledMeter.using(registry)
        .withName(POOL_SIZE)
        .withTag(idTag)
        .monitorValue(threadPool, ThreadPoolExecutor::getPoolSize);
    PolledMeter.using(registry)
        .withName(CORE_POOL_SIZE)
        .withTag(idTag)
        .monitorValue(threadPool, ThreadPoolExecutor::getCorePoolSize);
    PolledMeter.using(registry)
        .withName(QUEUE_SIZE)
        .withTag(idTag)
        .monitorValue(threadPool, tp -> tp.getQueue().size());

    // Handler is not allowed to be null, checked internally to thread pool
    Counter rejected = registry.counter(registry.createId(REJECTED_TASK_COUNT).withTag(idTag));
    RejectedExecutionHandler handler = threadPool.getRejectedExecutionHandler();
    RejectedExecutionHandler monitoredHandler = (Runnable r, ThreadPoolExecutor exec) -> {
      rejected.increment();
      handler.rejectedExecution(r, exec);
    };
    threadPool.setRejectedExecutionHandler(monitoredHandler);
  }
public static void attach ( final Registry registry , final ThreadPoolExecutor threadPool , final String threadPoolName ) { Preconditions . checkNotNull ( registry , "registry" ) ; Preconditions . checkNotNull ( threadPool , "threadPool" ) ; final String idValue ; if ( threadPoolName == null || threadPoolName . isEmpty ( ) ) { idValue = DEFAULT_ID ; } else { idValue = threadPoolName ; } final Tag idTag = new BasicTag ( ID_TAG_NAME , idValue ) ; PolledMeter . using ( registry ) . withName ( TASK_COUNT ) . withTag ( idTag ) . monitorMonotonicCounter ( threadPool , ThreadPoolExecutor :: getTaskCount ) ; PolledMeter . using ( registry ) . withName ( COMPLETED_TASK_COUNT ) . withTag ( idTag ) . monitorMonotonicCounter ( threadPool , ThreadPoolExecutor :: getCompletedTaskCount ) ; PolledMeter . using ( registry ) . withName ( CURRENT_THREADS_BUSY ) . withTag ( idTag ) . monitorValue ( threadPool , ThreadPoolExecutor :: getActiveCount ) ; PolledMeter . using ( registry ) . withName ( MAX_THREADS ) . withTag ( idTag ) . monitorValue ( threadPool , ThreadPoolExecutor :: getMaximumPoolSize ) ; PolledMeter . using ( registry ) . withName ( POOL_SIZE ) . withTag ( idTag ) . monitorValue ( threadPool , ThreadPoolExecutor :: getPoolSize ) ; PolledMeter . using ( registry ) . withName ( CORE_POOL_SIZE ) . withTag ( idTag ) . monitorValue ( threadPool , ThreadPoolExecutor :: getCorePoolSize ) ; PolledMeter . using ( registry ) . withName ( QUEUE_SIZE ) . withTag ( idTag ) . monitorValue ( threadPool , tp -> tp . getQueue ( ) . size ( ) ) ; Counter rejected = registry . counter ( registry . createId ( REJECTED_TASK_COUNT ) . withTag ( idTag ) ) ; RejectedExecutionHandler handler = threadPool . getRejectedExecutionHandler ( ) ; RejectedExecutionHandler monitoredHandler = ( Runnable r , ThreadPoolExecutor exec ) -> { rejected . increment ( ) ; handler . rejectedExecution ( r , exec ) ; } ; threadPool . setRejectedExecutionHandler ( monitoredHandler ) ; }
Register the provided thread pool, optionally tagged with a name. If a custom
{@link RejectedExecutionHandler} is going to be used, then it should be set on the pool
prior to attaching. Otherwise it will overwrite the wrapped rejection handler used to
track the number of rejected tasks from this pool.

@param registry
The registry to use.
@param threadPool
The thread pool to monitor.
@param threadPoolName
A name with which to tag the metrics (default name used if {@code null} or empty).