diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/boot/AgentPackagePath.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/boot/AgentPackagePath.java index 186c876a2ccc8d01575753cce1bd4c3e37b79012..02cf1fc08183510b45b0d0ace9e0e9c31c40edea 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/boot/AgentPackagePath.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/boot/AgentPackagePath.java @@ -22,15 +22,11 @@ import java.io.File; import java.net.MalformedURLException; import java.net.URL; import org.skywalking.apm.agent.core.logging.SystemOutWriter; -import org.skywalking.apm.logging.ILog; -import org.skywalking.apm.logging.LogManager; /** * @author wusheng */ public class AgentPackagePath { - private static final ILog logger = LogManager.getLogger(AgentPackagePath.class); - private static File AGENT_PACKAGE_PATH; public static File getPath() throws AgentPackageNotFoundException { @@ -48,23 +44,28 @@ public class AgentPackagePath { String urlString = resource.toString(); SystemOutWriter.INSTANCE.write(urlString); - logger.debug(urlString); - urlString = urlString.substring(urlString.indexOf("file:"), urlString.indexOf('!')); - File agentJarFile = null; - try { - agentJarFile = new File(new URL(urlString).getFile()); - } catch (MalformedURLException e) { - SystemOutWriter.INSTANCE.write("Can not locate agent jar file by url:" + urlString); - logger.error(e, "Can not locate agent jar file by url: {}", urlString); - } - if (agentJarFile.exists()) { - return agentJarFile.getParentFile(); + int insidePathIndex = urlString.indexOf('!'); + boolean isInJar = insidePathIndex > -1; + + if (isInJar) { + urlString = urlString.substring(urlString.indexOf("file:"), insidePathIndex); + File agentJarFile = null; + try { + agentJarFile = new File(new URL(urlString).getFile()); + } catch (MalformedURLException e) { + SystemOutWriter.INSTANCE.write("Can not locate agent jar file by url:" + urlString); + } + if (agentJarFile.exists()) { + return agentJarFile.getParentFile(); + } + } else { + String classLocation = urlString.substring(urlString.indexOf("file:"), urlString.length() - classResourcePath.length()); + return new File(classLocation); } } SystemOutWriter.INSTANCE.write("Can not locate agent jar file."); - logger.info("Can not locate agent jar file."); throw new AgentPackageNotFoundException("Can not locate agent jar file."); } diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java index 560b176ebafdf00b6adda148314e4cedd1b3f375..e94d63e4ba2e6aedb3dd565ebcd62c85bf7f5bfc 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java @@ -22,11 +22,12 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; +import java.util.Iterator; +import java.util.Map; import java.util.Properties; import org.skywalking.apm.agent.core.boot.AgentPackageNotFoundException; import org.skywalking.apm.agent.core.boot.AgentPackagePath; -import org.skywalking.apm.logging.ILog; -import org.skywalking.apm.logging.LogManager; +import org.skywalking.apm.agent.core.logging.SystemOutWriter; import org.skywalking.apm.util.ConfigInitializer; import org.skywalking.apm.util.StringUtil; @@ -37,12 +38,12 @@ import org.skywalking.apm.util.StringUtil; * @see {@link #initialize()}, to learn more about how to initialzie. */ public class SnifferConfigInitializer { - private static final ILog logger = LogManager.getLogger(SnifferConfigInitializer.class); - private static String CONFIG_FILE_NAME = "/sky-walking.config"; + private static String CONFIG_FILE_NAME = "/config/agent.config"; + private static String ENV_KEY_PREFIX = "skywalking."; /** * Try to locate config file, named {@link #CONFIG_FILE_NAME}, in following order: - * 1. Path from SystemProperty. {@link #loadConfigBySystemProperty()} + * 1. Path from SystemProperty. {@link #overrideConfigBySystemEnv()} * 2. class path. * 3. Path, where agent is. {@link #loadConfigFromAgentFolder()} *
@@ -55,60 +56,61 @@ public class SnifferConfigInitializer {
public static void initialize() throws ConfigNotFoundException, AgentPackageNotFoundException {
InputStream configFileStream;
- configFileStream = loadConfigFromAgentFolder();
-
try {
+ configFileStream = loadConfigFromAgentFolder();
Properties properties = new Properties();
properties.load(configFileStream);
ConfigInitializer.initialize(properties, Config.class);
} catch (Exception e) {
- logger.error("Failed to read the config file, sky-walking is going to run in default config.", e);
+ SystemOutWriter.INSTANCE.write("Failed to read the config file, skywalking is going to run in default config.");
+ e.printStackTrace(SystemOutWriter.INSTANCE.getStream());
}
- String applicationCode = System.getProperty("applicationCode");
- if (!StringUtil.isEmpty(applicationCode)) {
- Config.Agent.APPLICATION_CODE = applicationCode;
- }
- String servers = System.getProperty("servers");
- if (!StringUtil.isEmpty(servers)) {
- Config.Collector.SERVERS = servers;
+ try {
+ overrideConfigBySystemEnv();
+ } catch (Exception e) {
+ SystemOutWriter.INSTANCE.write("Failed to read the system env.");
+ e.printStackTrace(SystemOutWriter.INSTANCE.getStream());
}
if (StringUtil.isEmpty(Config.Agent.APPLICATION_CODE)) {
- throw new ExceptionInInitializerError("'-DapplicationCode=' is missing.");
+ throw new ExceptionInInitializerError("`agent.application_code` is missing.");
}
if (StringUtil.isEmpty(Config.Collector.SERVERS)) {
- throw new ExceptionInInitializerError("'-Dservers=' is missing.");
+ throw new ExceptionInInitializerError("`collector.servers` is missing.");
}
}
/**
- * Load the config file by the path, which is provided by system property, usually with a "-Dconfig=" arg.
+ * Override the config by system env. The env key must start with `skywalking`, the reuslt should be as same as in
+ * `agent.config`
+ *
+ * such as:
+ * Env key of `agent.application_code` shoule be `skywalking.agent.application_code`
*
* @return the config file {@link InputStream}, or null if not needEnhance.
*/
- private static InputStream loadConfigBySystemProperty() {
- String config = System.getProperty("config");
- if (StringUtil.isEmpty(config)) {
- return null;
- }
- File configFile = new File(config);
- if (configFile.exists() && configFile.isDirectory()) {
- logger.info("check {} in path {}, according system property.", CONFIG_FILE_NAME, config);
- configFile = new File(config, CONFIG_FILE_NAME);
+ private static void overrideConfigBySystemEnv() throws IllegalAccessException {
+ Properties properties = new Properties();
+ Map