Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support dynamic install agent multi-times with different agent directory #1683

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.sermant.core.operation.converter.api.YamlConverter;
import io.sermant.core.service.dynamicconfig.common.DynamicConfigEvent;
import io.sermant.core.service.dynamicconfig.common.DynamicConfigEventType;
import io.sermant.core.utils.FileUtils;
import io.sermant.core.utils.JarFileUtils;
import io.sermant.core.utils.StringUtils;
import io.sermant.implement.operation.converter.YamlConverterImpl;
Expand Down Expand Up @@ -84,6 +85,8 @@ public void setUp() throws Exception {
.thenReturn(YAML_CONVERTER);
commandProcessorMockedStatic.verify(() -> CommandProcessor.process(any()), times(0));
jarFileUtilsMockedStatic.when(() -> JarFileUtils.getManifestAttr(any(), anyString())).thenReturn("1.0.0");
String pathValid = FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
FileUtils.setAgentPath(pathValid);
Map<String, Object> argsMap = new HashMap<>();
argsMap.put(CommonConstant.CORE_IMPLEMENT_DIR_KEY, StringUtils.EMPTY);
argsMap.put(CommonConstant.CORE_CONFIG_FILE_KEY, StringUtils.EMPTY);
Expand Down
Loading