From d189b282100c7b38d61fb1bf14c1e22408765c22 Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Sat, 26 Mar 2022 00:58:41 +0800 Subject: [PATCH] Dynamic thread pool access prometheus monitoring (#96) --- .../hippo4j-core-spring-boot-starter/pom.xml | 7 +++ .../starter/monitor/MetricMonitorHandler.java | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml index 0def371a..6718084c 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml @@ -78,6 +78,13 @@ spring-boot-configuration-processor true + + + io.micrometer + micrometer-core + 1.8.4 + true + diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/monitor/MetricMonitorHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/monitor/MetricMonitorHandler.java index 1dec2b05..419b9199 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/monitor/MetricMonitorHandler.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/monitor/MetricMonitorHandler.java @@ -1,7 +1,16 @@ package cn.hippo4j.core.starter.monitor; +import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.model.PoolRunStateInfo; import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler; +import cn.hutool.core.bean.BeanUtil; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import io.micrometer.core.instrument.Metrics; +import io.micrometer.core.instrument.Tag; +import org.springframework.core.env.Environment; + +import java.util.Map; /** * Metric monitor handler. @@ -11,13 +20,51 @@ import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler; */ public class MetricMonitorHandler extends AbstractDynamicThreadPoolMonitor { + private final static String METRIC_NAME_PREFIX = "dynamic.thread-pool"; + + private final static String DYNAMIC_THREAD_POOL_ID_TAG = METRIC_NAME_PREFIX + ".id"; + + private final static String APPLICATION_NAME_TAG = "application.name"; + + private final Map RUN_STATE_CACHE = Maps.newConcurrentMap(); + public MetricMonitorHandler(ThreadPoolRunStateHandler threadPoolRunStateHandler) { super(threadPoolRunStateHandler); } @Override protected void execute(PoolRunStateInfo poolRunStateInfo) { + PoolRunStateInfo stateInfo = RUN_STATE_CACHE.get(poolRunStateInfo.getTpId()); + if (stateInfo == null) { + RUN_STATE_CACHE.put(poolRunStateInfo.getTpId(), poolRunStateInfo); + } else { + BeanUtil.copyProperties(poolRunStateInfo, stateInfo); + } + + Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); + String applicationName = environment.getProperty("spring.application.name", "application"); + Iterable tags = Lists.newArrayList( + Tag.of(DYNAMIC_THREAD_POOL_ID_TAG, poolRunStateInfo.getTpId()), + Tag.of(APPLICATION_NAME_TAG, applicationName) + ); + + // thread pool + Metrics.gauge(metricName("core.size"), tags, poolRunStateInfo, PoolRunStateInfo::getCoreSize); + Metrics.gauge(metricName("maximum.size"), tags, poolRunStateInfo, PoolRunStateInfo::getMaximumSize); + Metrics.gauge(metricName("current.size"), tags, poolRunStateInfo, PoolRunStateInfo::getPoolSize); + Metrics.gauge(metricName("largest.size"), tags, poolRunStateInfo, PoolRunStateInfo::getLargestPoolSize); + Metrics.gauge(metricName("active.size"), tags, poolRunStateInfo, PoolRunStateInfo::getActiveSize); + // queue + Metrics.gauge(metricName("queue.size"), tags, poolRunStateInfo, PoolRunStateInfo::getQueueSize); + Metrics.gauge(metricName("queue.capacity"), tags, poolRunStateInfo, PoolRunStateInfo::getQueueCapacity); + Metrics.gauge(metricName("queue.remaining.capacity"), tags, poolRunStateInfo, PoolRunStateInfo::getQueueRemainingCapacity); + // other + Metrics.gauge(metricName("completed.task.count"), tags, poolRunStateInfo, PoolRunStateInfo::getCompletedTaskCount); + Metrics.gauge(metricName("reject.count"), tags, poolRunStateInfo, PoolRunStateInfo::getRejectCount); + } + private String metricName(String name) { + return String.join(".", METRIC_NAME_PREFIX, name); } @Override