You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2022/03/24 09:02:45 UTC

[skywalking-java] branch main updated: Make sure Tomcat thread plugin working in Tomcat 9 (#128)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 8a0f797  Make sure Tomcat thread plugin working in Tomcat 9 (#128)
8a0f797 is described below

commit 8a0f79743cee6bc429218928f6afb296ed758ea4
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Thu Mar 24 17:02:37 2022 +0800

    Make sure Tomcat thread plugin working in Tomcat 9 (#128)
---
 .../tomcat-thread-pool-plugin/pom.xml              | 11 ++++++
 .../pool/TomcatThreadExecutorInterceptor.java      | 45 ++++++++++++----------
 .../scenarios/h2-scenario/config/expectedData.yaml | 34 ++++++++++++++++
 .../plugin/scenarios/h2-scenario/configuration.yml |  2 +
 test/plugin/scenarios/h2-scenario/pom.xml          |  2 +-
 5 files changed, 72 insertions(+), 22 deletions(-)

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 9b5dcc7..06d03b2 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 @@
     <name>tomcat-thread-pool-plugin</name>
     <url>http://maven.apache.org</url>
 
+    <properties>
+        <tomcat.version>8.0.36</tomcat.version>
+    </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.tomcat.embed</groupId>
+            <artifactId>tomcat-embed-core</artifactId>
+            <version>${tomcat.version}</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
 </project>
\ 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 b97f219..c468ba4 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<String, Function<ThreadPoolExecutor, Supplier<Double>>> METRIC_MAP = new HashMap<String, Function<ThreadPoolExecutor, Supplier<Double>>>() {{
-        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 fecc792..551771a 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 ea3d01a..93b923f 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 2622b42..bd65d36 100644
--- a/test/plugin/scenarios/h2-scenario/pom.xml
+++ b/test/plugin/scenarios/h2-scenario/pom.xml
@@ -31,7 +31,7 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <compiler.version>1.8</compiler.version>
         <test.framework.version>1.4.177</test.framework.version>
-        <spring.boot.version>2.1.6.RELEASE</spring.boot.version>
+        <spring.boot.version>2.5.6</spring.boot.version>
         <lombok.version>1.18.20</lombok.version>
     </properties>