提交 4a560433 编写于 作者: X Xin,Zhang 提交者: wu-sheng

Add mechanism to disable any or all plugins (#191)

* Add mechanism to disable any or all plugins, Relate: #186
上级 5f4fe494
......@@ -3,6 +3,7 @@ package org.skywalking.apm.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
......@@ -20,7 +21,7 @@ public class ConfigInitializer {
}
private static void initNextLevel(Properties properties, Class<?> recentConfigType,
ConfigDesc parentDesc) throws IllegalArgumentException, IllegalAccessException {
ConfigDesc parentDesc) throws IllegalArgumentException, IllegalAccessException {
for (Field field : recentConfigType.getFields()) {
if (Modifier.isPublic(field.getModifiers()) && Modifier.isStatic(field.getModifiers())) {
String configKey = (parentDesc + "." + field.getName()).toLowerCase();
......@@ -35,8 +36,10 @@ public class ConfigInitializer {
field.set(null, Long.valueOf(value));
else if (type.equals(boolean.class))
field.set(null, Boolean.valueOf(value));
else if (type.equals(List.class))
field.set(null, convert2List(value));
else if (type.isEnum())
field.set(null, Enum.valueOf((Class<Enum>) type, value.toUpperCase()));
field.set(null, Enum.valueOf((Class<Enum>)type, value.toUpperCase()));
}
}
}
......@@ -46,6 +49,22 @@ public class ConfigInitializer {
parentDesc.removeLastDesc();
}
}
private static List convert2List(String value) {
List result = new LinkedList();
if (StringUtil.isEmpty(value)) {
return result;
}
String[] segments = value.split(",");
for (String segment : segments) {
String trimmedSegment = segment.trim();
if (!StringUtil.isEmpty(trimmedSegment)) {
result.add(trimmedSegment);
}
}
return result;
}
}
class ConfigDesc {
......
package org.skywalking.apm.agent.core.conf;
import java.util.LinkedList;
import java.util.List;
import org.skywalking.apm.agent.core.logging.LogLevel;
import org.skywalking.apm.agent.core.logging.WriterFactory;
......@@ -83,6 +85,16 @@ public class Config {
}
public static class Plugin {
/**
* Name of disabled plugin, The value spilt by <code>,</code>
* if you have multiple plugins need to disable.
*
* Here are the plugin names :
* tomcat-7.x/8.x, dubbo, jedis-2.x, motan, httpclient-4.x, jdbc, mongodb-3.x.
*/
public static List DISABLED_PLUGINS = new LinkedList();
public static class MongoDB {
/**
* If true, trace all the parameters, default is false.
......@@ -90,5 +102,6 @@ public class Config {
*/
public static boolean TRACE_PARAM = false;
}
}
}
......@@ -42,18 +42,18 @@ public class PluginBootstrap {
}
}
List<String> pluginClassList = PluginCfg.INSTANCE.getPluginClassList();
List<PluginDefine> pluginClassList = PluginCfg.INSTANCE.getPluginClassList();
List<AbstractClassEnhancePluginDefine> plugins = new ArrayList<AbstractClassEnhancePluginDefine>();
for (String pluginClassName : pluginClassList) {
for (PluginDefine pluginDefine : pluginClassList) {
try {
logger.debug("loading plugin class {}.", pluginClassName);
logger.debug("loading plugin class {}.", pluginDefine.getDefineClass());
AbstractClassEnhancePluginDefine plugin =
(AbstractClassEnhancePluginDefine) Class.forName(pluginClassName).newInstance();
(AbstractClassEnhancePluginDefine) Class.forName(pluginDefine.getDefineClass()).newInstance();
plugin.setClassTypePool(classTypePool);
plugins.add(plugin);
} catch (Throwable t) {
logger.error(t, "load plugin [{}] failure.", pluginClassName);
logger.error(t, "load plugin [{}] failure.", pluginDefine.getDefineClass());
}
}
......
package org.skywalking.apm.agent.core.plugin;
import org.skywalking.apm.util.StringUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineException;
import org.skywalking.apm.logging.ILog;
import org.skywalking.apm.logging.LogManager;
public enum PluginCfg {
INSTANCE;
private List<String> pluginClassList = new ArrayList<String>();
private static final ILog logger = LogManager.getLogger(PluginCfg.class);
private List<PluginDefine> pluginClassList = new ArrayList<PluginDefine>();
void load(InputStream input) throws IOException {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String pluginDefineClassName = null;
while ((pluginDefineClassName = reader.readLine()) != null) {
if (!StringUtil.isEmpty(pluginDefineClassName)) {
pluginClassList.add(pluginDefineClassName.trim());
String pluginDefine = null;
while ((pluginDefine = reader.readLine()) != null) {
try {
PluginDefine plugin = PluginDefine.build(pluginDefine);
if (plugin.enable()) {
pluginClassList.add(plugin);
}
} catch (IllegalPluginDefineException e) {
logger.error("Failed to format plugin define.", e);
}
}
} finally {
......@@ -28,7 +36,8 @@ public enum PluginCfg {
}
}
public List<String> getPluginClassList() {
public List<PluginDefine> getPluginClassList() {
return pluginClassList;
}
}
package org.skywalking.apm.agent.core.plugin;
import org.skywalking.apm.agent.core.conf.Config;
import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineException;
import org.skywalking.apm.util.StringUtil;
public class PluginDefine {
/**
* Plugin name.
*/
private String name;
/**
* The class name of plugin defined.
*/
private String defineClass;
private PluginDefine(String name, String defineClass) {
this.name = name;
this.defineClass = defineClass;
}
public static PluginDefine build(String define) throws IllegalPluginDefineException {
if (StringUtil.isEmpty(define)) {
throw new IllegalPluginDefineException(define);
}
String[] pluginDefine = define.split("=");
if (pluginDefine.length != 2) {
throw new IllegalPluginDefineException(define);
}
return new PluginDefine(pluginDefine[0], pluginDefine[1]);
}
public boolean enable() {
return !Config.Plugin.DISABLED_PLUGINS.contains(name);
}
public String getDefineClass() {
return defineClass;
}
}
package org.skywalking.apm.agent.core.plugin.exception;
/**
* Thrown to indicate that a illegal format plugin definition has been defined in skywalking-plugin.define.
*/
public class IllegalPluginDefineException extends Exception {
public IllegalPluginDefineException(String define) {
super("Illegal plugin define : " + define);
}
}
org.skywalking.apm.agent.core.queue.TraceSegmentProcessQueue
org.skywalking.apm.agent.core.context.ContextManager
org.skywalking.apm.agent.core.client.CollectorClientService
org.skywalking.apm.agent.core.sampling.SamplingService
org.skywalking.apm.agent.core.sampling.SamplingService
\ No newline at end of file
......@@ -17,7 +17,7 @@ import java.util.List;
public class PluginCfgTest {
@Test
public void testLoad() throws IOException {
String data = "com.test.classA\r\ncom.test.ClassB";
String data = "TestA=com.test.classA\r\nTestB=com.test.ClassB";
final byte[] dataBytes = data.getBytes();
PluginCfg.INSTANCE.load(new InputStream() {
int index = 0;
......@@ -31,10 +31,10 @@ public class PluginCfgTest {
}
});
List<String> list = PluginCfg.INSTANCE.getPluginClassList();
List<PluginDefine> list = PluginCfg.INSTANCE.getPluginClassList();
Assert.assertEquals(2, list.size());
Assert.assertEquals("com.test.classA", list.get(0));
Assert.assertEquals("com.test.ClassB", list.get(1));
Assert.assertEquals("com.test.classA", list.get(0).getDefineClass());
Assert.assertEquals("com.test.ClassB", list.get(1).getDefineClass());
}
@Before
......
org.skywalking.apm.agent.core.plugin.MockAbstractClassEnhancePluginDefine
MOCKPLUGIN=org.skywalking.apm.agent.core.plugin.MockAbstractClassEnhancePluginDefine
org.skywalking.apm.plugin.dubbo.DubboInstrumentation
dubbo=org.skywalking.apm.plugin.dubbo.DubboInstrumentation
\ No newline at end of file
org.skywalking.apm.plugin.httpClient.v4.define.AbstractHttpClientInstrumentation
org.skywalking.apm.plugin.httpClient.v4.define.InternalHttpClientInstrumentation
org.skywalking.apm.plugin.httpClient.v4.define.MinimalHttpClientInstrumentation
org.skywalking.apm.plugin.httpClient.v4.define.DefaultRequestDirectorInstrumentation
httpclient-4.x=org.skywalking.apm.plugin.httpClient.v4.define.AbstractHttpClientInstrumentation
httpclient-4.x=org.skywalking.apm.plugin.httpClient.v4.define.InternalHttpClientInstrumentation
httpclient-4.x=org.skywalking.apm.plugin.httpClient.v4.define.MinimalHttpClientInstrumentation
httpclient-4.x=org.skywalking.apm.plugin.httpClient.v4.define.DefaultRequestDirectorInstrumentation
\ No newline at end of file
org.skywalking.apm.plugin.jdbc.define.H2Instrumentation
org.skywalking.apm.plugin.jdbc.define.MysqlInstrumentation
org.skywalking.apm.plugin.jdbc.define.OracleInstrumentation
jdbc=org.skywalking.apm.plugin.jdbc.define.H2Instrumentation
jdbc=org.skywalking.apm.plugin.jdbc.define.MysqlInstrumentation
jdbc=org.skywalking.apm.plugin.jdbc.define.OracleInstrumentation
\ No newline at end of file
org.skywalking.apm.plugin.jedis.v2.define.JedisClusterInstrumentation
org.skywalking.apm.plugin.jedis.v2.define.JedisInstrumentation
jedis-2.x=org.skywalking.apm.plugin.jedis.v2.define.JedisClusterInstrumentation
jedis-2.x=org.skywalking.apm.plugin.jedis.v2.define.JedisInstrumentation
\ No newline at end of file
org.skywalking.apm.plugin.mongodb.v3.define.MongoDBInstrumentation
\ No newline at end of file
mongodb-3.x=org.skywalking.apm.plugin.mongodb.v3.define.MongoDBInstrumentation
\ No newline at end of file
org.skywalking.apm.plugin.motan.define.MotanConsumerInstrumentation
org.skywalking.apm.plugin.motan.define.MotanProviderInstrumentation
motan-0.x=org.skywalking.apm.plugin.motan.define.MotanConsumerInstrumentation
motan-0.x=org.skywalking.apm.plugin.motan.define.MotanProviderInstrumentation
\ No newline at end of file
org.skywalking.apm.plugin.tomcat78x.define.TomcatInstrumentation
tomcat-7.x/8.x=org.skywalking.apm.plugin.tomcat78x.define.TomcatInstrumentation
\ No newline at end of file
org.skywalking.apm.toolkit.activation.log.log4j.v1.x.TraceIdPatternConverterActivation
log4j=org.skywalking.apm.toolkit.activation.log.log4j.v1.x.TraceIdPatternConverterActivation
\ No newline at end of file
org.skywalking.apm.toolkit.activation.log.log4j.v2.x.Log4j2OutputAppenderActivation
log4j2=org.skywalking.apm.toolkit.activation.log.log4j.v2.x.Log4j2OutputAppenderActivation
\ No newline at end of file
org.skywalking.apm.toolkit.activation.log.logback.v1.x.LogbackPatternConverterActivation
logback=org.skywalking.apm.toolkit.activation.log.logback.v1.x.LogbackPatternConverterActivation
\ No newline at end of file
org.skywalking.apm.toolkit.activation.opentracing.span.SkyWalkingSpanActivation
org.skywalking.apm.toolkit.activation.opentracing.tracer.SkyWalkingTracerActivation
opentracing=org.skywalking.apm.toolkit.activation.opentracing.span.SkyWalkingSpanActivation
opentracing=org.skywalking.apm.toolkit.activation.opentracing.tracer.SkyWalkingTracerActivation
\ No newline at end of file
org.skywalking.apm.toolkit.activation.trace.TraceContextActivation
\ No newline at end of file
tracecontext=org.skywalking.apm.toolkit.activation.trace.TraceContextActivation
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册