From efad1165f5b60acbedb9a50fb15641dc8ae5f80a Mon Sep 17 00:00:00 2001 From: lixin40 Date: Fri, 30 Nov 2018 12:12:05 +0800 Subject: [PATCH 1/2] add system env for trace ignore plugin --- .../apm-trace-ignore-plugin.config | 2 +- .../ignore/conf/IgnoreConfigInitializer.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config b/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config index acb31c74db..1af1189f95 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config @@ -20,4 +20,4 @@ # /path/* Match any number of characters # /path/** Match any number of characters and support multilevel directories # Multiple path comma separation, like trace.ignore_path=/eureka/**,/consul/** -#trace.ignore_path=/eureka/** +#trace.ignore_path=${SW_TRACE_IGNORE_PATH:/eureka/**} \ No newline at end of file diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java index af8f599eaf..b208c67184 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java @@ -24,6 +24,9 @@ import org.apache.skywalking.apm.agent.core.conf.ConfigNotFoundException; import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.util.ConfigInitializer; +import org.apache.skywalking.apm.util.PlaceholderConfigurerSupport; +import org.apache.skywalking.apm.util.PropertyPlaceholderHelper; +import org.apache.skywalking.apm.util.StringUtil; import java.io.File; import java.io.FileInputStream; @@ -57,6 +60,18 @@ public class IgnoreConfigInitializer { configFileStream = loadConfigFromAgentFolder(); Properties properties = new Properties(); properties.load(configFileStream); + PropertyPlaceholderHelper helper = + new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, + PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, + PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); + for (String key : properties.stringPropertyNames()) { + String value = (String)properties.get(key); + //replace the key's value. properties.replace(key,value) in jdk8+ + value = helper.replacePlaceholders(value, properties); + if (!StringUtil.isEmpty(value)) { + properties.put(key, helper.replacePlaceholders(value, properties)); + } + } ConfigInitializer.initialize(properties, IgnoreConfig.class); } catch (Exception e) { LOGGER.error(e, "Failed to read the config file, skywalking is going to run in default config."); From 5bc3ccde5e1e27a32d1c622fa06ceac3eaeac983 Mon Sep 17 00:00:00 2001 From: "Jared.Tan" Date: Thu, 6 Dec 2018 15:22:33 +0800 Subject: [PATCH 2/2] fix conflicts and test case. --- apm-sniffer/apm-test-tools/pom.xml | 9 +++- .../apm-trace-ignore-plugin.config | 2 +- .../trace-ignore-plugin/pom.xml | 16 +++++++- .../ignore/conf/IgnoreConfigInitializer.java | 41 +++++-------------- .../plugin/trace/ignore/TraceIgnoreTest.java | 26 ++++++++---- 5 files changed, 53 insertions(+), 41 deletions(-) diff --git a/apm-sniffer/apm-test-tools/pom.xml b/apm-sniffer/apm-test-tools/pom.xml index 25eaa54be7..61e07946cf 100644 --- a/apm-sniffer/apm-test-tools/pom.xml +++ b/apm-sniffer/apm-test-tools/pom.xml @@ -16,7 +16,8 @@ ~ --> - + 4.0.0 @@ -30,11 +31,15 @@ apm-test-tools http://maven.apache.org + + + 4.12 + junit junit - 4.12 + ${junit.version} provided diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config b/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config index 1af1189f95..d2393b2e66 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/apm-trace-ignore-plugin.config @@ -20,4 +20,4 @@ # /path/* Match any number of characters # /path/** Match any number of characters and support multilevel directories # Multiple path comma separation, like trace.ignore_path=/eureka/**,/consul/** -#trace.ignore_path=${SW_TRACE_IGNORE_PATH:/eureka/**} \ No newline at end of file +#trace.ignore_path=${SW_AGENT_TRACE_IGNORE_PATH:/eureka/**} diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/pom.xml b/apm-sniffer/optional-plugins/trace-ignore-plugin/pom.xml index 40ceb0f200..a97769dbd2 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/pom.xml +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/pom.xml @@ -16,7 +16,8 @@ ~ --> - + optional-plugins org.apache.skywalking @@ -29,4 +30,17 @@ apm-trace-ignore-plugin http://maven.apache.org + + + 1.18.0 + + + + + com.github.stefanbirkner + system-rules + ${ststem-rules.version} + test + + diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java index b208c67184..3263dff977 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/main/java/org/apache/skywalking/apm/plugin/trace/ignore/conf/IgnoreConfigInitializer.java @@ -18,28 +18,15 @@ package org.apache.skywalking.apm.plugin.trace.ignore.conf; -import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException; -import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath; +import java.io.*; +import java.util.*; +import org.apache.skywalking.apm.agent.core.boot.*; import org.apache.skywalking.apm.agent.core.conf.ConfigNotFoundException; -import org.apache.skywalking.apm.agent.core.logging.api.ILog; -import org.apache.skywalking.apm.agent.core.logging.api.LogManager; -import org.apache.skywalking.apm.util.ConfigInitializer; -import org.apache.skywalking.apm.util.PlaceholderConfigurerSupport; -import org.apache.skywalking.apm.util.PropertyPlaceholderHelper; -import org.apache.skywalking.apm.util.StringUtil; - -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.apache.skywalking.apm.agent.core.logging.api.*; +import org.apache.skywalking.apm.util.*; /** - * * @author liujc [liujunc1993@163.com] - * */ public class IgnoreConfigInitializer { private static final ILog LOGGER = LogManager.getLogger(IgnoreConfigInitializer.class); @@ -47,7 +34,8 @@ public class IgnoreConfigInitializer { private static String ENV_KEY_PREFIX = "skywalking."; /** - * Try to locate `apm-trace-ignore-plugin.config`, which should be in the /optional-plugins/apm-trace-ignore-plugin/ dictionary of agent package. + * Try to locate `apm-trace-ignore-plugin.config`, which should be in the /optional-plugins/apm-trace-ignore-plugin/ + * dictionary of agent package. *

