Skip to content

Commit a5ccbca

Browse files
authored
[Feature] Add JDK ThreadPoolExecutor plugin(#8743) (#146)
1 parent fab9ce5 commit a5ccbca

File tree

25 files changed

+1139
-2
lines changed

25 files changed

+1139
-2
lines changed

.github/workflows/plugins-test.3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ jobs:
9898
- tomcat-thread-pool-scenario
9999
- guava-eventbus-scenario
100100
- shenyu-2.4.x-scenario
101+
- jdk-threadpool-scenario
101102
steps:
102103
- uses: actions/checkout@v2
103104
with:

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Release Notes.
3636
* Make sure the parent endpoint in tracing context from existing first ENTRY span, rather than first span only.
3737
* Fix the bug that maybe causing memory leak and repeated traceId when use gateway-2.1.x-plugin or gateway-3.x-plugin.
3838
* Fix Grpc 1.x plugin could leak context due to gRPC cancelled.
39+
* Add JDK ThreadPoolExecutor Plugin.
3940

4041
#### Documentation
4142

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/logging/core/WriterFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
2323
import org.apache.skywalking.apm.agent.core.conf.Config;
2424
import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
25+
import org.apache.skywalking.apm.agent.core.plugin.PluginFinder;
2526
import org.apache.skywalking.apm.util.StringUtil;
2627

2728
public class WriterFactory {
@@ -35,7 +36,9 @@ public static IWriter getLogWriter() {
3536
if (WRITER != null) {
3637
return WRITER;
3738
}
38-
if (SnifferConfigInitializer.isInitCompleted() && AgentPackagePath.isPathFound()) {
39+
if (SnifferConfigInitializer.isInitCompleted()
40+
&& PluginFinder.isPluginInitCompleted()
41+
&& AgentPackagePath.isPathFound()) {
3942
if (StringUtil.isEmpty(Config.Logging.DIR)) {
4043
try {
4144
Config.Logging.DIR = AgentPackagePath.getPath() + "/logs";

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class PluginFinder {
4343
private final Map<String, LinkedList<AbstractClassEnhancePluginDefine>> nameMatchDefine = new HashMap<String, LinkedList<AbstractClassEnhancePluginDefine>>();
4444
private final List<AbstractClassEnhancePluginDefine> signatureMatchDefine = new ArrayList<AbstractClassEnhancePluginDefine>();
4545
private final List<AbstractClassEnhancePluginDefine> bootstrapClassMatchDefine = new ArrayList<AbstractClassEnhancePluginDefine>();
46+
private static boolean IS_PLUGIN_INIT_COMPLETED = false;
4647

4748
public PluginFinder(List<AbstractClassEnhancePluginDefine> plugins) {
4849
for (AbstractClassEnhancePluginDefine plugin : plugins) {
@@ -107,4 +108,12 @@ public boolean matches(NamedElement target) {
107108
public List<AbstractClassEnhancePluginDefine> getBootstrapClassMatchDefine() {
108109
return bootstrapClassMatchDefine;
109110
}
111+
112+
public static void pluginInitCompleted() {
113+
IS_PLUGIN_INIT_COMPLETED = true;
114+
}
115+
116+
public static boolean isPluginInitCompleted() {
117+
return IS_PLUGIN_INIT_COMPLETED;
118+
}
110119
}

apm-sniffer/apm-agent-core/src/test/java/org/apache/skywalking/apm/agent/core/logging/core/WriterFactoryTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
2222
import org.apache.skywalking.apm.agent.core.conf.Config;
2323
import org.apache.skywalking.apm.agent.core.conf.SnifferConfigInitializer;
24+
import org.apache.skywalking.apm.agent.core.plugin.PluginFinder;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627
import org.mockito.BDDMockito;
@@ -33,6 +34,7 @@
3334
@RunWith(PowerMockRunner.class)
3435
@PrepareForTest(value = {
3536
SnifferConfigInitializer.class,
37+
PluginFinder.class,
3638
AgentPackagePath.class
3739
})
3840
public class WriterFactoryTest {
@@ -41,11 +43,14 @@ public class WriterFactoryTest {
4143
public void alwaysReturnSystemLogWriteWithSetLoggingDir() {
4244
Config.Logging.OUTPUT = LogOutput.CONSOLE;
4345
PowerMockito.mockStatic(SnifferConfigInitializer.class);
46+
PowerMockito.mockStatic(PluginFinder.class);
4447
PowerMockito.mockStatic(AgentPackagePath.class);
4548
BDDMockito.given(SnifferConfigInitializer.isInitCompleted()).willReturn(true);
49+
BDDMockito.given(PluginFinder.isPluginInitCompleted()).willReturn(true);
4650
BDDMockito.given(AgentPackagePath.isPathFound()).willReturn(true);
4751

4852
assertTrue(SnifferConfigInitializer.isInitCompleted());
53+
assertTrue(PluginFinder.isPluginInitCompleted());
4954
assertTrue(AgentPackagePath.isPathFound());
5055

5156
IWriter logWriter = WriterFactory.getLogWriter();
@@ -56,11 +61,14 @@ public void alwaysReturnSystemLogWriteWithSetLoggingDir() {
5661
public void returnFileWriterWriteWithBlankLoggingDir() {
5762
Config.Logging.OUTPUT = LogOutput.FILE;
5863
PowerMockito.mockStatic(SnifferConfigInitializer.class);
64+
PowerMockito.mockStatic(PluginFinder.class);
5965
PowerMockito.mockStatic(AgentPackagePath.class);
6066
BDDMockito.given(SnifferConfigInitializer.isInitCompleted()).willReturn(true);
67+
BDDMockito.given(PluginFinder.isPluginInitCompleted()).willReturn(true);
6168
BDDMockito.given(AgentPackagePath.isPathFound()).willReturn(true);
6269

6370
assertTrue(SnifferConfigInitializer.isInitCompleted());
71+
assertTrue(PluginFinder.isPluginInitCompleted());
6472
assertTrue(AgentPackagePath.isPathFound());
6573

6674
IWriter logWriter = WriterFactory.getLogWriter();

apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public static void premain(String agentArgs, Instrumentation instrumentation) th
130130
.with(new Listener())
131131
.installOn(instrumentation);
132132

133+
PluginFinder.pluginInitCompleted();
134+
133135
try {
134136
ServiceManager.INSTANCE.boot();
135137
} catch (Exception e) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
<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+
<parent>
23+
<artifactId>bootstrap-plugins</artifactId>
24+
<groupId>org.apache.skywalking</groupId>
25+
<version>8.10.0-SNAPSHOT</version>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
29+
<artifactId>apm-jdk-threadpool-plugin</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<name>apm-jdk-threadpool-plugin</name>
33+
<url>http://maven.apache.org</url>
34+
35+
<properties>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
</properties>
38+
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<artifactId>maven-deploy-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
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 java.lang.reflect.Method;
26+
27+
public abstract class AbstractThreadingPoolInterceptor implements InstanceMethodsAroundInterceptor {
28+
@Override
29+
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
30+
if (!ContextManager.isActive()) {
31+
return;
32+
}
33+
34+
if (allArguments == null || allArguments.length < 1) {
35+
return;
36+
}
37+
38+
Object argument = allArguments[0];
39+
40+
Object wrappedObject = wrap(argument);
41+
if (wrappedObject != null) {
42+
allArguments[0] = wrappedObject;
43+
}
44+
}
45+
46+
/**
47+
* wrap the Callable or Runnable object if needed
48+
* @param param Callable or Runnable object
49+
* @return Wrapped object or null if not needed to wrap
50+
*/
51+
public abstract Object wrap(Object param);
52+
53+
@Override
54+
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable {
55+
return ret;
56+
}
57+
58+
@Override
59+
public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Throwable t) {
60+
ContextManager.activeSpan().log(t);
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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;
20+
21+
import org.apache.skywalking.apm.plugin.wrapper.SwRunnableWrapper;
22+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
23+
import java.util.concurrent.RunnableFuture;
24+
25+
public class ThreadPoolExecuteMethodInterceptor extends AbstractThreadingPoolInterceptor {
26+
27+
@Override
28+
public Object wrap(Object param) {
29+
if (param instanceof SwRunnableWrapper) {
30+
return null;
31+
}
32+
33+
if (param instanceof RunnableFuture) {
34+
return null;
35+
}
36+
37+
if (!(param instanceof Runnable)) {
38+
return null;
39+
}
40+
41+
Runnable runnable = (Runnable) param;
42+
return new SwRunnableWrapper(runnable, ContextManager.capture());
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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;
20+
21+
import org.apache.skywalking.apm.agent.core.context.ContextManager;
22+
import org.apache.skywalking.apm.plugin.wrapper.SwCallableWrapper;
23+
import org.apache.skywalking.apm.plugin.wrapper.SwRunnableWrapper;
24+
25+
import java.util.concurrent.Callable;
26+
27+
public class ThreadPoolSubmitMethodInterceptor extends AbstractThreadingPoolInterceptor {
28+
29+
@Override
30+
public Object wrap(Object param) {
31+
if (param instanceof SwRunnableWrapper || param instanceof SwCallableWrapper) {
32+
return null;
33+
}
34+
35+
if (param instanceof Callable) {
36+
Callable callable = (Callable) param;
37+
return new SwCallableWrapper(callable, ContextManager.capture());
38+
}
39+
40+
if (param instanceof Runnable) {
41+
Runnable runnable = (Runnable) param;
42+
return new SwRunnableWrapper(runnable, ContextManager.capture());
43+
}
44+
45+
return null;
46+
}
47+
}

0 commit comments

Comments
 (0)