You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/01/06 11:08:48 UTC

[skywalking-java] branch main updated: Put `Agent-Version` property reading in premain stage to avoid deadlock when use `jarsigner`. (#437)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 9d03d56778 Put `Agent-Version` property reading in premain stage to avoid deadlock when use `jarsigner`. (#437)
9d03d56778 is described below

commit 9d03d5677877f169b60b860ad3b5184f33cace26
Author: codePig <11...@qq.com>
AuthorDate: Fri Jan 6 19:08:41 2023 +0800

    Put `Agent-Version` property reading in premain stage to avoid deadlock when use `jarsigner`. (#437)
---
 CHANGES.md                                         |  2 +-
 .../skywalking/apm/agent/core/conf/Config.java     |  5 +++
 .../agent/core/conf/SnifferConfigInitializer.java  | 35 +++++++++++++++++++
 .../apm/agent/core/remote/AgentIDDecorator.java    | 40 +++-------------------
 4 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index e8e029ff0a..cc4da49a23 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -23,7 +23,7 @@ Release Notes.
 * Add support for KafkaClients 3.x.
 * Support to customize the collect period of JVM relative metrics.
 * Upgrade netty-codec-http2 to 4.1.86.Final.
-* Move `Agent-Version` property reading away from the class loading stage to avoid deadlock when use `jarsigner`.
+* Put `Agent-Version` property reading in the premain stage to avoid deadlock when using `jarsigner`.
 
 #### Documentation
 
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
index 6b87eb10a0..ff075e39b8 100755
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/Config.java
@@ -178,6 +178,11 @@ public class Config {
          * Private key file. If ssl_cert_chain and ssl_key exist, will enable mTLS for gRPC channel.
          */
         public static String SSL_KEY_PATH;
+
+        /**
+         * Agent version. This is set by the agent kernel through reading MANIFEST.MF file in the skywalking-agent.jar.
+         */
+        public static String VERSION = "UNKNOWN";
     }
 
     public static class OsInfo {
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java
index a8d97986e0..c48b58a11a 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java
@@ -23,11 +23,16 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.Enumeration;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
 import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException;
 import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath;
 import org.apache.skywalking.apm.agent.core.logging.api.ILog;
@@ -98,6 +103,8 @@ public class SnifferConfigInitializer {
         configureLogger();
         LOGGER = LogManager.getLogger(SnifferConfigInitializer.class);
 
+        setAgentVersion();
+
         if (StringUtil.isEmpty(Config.Agent.SERVICE_NAME)) {
             throw new ExceptionInInitializerError("`agent.service_name` is missing.");
         } else {
@@ -202,6 +209,34 @@ public class SnifferConfigInitializer {
         }
     }
 
+    /**
+     * Set agent version(Described in MANIFEST.MF)
+     */
+    private static void setAgentVersion() {
+        try {
+            Enumeration<URL> resources = SnifferConfigInitializer.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
+            while (resources.hasMoreElements()) {
+                URL url = resources.nextElement();
+                LOGGER.info("SnifferConfigInitializer url:{}", url.toString());
+                try (InputStream is = url.openStream()) {
+                    if (is != null) {
+                        Manifest manifest = new Manifest(is);
+                        Attributes mainAttribs = manifest.getMainAttributes();
+                        String projectName = mainAttribs.getValue("Implementation-Vendor-Id");
+                        if (projectName != null) {
+                            if ("org.apache.skywalking".equals(projectName)) {
+                                Config.Agent.VERSION = mainAttribs.getValue("Implementation-Version");
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            LOGGER.warn("Can't read version from MANIFEST.MF in the agent jar");
+        }
+    }
+
     /**
      * Load the specified config file or default config file
      *
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java
index 4149597512..c59a68966f 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/remote/AgentIDDecorator.java
@@ -26,46 +26,14 @@ import io.grpc.ClientInterceptors;
 import io.grpc.ForwardingClientCall;
 import io.grpc.Metadata;
 import io.grpc.MethodDescriptor;
-import java.io.InputStream;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.jar.Attributes;
-import java.util.jar.JarFile;
-import java.util.jar.Manifest;
-import org.apache.skywalking.apm.agent.core.logging.api.ILog;
-import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+
 
 /**
  * Add agent version(Described in MANIFEST.MF) to the connection establish stage.
  */
 public class AgentIDDecorator implements ChannelDecorator {
-    private static final ILog LOGGER = LogManager.getLogger(AgentIDDecorator.class);
-    private static Metadata.Key<String> AGENT_VERSION_HEAD_HEADER_NAME;
-    private String version = "UNKNOWN";
-
-    public AgentIDDecorator() {
-        try {
-            Enumeration<URL> resources = AgentIDDecorator.class.getClassLoader().getResources(JarFile.MANIFEST_NAME);
-            while (resources.hasMoreElements()) {
-                URL url = resources.nextElement();
-                try (InputStream is = url.openStream()) {
-                    if (is != null) {
-                        Manifest manifest = new Manifest(is);
-                        Attributes mainAttribs = manifest.getMainAttributes();
-                        String projectName = mainAttribs.getValue("Implementation-Vendor-Id");
-                        if (projectName != null) {
-                            if ("org.apache.skywalking".equals(projectName)) {
-                                version = mainAttribs.getValue("Implementation-Version");
-                            }
-                        }
-                    }
-                }
-            }
-            AGENT_VERSION_HEAD_HEADER_NAME = Metadata.Key.of("Agent-Version", Metadata.ASCII_STRING_MARSHALLER);
-        } catch (Exception e) {
-            LOGGER.warn("Can't read version from MANIFEST.MF in the agent jar");
-        }
-    }
+    private static final Metadata.Key<String> AGENT_VERSION_HEAD_HEADER_NAME = Metadata.Key.of("Agent-Version", Metadata.ASCII_STRING_MARSHALLER);
 
     @Override
     public Channel build(Channel channel) {
@@ -76,7 +44,7 @@ public class AgentIDDecorator implements ChannelDecorator {
                 return new ForwardingClientCall.SimpleForwardingClientCall<REQ, RESP>(channel.newCall(method, options)) {
                     @Override
                     public void start(Listener<RESP> responseListener, Metadata headers) {
-                        headers.put(AGENT_VERSION_HEAD_HEADER_NAME, version);
+                        headers.put(AGENT_VERSION_HEAD_HEADER_NAME, Config.Agent.VERSION);
 
                         super.start(responseListener, headers);
                     }