forked from apache/skywalking
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#1981 from a198720/len
add system env for trace ignore plugin
- Loading branch information
Showing
5 changed files
with
57 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,33 +18,24 @@ | |
|
||
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 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 [[email protected]] | ||
* | ||
*/ | ||
public class IgnoreConfigInitializer { | ||
private static final ILog LOGGER = LogManager.getLogger(IgnoreConfigInitializer.class); | ||
private static String CONFIG_FILE_NAME = "/config/apm-trace-ignore-plugin.config"; | ||
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. | ||
* <p> | ||
* 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 | ||
|
@@ -57,19 +48,25 @@ public static void initialize() throws ConfigNotFoundException, AgentPackageNotF | |
configFileStream = loadConfigFromAgentFolder(); | ||
Properties properties = new Properties(); | ||
properties.load(configFileStream); | ||
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+ | ||
properties.put(key, PropertyPlaceholderHelper.INSTANCE.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."); | ||
} | ||
|
||
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<Map.Entry<Object, Object>> entryIterator = systemProperties.entrySet().iterator(); | ||
|
@@ -86,7 +83,6 @@ private static void overrideConfigBySystemEnv() throws IllegalAccessException { | |
} | ||
} | ||
|
||
|
||
/** | ||
* Load the config file, where the agent jar is. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [[email protected]] | ||
*/ | ||
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 void testTraceIgnore() { | |
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")); | ||
} | ||
} |