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 envs = System.getenv(); + for (String envKey : envs.keySet()) { + if (envKey.startsWith(ENV_KEY_PREFIX)) { + String realKey = envKey.substring(ENV_KEY_PREFIX.length()); + properties.setProperty(realKey, envs.get(envKey)); + } } - - if (configFile.exists() && configFile.isFile()) { - try { - logger.info("found {}, according system property.", configFile.getAbsolutePath()); - return new FileInputStream(configFile); - } catch (FileNotFoundException e) { - logger.error(e, "Fail to load {} , according system property.", config); + Properties systemProperties = System.getProperties(); + Iterator> entryIterator = systemProperties.entrySet().iterator(); + while (entryIterator.hasNext()) { + Map.Entry prop = entryIterator.next(); + if (prop.getKey().toString().startsWith(ENV_KEY_PREFIX)) { + String realKey = prop.getKey().toString().substring(ENV_KEY_PREFIX.length()); + properties.put(realKey, prop.getValue()); } } - - logger.info("No {} found, according system property.", config); - return null; + if (!properties.isEmpty()) { + ConfigInitializer.initialize(properties, Config.class); + } } /** @@ -120,7 +122,8 @@ public class SnifferConfigInitializer { File configFile = new File(AgentPackagePath.getPath(), CONFIG_FILE_NAME); if (configFile.exists() && configFile.isFile()) { try { - logger.info("{} file found in agent folder.", CONFIG_FILE_NAME); + SystemOutWriter.INSTANCE.write(CONFIG_FILE_NAME + " file found in agent folder."); + return new FileInputStream(configFile); } catch (FileNotFoundException e) { throw new ConfigNotFoundException("Fail to load agent.config", e); diff --git a/apm-sniffer/apm-agent-core/src/test/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java b/apm-sniffer/apm-agent-core/src/test/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java index 7ea20d53f9d29f4fef9d939436e1f6f532a72c5c..58c84cc8d22e10effc4c6d5a440aea7ba8d03d5f 100644 --- a/apm-sniffer/apm-agent-core/src/test/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java +++ b/apm-sniffer/apm-agent-core/src/test/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java @@ -20,6 +20,7 @@ package org.skywalking.apm.agent.core.conf; import org.junit.AfterClass; import org.junit.Test; +import org.skywalking.apm.agent.core.boot.AgentPackageNotFoundException; import org.skywalking.apm.agent.core.logging.LogLevel; import static org.hamcrest.CoreMatchers.is; @@ -28,9 +29,10 @@ import static org.hamcrest.MatcherAssert.assertThat; public class SnifferConfigInitializerTest { @Test - public void testLoadConfigFromJavaAgentDir() { - System.setProperty("applicationCode", "testApp"); - System.setProperty("servers", "127.0.0.1:8090"); + public void testLoadConfigFromJavaAgentDir() throws AgentPackageNotFoundException, ConfigNotFoundException { + System.setProperty("skywalking.agent.application_code", "testApp"); + System.setProperty("skywalking.collector.servers", "127.0.0.1:8090"); + System.setProperty("skywalking.logging.level", "info"); SnifferConfigInitializer.initialize(); assertThat(Config.Agent.APPLICATION_CODE, is("testApp")); assertThat(Config.Collector.SERVERS, is("127.0.0.1:8090")); diff --git a/apm-sniffer/apm-agent-core/src/test/resources/sky-walking.config b/apm-sniffer/apm-agent-core/src/test/resources/config/agent.config similarity index 58% rename from apm-sniffer/apm-agent-core/src/test/resources/sky-walking.config rename to apm-sniffer/apm-agent-core/src/test/resources/config/agent.config index f7986fe99bd1f6e657aeeb528e7cce388e336624..5e9506e027f730e147a97a5a3f8ef0a92542a036 100644 --- a/apm-sniffer/apm-agent-core/src/test/resources/sky-walking.config +++ b/apm-sniffer/apm-agent-core/src/test/resources/config/agent.config @@ -1,3 +1,3 @@ agent.application_code = crmApp -collector.servers = 127.0.0.1: 8080 +collector.servers = 127.0.0.1:8080 logging.level=info