Skip to content

Commit 0ee0216

Browse files
committed
feat: package all in one agent jar
1 parent 9220c18 commit 0ee0216

File tree

28 files changed

+488
-188
lines changed

28 files changed

+488
-188
lines changed

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/AgentInitializer.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class AgentInitializer {
1010

1111
private static ClassLoader classLoader;
1212

13-
public static void initialize(Instrumentation inst, File agentFile, String agentArgs)
13+
public static void initialize(Instrumentation inst, File agentFile, String agentArgs, ClassLoader parentClassLoader)
1414
throws Exception {
1515
if (classLoader != null) {
1616
return;
@@ -20,7 +20,7 @@ public static void initialize(Instrumentation inst, File agentFile, String agent
2020
System.setProperty(ConfigConstants.SHADED_LOGGER_SHOW_DATE_TIME, "true");
2121
System.setProperty(ConfigConstants.SHADED_LOGGER_DATE_TIME_FORMAT, "yyyy-MM-dd HH:mm:ss:SSS");
2222
File[] extensionFiles = getExtensionJarFiles(agentFile);
23-
classLoader = createAgentClassLoader(agentFile, extensionFiles);
23+
classLoader = new AgentClassLoader(agentFile, parentClassLoader, extensionFiles);
2424
InstrumentationHolder.setAgentClassLoader(classLoader);
2525
InstrumentationHolder.setInstrumentation(inst);
2626
AgentInstaller installer = createAgentInstaller(inst, agentFile, agentArgs);
@@ -40,10 +40,6 @@ private static void addJarToLoaderSearch(File agentFile, File[] extensionFiles)
4040
}
4141
}
4242

43-
private static AgentClassLoader createAgentClassLoader(File agentFile, File[] extensionFiles) {
44-
return new AgentClassLoader(agentFile, getParentClassLoader(), extensionFiles);
45-
}
46-
4743
private static File[] getExtensionJarFiles(File jarFile) {
4844
String extensionDir = jarFile.getParent() + "/extensions/";
4945
return new File(extensionDir).listFiles(AgentInitializer::isJar);
@@ -58,8 +54,4 @@ private static AgentInstaller createAgentInstaller(Instrumentation inst, File fi
5854
Constructor<?> constructor = clazz.getDeclaredConstructor(Instrumentation.class, File.class, String.class);
5955
return (AgentInstaller) constructor.newInstance(inst, file, agentArgs);
6056
}
61-
62-
private static ClassLoader getParentClassLoader() {
63-
return AgentInitializer.class.getClassLoader();
64-
}
6557
}

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/constants/ConfigConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@ private ConfigConstants() {
3333
public static final String SHADED_LOGGER_SHOW_DATE_TIME = "shaded.org.slf4j.simpleLogger.showDateTime";
3434
public static final String SHADED_LOGGER_DATE_TIME_FORMAT = "shaded.org.slf4j.simpleLogger.dateTimeFormat";
3535
public static final String COVERAGE_PACKAGES = "arex.coverage.packages";
36+
public static final String RUNTIME_JAR_FILE_PATH = "arex.runtime.jar.file.path";
37+
public static final String APP_CLASSLOADER_NAME = "jdk.internal.loader.ClassLoaders$AppClassLoader";
3638
}

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/AdviceClassesCollector.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55

66
import java.io.File;
77
import java.io.IOException;
8+
import java.lang.reflect.Method;
9+
import java.net.URL;
10+
import java.net.URLClassLoader;
811
import java.nio.file.Files;
912
import java.util.jar.JarEntry;
13+
import java.util.jar.JarFile;
1014
import java.util.jar.JarInputStream;
15+
16+
import io.arex.agent.bootstrap.constants.ConfigConstants;
1117
import net.bytebuddy.dynamic.ClassFileLocator;
1218

1319
public class AdviceClassesCollector {
@@ -34,13 +40,14 @@ public void addJarToLoaderSearch(File file) {
3440
private void addJarToLoaderSearch(File file, boolean isExtensionJar) {
3541
try (JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(file.toPath()))) {
3642
JarEntry jarEntry;
43+
JarFile jarFile = new JarFile(file);
3744
do {
3845
jarEntry = jarInputStream.getNextJarEntry();
3946

4047
if (jarEntry != null && !jarEntry.isDirectory()) {
4148
String entryName = jarEntry.getName();
4249
if (ServiceLoader.match(entryName)) {
43-
ServiceLoader.buildCache(file, jarEntry, entryName);
50+
ServiceLoader.buildCache(jarFile, jarEntry, entryName);
4451
}
4552
// exclude package io.arex.inst.runtime/extension, not class, and shaded class.
4653
boolean isFilterEntry = StringUtil.isEmpty(entryName) ||
@@ -62,7 +69,7 @@ private void addJarToLoaderSearch(File file, boolean isExtensionJar) {
6269

6370
} while (jarEntry != null);
6471
} catch (Throwable ex) {
65-
System.err.printf("add jar classes to advice failed, file: %s%n", file.getAbsolutePath());
72+
System.err.printf("add jar classes to advice failed, file: %s%n ex: %s", file.getAbsolutePath(), ex);
6673
}
6774
}
6875

@@ -95,6 +102,66 @@ private void addClassToInjectorCache(String adviceClassName) {
95102
}
96103
}
97104

105+
private static void appendToClassLoaderSearch(ClassLoader classLoader, File jarFile) {
106+
try {
107+
Method addURL = Class.forName("java.net.URLClassLoader").getDeclaredMethod("addURL", URL.class);
108+
addURL.setAccessible(true);
109+
110+
if (classLoader instanceof URLClassLoader) {
111+
addURL.invoke(classLoader, jarFile.toURI().toURL());
112+
}
113+
114+
/*
115+
* Due to Java 8 vs java 9+ incompatibility issues
116+
* See https://stackoverflow.com/questions/46694600/java-9-compatability-issue-with-classloader-getsystemclassloader/51584718
117+
*/
118+
ClassLoader urlClassLoader = ClassLoader.getSystemClassLoader();
119+
if (!(urlClassLoader instanceof URLClassLoader)) {
120+
try (URLClassLoader tempClassLoader = new URLClassLoader(new URL[] {jarFile.toURI().toURL()}, urlClassLoader)) {
121+
addURL.invoke(tempClassLoader, jarFile.toURI().toURL());
122+
appendToAppClassLoaderSearch(classLoader, jarFile);
123+
}
124+
} else {
125+
addURL.invoke(urlClassLoader, jarFile.toURI().toURL());
126+
appendToAppClassLoaderSearch(classLoader, jarFile);
127+
}
128+
} catch (Exception e) {
129+
System.err.printf("appendToClassLoaderSearch failed, classLoader: %s, jarFile: %s%n",
130+
classLoader.getClass().getName(), jarFile.getAbsolutePath());
131+
}
132+
}
133+
134+
/**
135+
* append jar jdk.internal.loader.ClassLoaders.AppClassLoader
136+
* if java >= 11 need add jvm option:--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED
137+
* @param classLoader
138+
* @param jarFile
139+
*/
140+
private static void appendToAppClassLoaderSearch(ClassLoader classLoader, File jarFile) {
141+
try {
142+
Class<? extends ClassLoader> loaderClass = classLoader.getClass();
143+
if (JdkUtils.isJdk11() && ConfigConstants.APP_CLASSLOADER_NAME.equalsIgnoreCase(loaderClass.getName())) {
144+
Method classPathMethod = loaderClass.getDeclaredMethod("appendToClassPathForInstrumentation", String.class);
145+
classPathMethod.setAccessible(true);
146+
classPathMethod.invoke(classLoader, jarFile.getPath());
147+
}
148+
} catch (Exception e) {
149+
System.err.printf("appendToAppClassLoaderSearch failed, classLoader: %s, jarFile: %s%n",
150+
classLoader.getClass().getName(), jarFile.getAbsolutePath());
151+
}
152+
}
153+
154+
public void addJacksonJarToLoaderSearch() {
155+
String runTimePath = System.getProperty(ConfigConstants.RUNTIME_JAR_FILE_PATH);
156+
if (StringUtil.isEmpty(runTimePath)) {
157+
return;
158+
}
159+
String[] split = StringUtil.split(runTimePath, ';');
160+
for (String entry : split) {
161+
appendToClassLoaderSearch(Thread.currentThread().getContextClassLoader(), new File(entry));
162+
}
163+
}
164+
98165
private byte[] getBytes(String name, ClassLoader loader) throws IOException {
99166
ClassFileLocator locator = ClassFileLocator.ForClassLoader.of(loader);
100167
return locator.locate(name).resolve();

arex-instrumentation-foundation/src/main/java/io/arex/foundation/util/JdkUtils.java renamed to arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/JdkUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.arex.foundation.util;
1+
package io.arex.agent.bootstrap.util;
22

33
public class JdkUtils {
44
public static final int JDK_11 = 11;

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/ServiceLoader.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.arex.agent.bootstrap.util;
22

33
import java.io.BufferedReader;
4-
import java.io.File;
54
import java.io.InputStream;
65
import java.io.InputStreamReader;
76
import java.io.Reader;
@@ -48,9 +47,8 @@ public static <T> List<T> load(Class<T> service, ClassLoader loader) {
4847
* SERVICE_CACHE: key: io.arex.inst.runtime.serializer.StringSerializable
4948
* value: [io.arex.foundation.serializer.gson.GsonSerializer, io.arex.foundation.serializer.jackson.JacksonSerializer]
5049
*/
51-
public static void buildCache(File file, JarEntry jarEntry, String entryName) {
52-
try(JarFile jarFile = new JarFile(file);
53-
InputStream inputStream = jarFile.getInputStream(jarEntry)) {
50+
public static void buildCache(JarFile jarFile, JarEntry jarEntry, String entryName) {
51+
try(InputStream inputStream = jarFile.getInputStream(jarEntry)) {
5452
List<String> serviceList = readAllLines(inputStream);
5553
if (CollectionUtil.isNotEmpty(serviceList)) {
5654
String className = entryName.substring(PREFIX.length());

arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/AgentInitializerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void tearDown() {
3737
@Test
3838
void testFirstInitialize() {
3939
try (MockedConstruction<AgentClassLoader> mocked = Mockito.mockConstruction(AgentClassLoader.class, (mock, context) -> Mockito.doReturn(InstrumentationInstallerTest.class).when(mock).loadClass(any()))){
40-
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null));
40+
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null, AgentInitializerTest.class.getClassLoader()));
4141
} catch (Throwable ex) {
4242
ex.printStackTrace();
4343
}
@@ -46,8 +46,8 @@ void testFirstInitialize() {
4646
@Test
4747
void testDoubleInitialize() {
4848
try (MockedConstruction<AgentClassLoader> mocked = Mockito.mockConstruction(AgentClassLoader.class, (mock, context) -> Mockito.doReturn(InstrumentationInstallerTest.class).when(mock).loadClass(any()))){
49-
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null));
50-
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null));
49+
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null, AgentInitializerTest.class.getClassLoader()));
50+
Assertions.assertDoesNotThrow(() -> AgentInitializer.initialize(instrumentation, zipFile, null, AgentInitializerTest.class.getClassLoader()));
5151
} catch (Throwable ex) {
5252
ex.printStackTrace();
5353
}

arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/AdviceClassesCollectorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import java.lang.reflect.Method;
1010
import java.net.URL;
1111
import java.net.URLClassLoader;
12-
import java.nio.file.Path;
13-
import java.nio.file.Paths;
1412

1513
import org.junit.jupiter.api.AfterAll;
1614
import org.junit.jupiter.api.BeforeAll;
@@ -81,9 +79,11 @@ void testNull() {
8179
}
8280

8381
@Test
84-
public void test() {
85-
final File file = new File("D:\\Users\\yongwuhe\\IdeaProjects\\arex-agent-java\\arex-agent-jar\\arex-agent-0.3.6.jar");
86-
String enrtyName = "META-INF/services/com.fasterxml.jackson.core.JsonFactory";
87-
final Path path = Paths.get(file.getAbsolutePath() + "!/" + enrtyName);
82+
void addJarToLoaderSearch() {
83+
// no file
84+
assertDoesNotThrow(AdviceClassesCollector.INSTANCE::addJacksonJarToLoaderSearch);
85+
// file path
86+
System.setProperty("arex.runtime.jar.file.path", zipFile.getAbsolutePath());
87+
assertDoesNotThrow(AdviceClassesCollector.INSTANCE::addJacksonJarToLoaderSearch);
8888
}
89-
}
89+
}

arex-instrumentation-foundation/src/test/java/io/arex/foundation/util/JdkUtilsTest.java renamed to arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/JdkUtilsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
package io.arex.foundation.util;
2-
3-
import static org.junit.jupiter.api.Assertions.*;
1+
package io.arex.agent.bootstrap.util;
42

53
import org.junit.jupiter.api.AfterEach;
64
import org.junit.jupiter.api.BeforeEach;
75
import org.junit.jupiter.api.Test;
86

9-
class JdkUtilsTest {
7+
import static org.junit.jupiter.api.Assertions.*;
108

9+
class JdkUtilsTest {
1110
@BeforeEach
1211
void setUp() {
1312
}
@@ -25,4 +24,5 @@ void getJavaVersion() {
2524
void isJdk11() {
2625
assertInstanceOf(Boolean.class, JdkUtils.isJdk11());
2726
}
28-
}
27+
28+
}

arex-agent-core/src/main/java/io/arex/agent/instrumentation/BaseAgentInstaller.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
import io.arex.agent.bootstrap.util.FileUtils;
77
import io.arex.foundation.config.ConfigManager;
88
import io.arex.foundation.healthy.HealthManager;
9-
import io.arex.foundation.serializer.jackson.JacksonSerializer;
109
import io.arex.foundation.services.ConfigService;
1110
import io.arex.foundation.services.DataCollectorService;
1211
import io.arex.foundation.services.TimerService;
1312
import io.arex.foundation.util.NetUtils;
1413
import io.arex.inst.runtime.context.RecordLimiter;
15-
import io.arex.inst.runtime.serializer.Serializer;
1614
import io.arex.inst.runtime.service.DataCollector;
1715
import io.arex.inst.runtime.service.DataService;
1816

@@ -113,16 +111,8 @@ private void timedReportStatus() {
113111
private void initDependentComponents() {
114112
TraceContextManager.init(NetUtils.getIpAddress());
115113
RecordLimiter.init(HealthManager::acquire);
116-
initSerializer();
117114
initDataCollector();
118115
}
119-
120-
/**
121-
* add class to user loader search. ex: ParallelWebappClassLoader
122-
*/
123-
private void initSerializer() {
124-
Serializer.builder(JacksonSerializer.INSTANCE).build();
125-
}
126116
private void initDataCollector() {
127117
DataCollector collector = DataCollectorService.INSTANCE;
128118
if (ConfigManager.INSTANCE.isLocalStorage()) {

arex-agent/pom.xml

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,43 @@
245245
</configuration>
246246
</plugin>
247247

248+
<plugin>
249+
<groupId>org.apache.maven.plugins</groupId>
250+
<artifactId>maven-assembly-plugin</artifactId>
251+
<version>3.3.0</version>
252+
<configuration>
253+
<finalName>arex</finalName>
254+
<descriptors>
255+
<descriptor>${xml-path}</descriptor>
256+
</descriptors>
257+
<archive>
258+
<manifest>
259+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
260+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
261+
</manifest>
262+
<manifestEntries>
263+
<Premain-Class>io.arex.agent.ArexJavaAgent</Premain-Class>
264+
<Agent-Class>io.arex.agent.ArexJavaAgent</Agent-Class>
265+
<Can-Redefine-Classes>true</Can-Redefine-Classes>
266+
<Can-Retransform-Classes>true</Can-Retransform-Classes>
267+
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
268+
<Build-Time>${maven.build.timestamp}</Build-Time>
269+
<Built-By>arextest.com</Built-By>
270+
</manifestEntries>
271+
</archive>
272+
</configuration>
273+
274+
<executions>
275+
<execution>
276+
<id>make-assembly</id>
277+
<phase>package</phase>
278+
<goals>
279+
<goal>single</goal>
280+
</goals>
281+
</execution>
282+
</executions>
283+
</plugin>
284+
248285
<plugin>
249286
<groupId>org.apache.maven.plugins</groupId>
250287
<artifactId>maven-shade-plugin</artifactId>
@@ -282,7 +319,6 @@
282319
<include>net.bytebuddy:byte-buddy</include>
283320
<include>org.slf4j:slf4j-simple</include>
284321
<include>io.arex:**</include>
285-
<include>com.fasterxml.jackson.core:**</include>
286322
</includes>
287323
</artifactSet>
288324
</configuration>
@@ -304,7 +340,6 @@
304340
</delete>
305341
<copy todir="../arex-agent-jar">
306342
<fileset dir="../arex-agent/target/" includes="arex-agent*.jar" />
307-
<fileset dir="../arex-agent-bootstrap/target/" includes="arex-agent-bootstrap*.jar" />
308343
</copy>
309344
</target>
310345
</configuration>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
4+
<id>agent-${project.version}</id>
5+
<formats>
6+
<format>jar</format>
7+
</formats>
8+
<includeBaseDirectory>false</includeBaseDirectory>
9+
<fileSets>
10+
<fileSet>
11+
<directory>${project.build.directory}/classes</directory>
12+
<outputDirectory>/</outputDirectory>
13+
<includes>
14+
<include>**</include>
15+
</includes>
16+
</fileSet>
17+
</fileSets>
18+
<dependencySets>
19+
<dependencySet>
20+
<outputDirectory>/</outputDirectory>
21+
<includes>
22+
<include>com.fasterxml.jackson.core:**</include>
23+
<include>io.arex:arex-agent-bootstrap</include>
24+
</includes>
25+
<useProjectArtifact>false</useProjectArtifact>
26+
<unpack>false</unpack>
27+
<scope>runtime</scope>
28+
</dependencySet>
29+
</dependencySets>
30+
</assembly>

0 commit comments

Comments
 (0)