Poison

jvm-profiler

最近在做 Java Agent 的相关开发,在查询相关开源 Java Agent 实现时想起了 Uber 的 jvm-profiler,之前在开发 Spark 相关应用时了解过该 Agent,最近处理 Java Agent 的类隔离又想起了它,于是看了看相关实现,其中 premain 方法的入口如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Copyright (c) 2018 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.uber.profiling;

import java.lang.instrument.Instrumentation;

public final class Agent {

private static AgentImpl agentImpl = new AgentImpl();

private Agent() {
}

public static void agentmain(final String args, final Instrumentation instrumentation) {
premain(args, instrumentation);
}

public static void premain(final String args, final Instrumentation instrumentation) {
System.out.println("Java Agent " + AgentImpl.VERSION + " premain args: " + args);

Arguments arguments = Arguments.parseArgs(args);
arguments.runConfigProvider();
agentImpl.run(arguments, instrumentation, null);
}
}

其中 agentImpl.run 方法的实现见:AgentImpl.java,在入口处没有看见有创建独立的类加载器的相关逻辑,而在 pom.xml 中看到使用 maven-shade-plugin 对类进行了重定位,并未实现物理隔离。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>ujagent_shaded.org.apache</shadedPattern>
</relocation>
<relocation>
<pattern>com.fasterxml</pattern>
<shadedPattern>ujagent_shaded.com.fasterxml</shadedPattern>
</relocation>
<relocation>
<pattern>org.codehaus</pattern>
<shadedPattern>ujagent_shaded.org.codehaus</shadedPattern>
</relocation>
<relocation>
<pattern>com.thoughtworks</pattern>
<shadedPattern>ujagent_shaded.com.thoughtworks</shadedPattern>
</relocation>
<relocation>
<pattern>org.xerial</pattern>
<shadedPattern>ujagent_shaded.org.xerial</shadedPattern>
</relocation>
<relocation>
<pattern>org.tukaani</pattern>
<shadedPattern>ujagent_shaded.org.tukaani</shadedPattern>
</relocation>
<relocation>
<pattern>org.slf4j</pattern>
<shadedPattern>ujagent_shaded.org.slf4j</shadedPattern>
</relocation>
<relocation>
<pattern>com.alibaba</pattern>
<shadedPattern>ujagent_shaded.com.alibaba</shadedPattern>
</relocation>
<relocation>
<pattern>net.logstash</pattern>
<shadedPattern>ujagent_shaded.net.logstash</shadedPattern>
</relocation>
<relocation>
<pattern>com.timgroup</pattern>
<shadedPattern>ujagent_shaded.com.timgroup</shadedPattern>
</relocation>
<relocation>
<pattern>com.amazonaws</pattern>
<shadedPattern>ujagent_shaded.com.amazonaws</shadedPattern>
</relocation>
<relocation>
<pattern>org.javassist</pattern>
<shadedPattern>ujagent_shaded.org.javassist</shadedPattern>
</relocation>
<relocation>
<pattern>org.joda</pattern>
<shadedPattern>ujagent_shaded.org.joda</shadedPattern>
</relocation>
<relocation>
<pattern>avro</pattern>
<shadedPattern>ujagent_shaded.avro</shadedPattern>
</relocation>
<relocation>
<pattern>edu</pattern>
<shadedPattern>ujagent_shaded.edu</shadedPattern>
</relocation>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>ujagent_shaded.javassist</shadedPattern>
</relocation>
<relocation>
<pattern>net</pattern>
<shadedPattern>ujagent_shaded.net</shadedPattern>
</relocation>
<relocation>
<pattern>com.uber.data</pattern>
<shadedPattern>ujagent_shaded.com.uber.data</shadedPattern>
</relocation>
<relocation>
<pattern>com.uber.m3</pattern>
<shadedPattern>ujagent_shaded.com.uber.m3</shadedPattern>
</relocation>
<relocation>
<pattern>com.uber.stream</pattern>
<shadedPattern>ujagent_shaded.com.uber.stream</shadedPattern>
</relocation>
<relocation>
<pattern>com.uber.elk</pattern>
<shadedPattern>ujagent_shaded.com.uber.elk</shadedPattern>
</relocation>
<relocation>
<pattern>com.shade</pattern>
<shadedPattern>ujagent_shaded.com.shade</shadedPattern>
</relocation>
<relocation>
<pattern>org.yaml</pattern>
<shadedPattern>ujagent_org.yaml</shadedPattern>
</relocation>
<relocation>
<pattern>redis.clients</pattern>
<shadedPattern>ujagent_shaded.redis.clients</shadedPattern>
</relocation>
<relocation>
<pattern>org.influxdb</pattern>
<shadedPattern>ujagent_shaded.org.influxdb</shadedPattern>
</relocation>
<relocation>
<pattern>ch</pattern>
<shadedPattern>ujagent_shaded.ch</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>ujagent_shaded.okio</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/maven/**</exclude>
<exclude>META-INF/services/com.fasterxml.**</exclude>
<exclude>META-INF/services/java.sql.Driver</exclude>
<exclude>awssdk_config_default.json</exclude>
<exclude>heatpipe.config.properties</exclude>
<exclude>log4j.properties</exclude>
<exclude>kafka/kafka-version.properties</exclude>
<exclude>darwin/x86_64/liblz4-java.dylib</exclude>
<exclude>linux/amd64/liblz4-java.so</exclude>
<exclude>linux/i386/liblz4-java.so</exclude>
<exclude>win32/amd64/liblz4-java.so</exclude>
<exclude>org/gjt/mm/mysql/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

jvm-profiler
JVM Profiler: An Open Source Tool for Tracing Distributed JVM Applications at Scale