From 4e3b806113bffce80f6de6971fa33d87f84cc7f3 Mon Sep 17 00:00:00 2001 From: lilai Date: Mon, 25 Nov 2024 20:46:05 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=E3=80=91support=20dynamic=20instal?= =?UTF-8?q?l=20agent=20multi-times=20with=20different=20agent=20directory?= =?UTF-8?q?=20Signed-off-by:=20lilai=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lilai --- .../java/io/sermant/core/AgentCoreEntrance.java | 4 ++++ .../java/io/sermant/core/common/CommonConstant.java | 5 +++++ .../main/java/io/sermant/core/utils/FileUtils.java | 13 ++++++++----- .../java/io/sermant/core/utils/FileUtilsTest.java | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/AgentCoreEntrance.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/AgentCoreEntrance.java index c048519ff7..e7d93de004 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/AgentCoreEntrance.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/AgentCoreEntrance.java @@ -19,6 +19,7 @@ import io.sermant.core.classloader.ClassLoaderManager; import io.sermant.core.common.AgentType; import io.sermant.core.common.BootArgsIndexer; +import io.sermant.core.common.CommonConstant; import io.sermant.core.common.LoggerFactory; import io.sermant.core.config.ConfigManager; import io.sermant.core.event.EventManager; @@ -35,6 +36,7 @@ import io.sermant.core.plugin.agent.info.EnhancementManager; import io.sermant.core.plugin.agent.template.DefaultAdviser; import io.sermant.core.service.ServiceManager; +import io.sermant.core.utils.FileUtils; import io.sermant.god.common.SermantManager; import java.lang.instrument.Instrumentation; @@ -90,6 +92,8 @@ public static void install(String artifact, Map argsMap, Instrum LoggerFactory.initDefaultLogger(artifact); adviserCache = new DefaultAdviser(); + FileUtils.setAgentPath((String) argsMap.get(CommonConstant.AGENT_PATH_KEY)); + // Initialize the classloader of framework ClassLoaderManager.init(argsMap); diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/common/CommonConstant.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/common/CommonConstant.java index e2aa2b06d0..7533d49b22 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/common/CommonConstant.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/common/CommonConstant.java @@ -127,6 +127,11 @@ public class CommonConstant { */ public static final String ARTIFACT_NAME_KEY = "artifact"; + /** + * The key of agent path + */ + public static final String AGENT_PATH_KEY = "agentPath"; + private CommonConstant() { } } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/utils/FileUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/utils/FileUtils.java index 9174a020ad..c5f6ba8bf5 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/utils/FileUtils.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/utils/FileUtils.java @@ -52,8 +52,7 @@ public class FileUtils { private static final String[] INVALID_SYMBOL = {"../", "..\\"}; - private static final String AGENT_PATH = new File(new File(FileUtils.class.getProtectionDomain().getCodeSource() - .getLocation().getPath()).getParent()).getParent(); + private static String agentPath; /** * file name of unmatched class name @@ -79,7 +78,7 @@ private FileUtils() { * @return path */ public static String getAgentPath() { - return AGENT_PATH; + return agentPath; } /** @@ -89,7 +88,7 @@ public static String getAgentPath() { * @return fixed path */ public static String validatePath(String path) { - if (!path.startsWith(AGENT_PATH)) { + if (!path.startsWith(agentPath)) { return ""; } @@ -288,7 +287,7 @@ private static String buildUnmatchedFileString() { AgentConfig config = ConfigManager.getConfig(AgentConfig.class); String preFilterPath = config.getPreFilterPath(); if (StringUtils.isEmpty(preFilterPath)) { - preFilterPath = AGENT_PATH; + preFilterPath = agentPath; } String preFilterFile = config.getPreFilterFile(); if (StringUtils.isEmpty(preFilterFile)) { @@ -296,4 +295,8 @@ private static String buildUnmatchedFileString() { } return preFilterPath + File.separatorChar + preFilterFile; } + + public static void setAgentPath(String path) { + agentPath = path; + } } diff --git a/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/utils/FileUtilsTest.java b/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/utils/FileUtilsTest.java index dfe45b03c3..fa21dcbf03 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/utils/FileUtilsTest.java +++ b/sermant-agentcore/sermant-agentcore-core/src/test/java/io/sermant/core/utils/FileUtilsTest.java @@ -29,7 +29,7 @@ public class FileUtilsTest { @Test public void testPath() { String pathValid = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath(); - Assert.assertEquals(pathValid, FileUtils.validatePath(pathValid)); + FileUtils.setAgentPath(pathValid); String pathInvalid = "/test/path"; Assert.assertEquals("", FileUtils.validatePath(pathInvalid)); String pathInvalidSymbolA = pathValid + "../" + "/test";