Skip to content

Commit

Permalink
【fix】support dynamic install agent multi-times with different agent d…
Browse files Browse the repository at this point in the history
…irectory

Signed-off-by: lilai <[email protected]>

Signed-off-by: lilai <[email protected]>
  • Loading branch information
lilai23 committed Nov 26, 2024
1 parent 6dc2fcc commit 4e3b806
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,6 +92,8 @@ public static void install(String artifact, Map<String, Object> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,7 +78,7 @@ private FileUtils() {
* @return path
*/
public static String getAgentPath() {
return AGENT_PATH;
return agentPath;
}

/**
Expand All @@ -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 "";
}

Expand Down Expand Up @@ -288,12 +287,16 @@ 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)) {
preFilterFile = DEFAULT_OUTPUT_CLASS_NAME_FILE;
}
return preFilterPath + File.separatorChar + preFilterFile;
}

public static void setAgentPath(String path) {
agentPath = path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 4e3b806

Please sign in to comment.