Skip to content

Commit 9e26f26

Browse files
authored
Support monitor grizzly thread pool (#533)
1 parent de730d7 commit 9e26f26

File tree

20 files changed

+686
-0
lines changed

20 files changed

+686
-0
lines changed

.github/workflows/plugins-jdk17-test.0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ jobs:
6161
- jetty-thread-pool-scenario
6262
- jetty-11.x-thread-pool-scenario
6363
- grizzly-2.3.x-4.x-scenario
64+
- grizzly-2.3.x-4.x-workthreadpool-scenario
6465
steps:
6566
- uses: actions/checkout@v2
6667
with:

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Release Notes.
1717
* Change the classloader to locate the agent path in AgentPackagePath, from `SystemClassLoader` to AgentPackagePath's loader.
1818
* Support Grizzly Trace
1919
* Fix possible IllegalStateException when using Micrometer.
20+
* Support Grizzly Work ThreadPool Metric Monitor
2021

2122
#### Documentation
2223

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to You under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
~
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>org.apache.skywalking</groupId>
25+
<artifactId>apm-sdk-plugin</artifactId>
26+
<version>8.16.0-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>apm-grizzly-2.x-4.x-work-threadpool-plugin</artifactId>
30+
31+
<properties>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
<grizzly.version>2.4.0</grizzly.version>
34+
</properties>
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.glassfish.grizzly</groupId>
38+
<artifactId>grizzly-framework</artifactId>
39+
<version>${grizzly.version}</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.grizzly.workthreadpool;
20+
21+
import org.apache.skywalking.apm.agent.core.meter.MeterFactory;
22+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
25+
import org.glassfish.grizzly.threadpool.AbstractThreadPool;
26+
import org.glassfish.grizzly.threadpool.GrizzlyExecutorService;
27+
import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
28+
29+
import java.lang.reflect.Field;
30+
import java.lang.reflect.Method;
31+
32+
public class TransportInterceptor implements InstanceMethodsAroundInterceptor {
33+
34+
private static final String METER_NAME = "thread_pool";
35+
private static final String METRIC_POOL_NAME_TAG_NAME = "pool_name";
36+
private static final String THREAD_POOL_NAME = "grizzly_execute_pool";
37+
private static final String METRIC_TYPE_TAG_NAME = "metric_type";
38+
39+
@Override
40+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
41+
GrizzlyExecutorService executorServices = (GrizzlyExecutorService) allArguments[0];
42+
// reflect get private field just once
43+
Field poolField = GrizzlyExecutorService.class.getDeclaredField("pool");
44+
poolField.setAccessible(true);
45+
AbstractThreadPool abstractThreadPool = (AbstractThreadPool) poolField.get(executorServices);
46+
ThreadPoolConfig threadPoolConfig = abstractThreadPool.getConfig();
47+
MeterFactory.gauge(METER_NAME, () -> (double) threadPoolConfig.getCorePoolSize())
48+
.tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME)
49+
.tag(METRIC_TYPE_TAG_NAME, "core_pool_size")
50+
.build();
51+
MeterFactory.gauge(METER_NAME, () -> (double) threadPoolConfig.getMaxPoolSize())
52+
.tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME)
53+
.tag(METRIC_TYPE_TAG_NAME, "max_pool_size")
54+
.build();
55+
MeterFactory.gauge(METER_NAME, () -> (double) abstractThreadPool.getSize())
56+
.tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME)
57+
.tag(METRIC_TYPE_TAG_NAME, "pool_size")
58+
.build();
59+
MeterFactory.gauge(METER_NAME, () -> (double) abstractThreadPool.getQueue().size())
60+
.tag(METRIC_POOL_NAME_TAG_NAME, THREAD_POOL_NAME)
61+
.tag(METRIC_TYPE_TAG_NAME, "queue_size")
62+
.build();
63+
}
64+
65+
@Override
66+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
67+
return ret;
68+
}
69+
70+
@Override
71+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
72+
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.skywalking.apm.plugin.grizzly.workthreadpool.define;
19+
20+
import org.apache.skywalking.apm.agent.core.plugin.WitnessMethod;
21+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
22+
23+
import java.util.Collections;
24+
import java.util.List;
25+
26+
import static net.bytebuddy.matcher.ElementMatchers.named;
27+
28+
public abstract class AbstractWitnessInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
29+
30+
@Override
31+
protected List<WitnessMethod> witnessMethods() {
32+
return Collections.singletonList(new WitnessMethod(
33+
"org.glassfish.grizzly.http.server.HttpHandler",
34+
named("runService")
35+
));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.grizzly.workthreadpool.define;
20+
21+
import net.bytebuddy.description.method.MethodDescription;
22+
import net.bytebuddy.matcher.ElementMatcher;
23+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
25+
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
26+
27+
import static net.bytebuddy.matcher.ElementMatchers.named;
28+
import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName;
29+
30+
public class TransportInstrumentation extends AbstractWitnessInstrumentation {
31+
private static final String ENHANCE_CLASS = "org.glassfish.grizzly.AbstractTransport";
32+
33+
private static final String INTERCEPTOR_CLASS = "org.apache.skywalking.apm.plugin.grizzly.workthreadpool.TransportInterceptor";
34+
35+
@Override
36+
protected ClassMatch enhanceClass() {
37+
return byName(ENHANCE_CLASS);
38+
}
39+
40+
@Override
41+
public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
42+
return new ConstructorInterceptPoint[0];
43+
}
44+
45+
@Override
46+
public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
47+
return new InstanceMethodsInterceptPoint[]{
48+
new InstanceMethodsInterceptPoint() {
49+
@Override
50+
public ElementMatcher<MethodDescription> getMethodsMatcher() {
51+
return named("setWorkerThreadPool0");
52+
}
53+
54+
@Override
55+
public String getMethodsInterceptor() {
56+
return INTERCEPTOR_CLASS;
57+
}
58+
59+
@Override
60+
public boolean isOverrideArgs() {
61+
return false;
62+
}
63+
}
64+
};
65+
}
66+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
grizzly-2.3.x-4.x-threadpool=org.apache.skywalking.apm.plugin.grizzly.workthreadpool.define.TransportInstrumentation

apm-sniffer/apm-sdk-plugin/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<module>jersey-2.x-plugin</module>
131131
<module>jersey-3.x-plugin</module>
132132
<module>grizzly-2.3.x-4.x-plugin</module>
133+
<module>grizzly-2.3.x-4.x-work-threadpool-plugin</module>
133134
</modules>
134135
<packaging>pom</packaging>
135136

docs/en/setup/service-agent/java-agent/Plugin-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,4 @@
162162
- jersey-2.x
163163
- jersey-3.x
164164
- grizzly-2.3.x-4.x
165+
- grizzly-2.3.x-4.x-threadpool

docs/en/setup/service-agent/java-agent/Supported-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ The meter plugin provides the advanced metrics collections, which are not a part
158158
* [Tomcat](https://github.com/apache/tomcat) 7.0.x -> 10.0.x
159159
* [Dubbo](https://github.com/apache/dubbo) 2.5.x -> 2.7.x
160160
* [Jetty](https://github.com/eclipse/jetty.project) 9.1.x -> 11.x
161+
* [Grizzly](https://github.com/eclipse-ee4j/grizzly) 2.3.x -> 4.x
161162
___
162163
¹Due to license incompatibilities/restrictions these plugins are hosted and released in 3rd part repository,
163164
go to [SkyAPM java plugin extension repository](https://github.com/SkyAPM/java-plugin-extensions) to get these.

0 commit comments

Comments
 (0)