SnifferConfigInitializer { public static void initializeCoreConfig(String agentOptions) { AGENT_SETTINGS = new Properties(); try (final InputStreamReader configFileStream = loadConfig()) { AGENT_SETTINGS.load(configFileStream); for (String key : AGENT_SETTINGS.stringPropertyNames()) { String value = (String) AGENT_SETTINGS.get(key); AGENT_SETTINGS.put(key, PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(value, AGENT_SETTINGS)); } } catch (Exception e) { LOGGER.error(e, "Failed to read the config file, skywalking is going to run in default config."); } try { overrideConfigBySystemProp(); } catch (Exception e) { LOGGER.error(e, "Failed to read the system properties."); } agentOptions = StringUtil.trim(agentOptions, ','); if (!StringUtil.isEmpty(agentOptions)) { try { agentOptions = agentOptions.trim(); LOGGER.info("Agent options is {}.", agentOptions); overrideConfigByAgentOptions(agentOptions); } catch (Exception e) { LOGGER.error(e, "Failed to parse the agent options, val is {}.", agentOptions); } } initializeConfig(Config.class); configureLogger(); LOGGER = LogManager.getLogger(SnifferConfigInitializer.class); if (StringUtil.isEmpty(Config.Agent.SERVICE_NAME)) { throw new ExceptionInInitializerError("`agent.service_name` is missing."); } if (StringUtil.isEmpty(Config.Collector.BACKEND_SERVICE)) { throw new ExceptionInInitializerError("`collector.backend_service` is missing."); } if (Config.Plugin.PEER_MAX_LENGTH <= 3) { LOGGER.warn( "PEER_MAX_LENGTH configuration:{} error, the default value of 200 will be used.", Config.Plugin.PEER_MAX_LENGTH ); Config.Plugin.PEER_MAX_LENGTH = 200; } IS_INIT_COMPLETED = true; }  static void initializeCoreConfig(String agentOptions); static void initializeConfig(Class configClass); static boolean isInitCompleted();  }
@Test public void testLoadConfigFromJavaAgentDir() throws AgentPackageNotFoundException, ConfigNotFoundException { System.setProperty("skywalking.agent.service_name", "testApp"); System.setProperty("skywalking.collector.backend_service", "127.0.0.1:8090"); System.setProperty("skywalking.logging.level", "info"); SnifferConfigInitializer.initializeCoreConfig(null); assertThat(Config.Agent.SERVICE_NAME, is("testApp")); assertThat(Config.Collector.BACKEND_SERVICE, is("127.0.0.1:8090")); assertThat(Config.Logging.LEVEL, is(LogLevel.INFO)); }
@Test public void testLoadConfigFromAgentOptions() throws AgentPackageNotFoundException, ConfigNotFoundException { String agentOptions = "agent.service_name=testApp,collector.backend_service=127.0.0.1:8090,logging.level=info"; SnifferConfigInitializer.initializeCoreConfig(agentOptions); assertThat(Config.Agent.SERVICE_NAME, is("testApp")); assertThat(Config.Collector.BACKEND_SERVICE, is("127.0.0.1:8090")); assertThat(Config.Logging.LEVEL, is(LogLevel.INFO)); }
@Test public void testConfigOverriding() throws AgentPackageNotFoundException, ConfigNotFoundException { System.setProperty("skywalking.agent.service_name", "testAppFromSystem"); System.setProperty("skywalking.agent.instance_properties[key1]", "value1"); System.setProperty("skywalking.agent.instance_properties[key2]", "value2"); System.setProperty("skywalking.collector.backend_service", "127.0.0.1:8090"); String agentOptions = "agent.service_name=testAppFromAgentOptions,logging.level=debug"; SnifferConfigInitializer.initializeCoreConfig(agentOptions); assertThat(Config.Agent.SERVICE_NAME, is("testAppFromAgentOptions")); assertThat(Config.Collector.BACKEND_SERVICE, is("127.0.0.1:8090")); assertThat(Config.Logging.LEVEL, is(LogLevel.DEBUG)); assertThat(Config.Agent.INSTANCE_PROPERTIES.get("key1"), is("value1")); assertThat(Config.Agent.INSTANCE_PROPERTIES.get("key2"), is("value2")); }
@Test public void testAgentOptionsSeparator() throws AgentPackageNotFoundException, ConfigNotFoundException { System.setProperty("skywalking.agent.service_name", "testApp"); System.setProperty("skywalking.collector.backend_service", "127.0.0.1:8090"); String agentOptions = "agent.ignore_suffix='.jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg'"; SnifferConfigInitializer.initializeCoreConfig(agentOptions); assertThat(Config.Agent.IGNORE_SUFFIX, is(".jpg,.jpeg,.js,.css,.png,.bmp,.gif,.ico,.mp3,.mp4,.html,.svg")); }
@Test public void testAgentOptionsParser() throws AgentPackageNotFoundException, ConfigNotFoundException { System.setProperty("skywalking.collector.backend_service", "127.0.0.1:8090"); String agentOptions = "agent.service_name=test=abc"; try { SnifferConfigInitializer.initializeCoreConfig(agentOptions); fail("test=abc without quotes is not a valid value"); } catch (ExceptionInInitializerError e) { } agentOptions = "agent.service_name='test=abc'"; SnifferConfigInitializer.initializeCoreConfig(agentOptions); assertThat(Config.Agent.SERVICE_NAME, is("test=abc")); }