public static void attach()

in spectator-api/src/main/java/com/netflix/spectator/api/patterns/ThreadPoolMonitor.java [121:175]


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