diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/pom.xml index 9b5dcc746..06d03b2b5 100644 --- a/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/pom.xml @@ -31,5 +31,16 @@ tomcat-thread-pool-plugin http://maven.apache.org + + 8.0.36 + + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + provided + + \ No newline at end of file diff --git a/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat/thread/pool/TomcatThreadExecutorInterceptor.java b/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat/thread/pool/TomcatThreadExecutorInterceptor.java index b97f219ba..c468ba40f 100644 --- a/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat/thread/pool/TomcatThreadExecutorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/tomcat-thread-pool-plugin/src/main/java/org/apache/skywalking/apm/plugin/tomcat/thread/pool/TomcatThreadExecutorInterceptor.java @@ -18,37 +18,40 @@ package org.apache.skywalking.apm.plugin.tomcat.thread.pool; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.function.Function; -import java.util.function.Supplier; import org.apache.skywalking.apm.agent.core.meter.MeterFactory; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; +import org.apache.tomcat.util.threads.ThreadPoolExecutor; public class TomcatThreadExecutorInterceptor implements InstanceConstructorInterceptor { + private static final String METER_NAME = "thread_pool"; + private static final String METRIC_POOL_NAME_TAG_NAME = "pool_name"; private static final String THREAD_POOL_NAME = "tomcat_execute_pool"; - private static final Map>> METRIC_MAP = new HashMap>>() {{ - put("core_pool_size", (ThreadPoolExecutor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getCorePoolSize()); - put("max_pool_size", (ThreadPoolExecutor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getMaximumPoolSize()); - put("pool_size", (ThreadPoolExecutor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getPoolSize()); - put("queue_size", (ThreadPoolExecutor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getQueue().size()); - put("active_size", (ThreadPoolExecutor threadPoolExecutor) -> () -> (double) threadPoolExecutor.getActiveCount()); - }}; + private static final String METRIC_TYPE_TAG_NAME = "metric_type"; @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) throws Throwable { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) objInst; - buildThreadPoolMeterMetric(threadPoolExecutor); - } - - private void buildThreadPoolMeterMetric(ThreadPoolExecutor threadPoolExecutor) { - String threadPoolMeterName = "thread_pool"; - String poolNameTag = "pool_name"; - String metricTypeTag = "metric_type"; - METRIC_MAP.forEach((key, value) -> MeterFactory.gauge(threadPoolMeterName, value.apply(threadPoolExecutor)) - .tag(poolNameTag, THREAD_POOL_NAME).tag(metricTypeTag, key).build()); + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolExecutor.getCorePoolSize()) + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) + .tag(METRIC_TYPE_TAG_NAME, "core_pool_size") + .build(); + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolExecutor.getMaximumPoolSize()) + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) + .tag(METRIC_TYPE_TAG_NAME, "max_pool_size") + .build(); + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolExecutor.getPoolSize()) + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) + .tag(METRIC_TYPE_TAG_NAME, "pool_size") + .build(); + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolExecutor.getQueue().size()) + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) + .tag(METRIC_TYPE_TAG_NAME, "queue_size") + .build(); + MeterFactory.gauge(METER_NAME, () -> (double) threadPoolExecutor.getActiveCount()) + .tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME) + .tag(METRIC_TYPE_TAG_NAME, "active_size") + .build(); } } diff --git a/test/plugin/scenarios/h2-scenario/config/expectedData.yaml b/test/plugin/scenarios/h2-scenario/config/expectedData.yaml index fecc79215..551771a5e 100644 --- a/test/plugin/scenarios/h2-scenario/config/expectedData.yaml +++ b/test/plugin/scenarios/h2-scenario/config/expectedData.yaml @@ -95,3 +95,37 @@ segmentItems: - {key: url, value: 'http://localhost:8080/h2-scenario/case/h2-scenario'} - {key: http.method, value: GET} skipAnalysis: 'false' +meterItems: + - serviceName: h2-scenario + meterSize: 5 + meters: + - meterId: + name: thread_pool + tags: + - {name: metric_type, value: core_pool_size} + - {name: pool_name, value: tomcat_execute_pool} + singleValue: ge 1 + - meterId: + name: thread_pool + tags: + - {name: metric_type, value: max_pool_size} + - {name: pool_name, value: tomcat_execute_pool} + singleValue: ge 1 + - meterId: + name: thread_pool + tags: + - {name: metric_type, value: pool_size} + - {name: pool_name, value: tomcat_execute_pool} + singleValue: ge 0 + - meterId: + name: thread_pool + tags: + - {name: metric_type, value: active_size} + - {name: pool_name, value: tomcat_execute_pool} + singleValue: ge 0 + - meterId: + name: thread_pool + tags: + - {name: metric_type, value: queue_size} + - {name: pool_name, value: tomcat_execute_pool} + singleValue: ge 0 diff --git a/test/plugin/scenarios/h2-scenario/configuration.yml b/test/plugin/scenarios/h2-scenario/configuration.yml index ea3d01ada..93b923fac 100644 --- a/test/plugin/scenarios/h2-scenario/configuration.yml +++ b/test/plugin/scenarios/h2-scenario/configuration.yml @@ -18,3 +18,5 @@ type: jvm entryService: http://localhost:8080/h2-scenario/case/h2-scenario healthCheck: http://localhost:8080/h2-scenario/case/healthCheck startScript: ./bin/startup.sh +environment: + - SW_METER_REPORT_INTERVAL=1 diff --git a/test/plugin/scenarios/h2-scenario/pom.xml b/test/plugin/scenarios/h2-scenario/pom.xml index 2622b4259..bd65d366b 100644 --- a/test/plugin/scenarios/h2-scenario/pom.xml +++ b/test/plugin/scenarios/h2-scenario/pom.xml @@ -31,7 +31,7 @@ UTF-8 1.8 1.4.177 - 2.1.6.RELEASE + 2.5.6 1.18.20