* Also try to override the config by system.env and system.properties. All the keys in these two places should * start with {@link #ENV_KEY_PREFIX}. e.g. in env `skywalking.trace.ignore_path=your_path` to override @@ -60,17 +48,11 @@ public class IgnoreConfigInitializer { configFileStream = loadConfigFromAgentFolder(); Properties properties = new Properties(); properties.load(configFileStream); - PropertyPlaceholderHelper helper = - new PropertyPlaceholderHelper(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, - PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, - PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, true); + PropertyPlaceholderHelper helper = PropertyPlaceholderHelper.INSTANCE; for (String key : properties.stringPropertyNames()) { String value = (String)properties.get(key); //replace the key's value. properties.replace(key,value) in jdk8+ - value = helper.replacePlaceholders(value, properties); - if (!StringUtil.isEmpty(value)) { - properties.put(key, helper.replacePlaceholders(value, properties)); - } + properties.put(key, PropertyPlaceholderHelper.INSTANCE.replacePlaceholders(value, properties)); } ConfigInitializer.initialize(properties, IgnoreConfig.class); } catch (Exception e) { @@ -78,13 +60,13 @@ public class IgnoreConfigInitializer { } try { - overrideConfigBySystemEnv(); + overrideConfigBySystemProp(); } catch (Exception e) { LOGGER.error(e, "Failed to read the system env."); } } - private static void overrideConfigBySystemEnv() throws IllegalAccessException { + private static void overrideConfigBySystemProp() throws IllegalAccessException { Properties properties = new Properties(); Properties systemProperties = System.getProperties(); Iterator> entryIterator = systemProperties.entrySet().iterator(); @@ -101,7 +83,6 @@ public class IgnoreConfigInitializer { } } - /** * Load the config file, where the agent jar is. * diff --git a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreTest.java b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreTest.java index 3c4444c2b7..9ca70ed65c 100644 --- a/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreTest.java +++ b/apm-sniffer/optional-plugins/trace-ignore-plugin/src/test/java/org/apache/skywalking/apm/plugin/trace/ignore/TraceIgnoreTest.java @@ -18,22 +18,26 @@ package org.apache.skywalking.apm.plugin.trace.ignore; +import java.util.Properties; import org.apache.skywalking.apm.agent.core.boot.ServiceManager; -import org.apache.skywalking.apm.agent.core.context.AbstractTracerContext; -import org.apache.skywalking.apm.agent.core.context.ContextManagerExtendService; -import org.apache.skywalking.apm.agent.core.context.IgnoredTracerContext; -import org.apache.skywalking.apm.agent.core.context.TracingContext; +import org.apache.skywalking.apm.agent.core.context.*; import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; import org.apache.skywalking.apm.plugin.trace.ignore.conf.IgnoreConfig; -import org.junit.Assert; -import org.junit.Rule; -import org.junit.Test; +import org.apache.skywalking.apm.util.*; +import org.junit.*; +import org.junit.contrib.java.lang.system.EnvironmentVariables; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; /** * @author liujc [liujunc1993@163.com] */ public class TraceIgnoreTest { + @Rule + public final EnvironmentVariables environmentVariables = new EnvironmentVariables().set("SW_AGENT_TRACE_IGNORE_PATH", "path_test"); + @Rule public AgentServiceRule serviceRule = new AgentServiceRule(); @@ -54,4 +58,12 @@ public class TraceIgnoreTest { Assert.assertEquals(TracingContext.class, traceContext.getClass()); } + @Test + public void testTraceIgnoreConfigOverridingFromSystemEnv() throws IllegalAccessException { + Properties properties = new Properties(); + properties.put("trace.ignore_path", "${SW_AGENT_TRACE_IGNORE_PATH:/path/eureka/**}"); + properties.put("trace.ignore_path", PropertyPlaceholderHelper.INSTANCE.replacePlaceholders((String)properties.get("trace.ignore_path"), properties)); + ConfigInitializer.initialize(properties, IgnoreConfig.class); + assertThat(IgnoreConfig.Trace.IGNORE_PATH, is("path_test")); + } }