/*
 * Copyright 2017 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.orca.telemetry;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.BiConsumer;
import java.util.function.Function;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Registry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import static java.lang.String.format;

@Component
public class ThreadPoolMetricsPostProcessor implements BeanPostProcessor {

  private final Registry registry;

  @Autowired
  public ThreadPoolMetricsPostProcessor(Registry registry) {this.registry = registry;}

  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) {
    if (bean instanceof ThreadPoolTaskExecutor) {
      applyThreadPoolMetrics((ThreadPoolTaskExecutor) bean, beanName);
    }
    return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) {
    return bean;
  }

  private void applyThreadPoolMetrics(ThreadPoolTaskExecutor executor,
                                      String threadPoolName) {
    BiConsumer<String, Function<ThreadPoolExecutor, Integer>> createGauge =
      (name, fn) -> {
        Id id = registry
          .createId(format("threadpool.%s", name))
          .withTag("id", threadPoolName);

        registry.gauge(id, executor, ref -> fn.apply(ref.getThreadPoolExecutor()));
      };

    createGauge.accept("activeCount", ThreadPoolExecutor::getActiveCount);
    createGauge.accept("maximumPoolSize", ThreadPoolExecutor::getMaximumPoolSize);
    createGauge.accept("corePoolSize", ThreadPoolExecutor::getCorePoolSize);
    createGauge.accept("poolSize", ThreadPoolExecutor::getPoolSize);
    createGauge.accept("blockingQueueSize", e -> e.getQueue().size());
  }
}

@@ -1036,24 +1036,98 @@
 skExecutor;%0A
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;%0A
 import org.s
@@ -1163,16 +1163,16 @@
 ponent;%0A
-
 import s
@@ -1636,24 +1636,154 @@
  beanName);%0A
+    %7D else if (bean instanceof ThreadPoolTaskScheduler) %7B%0A      applyThreadPoolMetrics((ThreadPoolTaskScheduler) bean, beanName);%0A
     %7D%0A    re
@@ -2262,16 +2262,16 @@
 Name);%0A%0A
-
         
@@ -2356,16 +2356,851 @@
     %7D;%0A%0A
+    createGauge.accept(%22activeCount%22, ThreadPoolExecutor::getActiveCount);%0A    createGauge.accept(%22maximumPoolSize%22, ThreadPoolExecutor::getMaximumPoolSize);%0A    createGauge.accept(%22corePoolSize%22, ThreadPoolExecutor::getCorePoolSize);%0A    createGauge.accept(%22poolSize%22, ThreadPoolExecutor::getPoolSize);%0A    createGauge.accept(%22blockingQueueSize%22, e -%3E e.getQueue().size());%0A  %7D%0A%0A  private void applyThreadPoolMetrics(ThreadPoolTaskScheduler executor,%0A                                      String threadPoolName) %7B%0A    BiConsumer%3CString, Function%3CThreadPoolExecutor, Integer%3E%3E createGauge =%0A      (name, fn) -%3E %7B%0A        Id id = registry%0A          .createId(format(%22threadpool.%25s%22, name))%0A          .withTag(%22id%22, threadPoolName);%0A%0A        registry.gauge(id, executor, ref -%3E fn.apply(ref.getScheduledThreadPoolExecutor()));%0A      %7D;%0A%0A
     crea

Add the metrics for the task scheduler thread pool