Skip to content

Commit 9d03d56

Browse files
authored
Put Agent-Version property reading in premain stage to avoid deadlock when use jarsigner. (#437)
1 parent 3ff8c23 commit 9d03d56

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Release Notes.
2323
* Add support for KafkaClients 3.x.
2424
* Support to customize the collect period of JVM relative metrics.
2525
* Upgrade netty-codec-http2 to 4.1.86.Final.
26-
* Move `Agent-Version` property reading away from the class loading stage to avoid deadlock when use `jarsigner`.
26+
* Put `Agent-Version` property reading in the premain stage to avoid deadlock when using `jarsigner`.
2727

2828
#### Documentation
2929

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ public static class Agent {
178178
* Private key file. If ssl_cert_chain and ssl_key exist, will enable mTLS for gRPC channel.
179179
*/
180180
public static String SSL_KEY_PATH;
181+
182+
/**
183+
* Agent version. This is set by the agent kernel through reading MANIFEST.MF file in the skywalking-agent.jar.
184+
*/
185+
public static String VERSION = "UNKNOWN";
181186
}
182187

183188
public static class OsInfo {

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
import java.io.FileNotFoundException;
2424
import java.io.InputStream;
2525
import java.io.InputStreamReader;
26+
import java.net.URL;
2627
import java.nio.charset.StandardCharsets;
2728
import java.util.ArrayList;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Properties;
32+
import java.util.Enumeration;
33+
import java.util.jar.Attributes;
34+
import java.util.jar.JarFile;
35+
import java.util.jar.Manifest;
3136
import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
3237
import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
3338
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
@@ -98,6 +103,8 @@ public static void initializeCoreConfig(String agentOptions) {
98103
configureLogger();
99104
LOGGER = LogManager.getLogger(SnifferConfigInitializer.class);
100105

106+
setAgentVersion();
107+
101108
if (StringUtil.isEmpty(Config.Agent.SERVICE_NAME)) {
102109
throw new ExceptionInInitializerError("`agent.service_name` is missing.");
103110
} else {
@@ -202,6 +209,34 @@ private static void overrideConfigBySystemProp() {
202209
}
203210
}
204211

212+
/**
213+
* Set agent version(Described in MANIFEST.MF)
214+
*/
215+
private static void setAgentVersion() {
216+
try {
217+
Enumeration<URL> resources = SnifferConfigInitializer.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
218+
while (resources.hasMoreElements()) {
219+
URL url = resources.nextElement();
220+
LOGGER.info("SnifferConfigInitializer url:{}", url.toString());
221+
try (InputStream is = url.openStream()) {
222+
if (is != null) {
223+
Manifest manifest = new Manifest(is);
224+
Attributes mainAttribs = manifest.getMainAttributes();
225+
String projectName = mainAttribs.getValue("Implementation-Vendor-Id");
226+
if (projectName != null) {
227+
if ("org.apache.skywalking".equals(projectName)) {
228+
Config.Agent.VERSION = mainAttribs.getValue("Implementation-Version");
229+
break;
230+
}
231+
}
232+
}
233+
}
234+
}
235+
} catch (Exception e) {
236+
LOGGER.warn("Can't read version from MANIFEST.MF in the agent jar");
237+
}
238+
}
239+
205240
/**
206241
* Load the specified config file or default config file
207242
*

apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,14 @@
2626
import io.grpc.ForwardingClientCall;
2727
import io.grpc.Metadata;
2828
import io.grpc.MethodDescriptor;
29-
import java.io.InputStream;
30-
import java.net.URL;
31-
import java.util.Enumeration;
32-
import java.util.jar.Attributes;
33-
import java.util.jar.JarFile;
34-
import java.util.jar.Manifest;
35-
import org.apache.skywalking.apm.agent.core.logging.api.ILog;
36-
import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
29+
import org.apache.skywalking.apm.agent.core.conf.Config;
30+
3731

3832
/**
3933
* Add agent version(Described in MANIFEST.MF) to the connection establish stage.
4034
*/
4135
public class AgentIDDecorator implements ChannelDecorator {
42-
private static final ILog LOGGER = LogManager.getLogger(AgentIDDecorator.class);
43-
private static Metadata.Key<String> AGENT_VERSION_HEAD_HEADER_NAME;
44-
private String version = "UNKNOWN";
45-
46-
public AgentIDDecorator() {
47-
try {
48-
Enumeration<URL> resources = AgentIDDecorator.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
49-
while (resources.hasMoreElements()) {
50-
URL url = resources.nextElement();
51-
try (InputStream is = url.openStream()) {
52-
if (is != null) {
53-
Manifest manifest = new Manifest(is);
54-
Attributes mainAttribs = manifest.getMainAttributes();
55-
String projectName = mainAttribs.getValue("Implementation-Vendor-Id");
56-
if (projectName != null) {
57-
if ("org.apache.skywalking".equals(projectName)) {
58-
version = mainAttribs.getValue("Implementation-Version");
59-
}
60-
}
61-
}
62-
}
63-
}
64-
AGENT_VERSION_HEAD_HEADER_NAME = Metadata.Key.of("Agent-Version", Metadata.ASCII_STRING_MARSHALLER);
65-
} catch (Exception e) {
66-
LOGGER.warn("Can't read version from MANIFEST.MF in the agent jar");
67-
}
68-
}
36+
private static final Metadata.Key<String> AGENT_VERSION_HEAD_HEADER_NAME = Metadata.Key.of("Agent-Version", Metadata.ASCII_STRING_MARSHALLER);
6937

7038
@Override
7139
public Channel build(Channel channel) {
@@ -76,7 +44,7 @@ public <REQ, RESP> ClientCall<REQ, RESP> interceptCall(MethodDescriptor<REQ, RES
7644
return new ForwardingClientCall.SimpleForwardingClientCall<REQ, RESP>(channel.newCall(method, options)) {
7745
@Override
7846
public void start(Listener<RESP> responseListener, Metadata headers) {
79-
headers.put(AGENT_VERSION_HEAD_HEADER_NAME, version);
47+
headers.put(AGENT_VERSION_HEAD_HEADER_NAME, Config.Agent.VERSION);
8048

8149
super.start(responseListener, headers);
8250
}

0 commit comments

Comments
 (0)