Make sure Tomcat thread plugin working in Tomcat 9 (#128)

This commit is contained in:
吴晟 Wu Sheng 2022-03-24 17:02:37 +08:00 committed by GitHub
parent ff584717d9
commit 8a0f79743c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 22 deletions

View File

@ -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>

View File

@ -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();
}
}

View File

@ -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

View File

@ -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

View File

@ -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>