|
|
@@ -12,6 +12,10 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import io.micrometer.core.instrument.Gauge;
|
|
|
+import io.micrometer.core.instrument.MeterRegistry;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
import java.util.List;
|
|
|
@@ -95,6 +99,9 @@ public class DynamicThreadPoolManager {
|
|
|
@Value("${thread.pool.monitor.queue.threshold:0.8}")
|
|
|
private double queueUsageThreshold;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private MeterRegistry meterRegistry;
|
|
|
+
|
|
|
/**
|
|
|
* 定时监控调度器
|
|
|
*/
|
|
|
@@ -209,10 +216,56 @@ public class DynamicThreadPoolManager {
|
|
|
private void initThreadPool(DynamicThreadPoolConfig config) {
|
|
|
ThreadPoolExecutor executor = createThreadPoolExecutor(config);
|
|
|
threadPoolRegistry.put(config.getPoolName(), executor);
|
|
|
+ registerMetrics(executor, config.getPoolName());
|
|
|
log.info("Thread pool [{}] initialized: coreSize={}, maxSize={}, queueCapacity={}",
|
|
|
config.getPoolName(), config.getCorePoolSize(), config.getMaxPoolSize(), config.getQueueCapacity());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 注册线程池指标到 Micrometer
|
|
|
+ */
|
|
|
+ private void registerMetrics(ThreadPoolExecutor executor, String poolName) {
|
|
|
+ Gauge.builder("threadpool.core.size", executor, ThreadPoolExecutor::getCorePoolSize)
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Core pool size")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.max.size", executor, ThreadPoolExecutor::getMaximumPoolSize)
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Maximum pool size")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.active.count", executor, ThreadPoolExecutor::getActiveCount)
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Active thread count")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.pool.size", executor, ThreadPoolExecutor::getPoolSize)
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Current pool size")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.queue.size", executor, e -> e.getQueue().size())
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Queue size")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.queue.capacity", () -> getQueueCapacity(poolName))
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Queue capacity")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.completed.tasks", executor, ThreadPoolExecutor::getCompletedTaskCount)
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Completed task count")
|
|
|
+ .register(meterRegistry);
|
|
|
+
|
|
|
+ Gauge.builder("threadpool.task.count", executor, ThreadPoolExecutor::getTaskCount)
|
|
|
+ .tag("pool", poolName)
|
|
|
+ .description("Total task count")
|
|
|
+ .register(meterRegistry);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 创建线程池
|
|
|
*/
|