提交 fd6bcb08 编写于 作者: wu-sheng's avatar wu-sheng 提交者: GitHub

Merge pull request #245 from ascrutae/zhangxin/feature/force-enable-plugin

Add mechanism to set default status(on/off) of plugin
......@@ -101,6 +101,12 @@ public class Config {
*/
public static List DISABLED_PLUGINS = new LinkedList();
/**
* Name of force enable plugin, The value spilt by <code>,</code>
* if you have multiple plugins need to enable.
*/
public static List FORCE_ENABLE_PLUGINS = new LinkedList();
public static class MongoDB {
/**
* If true, trace all the parameters, default is false.
......
......@@ -5,6 +5,7 @@ import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineExcepti
import org.skywalking.apm.util.StringUtil;
public class PluginDefine {
public static final String PLUGIN_OFF_PREFIX = "[OFF]";
/**
* Plugin name.
*/
......@@ -15,9 +16,15 @@ public class PluginDefine {
*/
private String defineClass;
private PluginDefine(String name, String defineClass) {
/**
* The sate of plugin.
*/
private State state;
private PluginDefine(String name, String defineClass, State state) {
this.name = name;
this.defineClass = defineClass;
this.state = state;
}
public static PluginDefine build(String define) throws IllegalPluginDefineException {
......@@ -30,16 +37,34 @@ public class PluginDefine {
throw new IllegalPluginDefineException(define);
}
return new PluginDefine(pluginDefine[0], pluginDefine[1]);
String pluginName = pluginDefine[0];
String defineClass = pluginDefine[1];
if (pluginName.toUpperCase().startsWith(PLUGIN_OFF_PREFIX)) {
return new PluginDefine(pluginName.substring(PLUGIN_OFF_PREFIX.length()), defineClass, State.OFF);
} else {
return new PluginDefine(pluginName, defineClass, State.ON);
}
}
public boolean enable() {
return !Config.Plugin.DISABLED_PLUGINS.contains(name);
return !forceDisable() || forceEnable();
}
private boolean forceDisable() {
return state != State.ON || Config.Plugin.DISABLED_PLUGINS.contains(name);
}
private boolean forceEnable() {
return state == State.OFF && Config.Plugin.FORCE_ENABLE_PLUGINS.contains(name);
}
public String getDefineClass() {
return defineClass;
}
private enum State {
OFF, ON;
}
}
package org.skywalking.apm.agent.core.plugin;
import org.junit.Test;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PluginDefineTest {
private static final String TEST_PLUGIN = "test_plugin";
private static final String TEST_DEFINE_CLASS = "test_define_class";
@Test(expected = IllegalPluginDefineException.class)
public void testIllegalPluginDefine() throws IllegalPluginDefineException {
PluginDefine.build("illegal_plugin_define");
}
@Test(expected = IllegalPluginDefineException.class)
public void testEmptyPluginDefine() throws IllegalPluginDefineException {
PluginDefine.build("");
}
@Test
public void testOffStatePlugin() throws IllegalPluginDefineException {
PluginDefine pluginDefine = PluginDefine.build(PluginDefine.PLUGIN_OFF_PREFIX + TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
assertFalse(pluginDefine.enable());
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
}
@Test
public void testDefaultStatePlugin() throws IllegalPluginDefineException {
PluginDefine pluginDefine = PluginDefine.build(TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
assertTrue(pluginDefine.enable());
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
}
@Test
public void testForceEnablePlugin() throws IllegalPluginDefineException {
Config.Plugin.FORCE_ENABLE_PLUGINS.add(TEST_PLUGIN);
PluginDefine pluginDefine = PluginDefine.build(PluginDefine.PLUGIN_OFF_PREFIX + TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
assertTrue(pluginDefine.enable());
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
Config.Plugin.FORCE_ENABLE_PLUGINS.clear();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册