1. 移除BootPlugin模式, 2. 修改JDBC插件模式
This commit is contained in:
parent
306652bc7d
commit
9a741317ea
|
|
@ -5,8 +5,10 @@ import com.ai.cloud.skywalking.conf.AuthDesc;
|
|||
import com.ai.cloud.skywalking.conf.Config;
|
||||
import com.ai.cloud.skywalking.logging.LogManager;
|
||||
import com.ai.cloud.skywalking.logging.Logger;
|
||||
import com.ai.cloud.skywalking.plugin.*;
|
||||
import com.ai.cloud.skywalking.plugin.boot.IBootPluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.AbstractClassEnhancePluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.PluginBootstrap;
|
||||
import com.ai.cloud.skywalking.plugin.PluginDefineCategory;
|
||||
import com.ai.cloud.skywalking.plugin.PluginException;
|
||||
import net.bytebuddy.agent.builder.AgentBuilder;
|
||||
import net.bytebuddy.description.NamedElement;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
|
|
@ -17,7 +19,6 @@ import net.bytebuddy.utility.JavaModule;
|
|||
import java.io.File;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
public class SkyWalkingAgent {
|
||||
private static Logger logger = LogManager.getLogger(SkyWalkingAgent.class);
|
||||
|
|
@ -25,18 +26,21 @@ public class SkyWalkingAgent {
|
|||
public static void premain(String agentArgs, Instrumentation instrumentation) throws PluginException {
|
||||
initConfig();
|
||||
if (AuthDesc.isAuth()) {
|
||||
final PluginDefineCategory pluginDefineCategory = PluginDefineCategory.category(new PluginBootstrap().loadPlugins());
|
||||
final PluginDefineCategory pluginDefineCategory =
|
||||
PluginDefineCategory.category(new PluginBootstrap().loadPlugins());
|
||||
|
||||
startBootPluginDefines(pluginDefineCategory.getBootPluginsDefines());
|
||||
|
||||
new AgentBuilder.Default().type(enhanceClassMatcher(pluginDefineCategory)).transform(new AgentBuilder.Transformer() {
|
||||
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader) {
|
||||
AbstractClassEnhancePluginDefine pluginDefine = pluginDefineCategory.findPluginDefine(typeDescription.getTypeName());
|
||||
return pluginDefine.define(typeDescription.getTypeName(), builder);
|
||||
}
|
||||
}).with(new AgentBuilder.Listener() {
|
||||
new AgentBuilder.Default().type(enhanceClassMatcher(pluginDefineCategory))
|
||||
.transform(new AgentBuilder.Transformer() {
|
||||
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder,
|
||||
TypeDescription typeDescription, ClassLoader classLoader) {
|
||||
AbstractClassEnhancePluginDefine pluginDefine =
|
||||
pluginDefineCategory.findPluginDefine(typeDescription.getTypeName());
|
||||
return pluginDefine.define(typeDescription.getTypeName(), builder);
|
||||
}
|
||||
}).with(new AgentBuilder.Listener() {
|
||||
@Override
|
||||
public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule module, DynamicType dynamicType) {
|
||||
public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader,
|
||||
JavaModule module, DynamicType dynamicType) {
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -58,18 +62,11 @@ public class SkyWalkingAgent {
|
|||
}
|
||||
|
||||
|
||||
private static <T extends NamedElement> ElementMatcher.Junction<T> enhanceClassMatcher(PluginDefineCategory pluginDefineCategory) {
|
||||
private static <T extends NamedElement> ElementMatcher.Junction<T> enhanceClassMatcher(
|
||||
PluginDefineCategory pluginDefineCategory) {
|
||||
return new SkyWalkingEnhanceMatcher<T>(pluginDefineCategory);
|
||||
}
|
||||
|
||||
|
||||
public static void startBootPluginDefines(List<IBootPluginDefine> IBootPluginDefines) throws PluginException {
|
||||
for (IBootPluginDefine bootPluginDefine : IBootPluginDefines) {
|
||||
bootPluginDefine.boot();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static String generateLocationPath() {
|
||||
return SkyWalkingAgent.class.getName().replaceAll("\\.", "/") + ".class";
|
||||
}
|
||||
|
|
@ -82,7 +79,9 @@ public class SkyWalkingAgent {
|
|||
|
||||
private static String initAgentBasePath() {
|
||||
try {
|
||||
String urlString = SkyWalkingAgent.class.getClassLoader().getSystemClassLoader().getResource(generateLocationPath()).toString();
|
||||
String urlString =
|
||||
SkyWalkingAgent.class.getClassLoader().getSystemClassLoader().getResource(generateLocationPath())
|
||||
.toString();
|
||||
urlString = urlString.substring(urlString.indexOf("file:"), urlString.indexOf('!'));
|
||||
return new File(new URL(urlString).getFile()).getParentFile().getAbsolutePath();
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import net.bytebuddy.pool.TypePool.Resolution;
|
|||
|
||||
import static com.ai.cloud.skywalking.plugin.PluginBootstrap.CLASS_TYPE_POOL;
|
||||
|
||||
public abstract class AbstractClassEnhancePluginDefine implements IPlugin {
|
||||
public abstract class AbstractClassEnhancePluginDefine{
|
||||
private static Logger logger = LogManager.getLogger(AbstractClassEnhancePluginDefine.class);
|
||||
|
||||
public DynamicType.Builder<?> define(String transformClassName, DynamicType.Builder<?> builder) throws PluginException {
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin;
|
||||
|
||||
|
||||
public interface IPlugin {
|
||||
}
|
||||
|
|
@ -13,7 +13,7 @@ public class PluginBootstrap {
|
|||
|
||||
public static TypePool CLASS_TYPE_POOL = null;
|
||||
|
||||
public List<IPlugin> loadPlugins() {
|
||||
public List<AbstractClassEnhancePluginDefine> loadPlugins() {
|
||||
CLASS_TYPE_POOL = TypePool.Default.ofClassPath();
|
||||
|
||||
PluginResourcesResolver resolver = new PluginResourcesResolver();
|
||||
|
|
@ -21,7 +21,7 @@ public class PluginBootstrap {
|
|||
|
||||
if (resources == null || resources.size() == 0) {
|
||||
logger.info("no plugin files (skywalking-plugin.properties) found, continue to start application.");
|
||||
return new ArrayList<IPlugin>();
|
||||
return new ArrayList<AbstractClassEnhancePluginDefine>();
|
||||
}
|
||||
|
||||
for (URL pluginUrl : resources) {
|
||||
|
|
@ -34,11 +34,12 @@ public class PluginBootstrap {
|
|||
|
||||
List<String> pluginClassList = PluginCfg.CFG.getPluginClassList();
|
||||
|
||||
List<IPlugin> plugins = new ArrayList<IPlugin>();
|
||||
List<AbstractClassEnhancePluginDefine> plugins = new ArrayList<AbstractClassEnhancePluginDefine>();
|
||||
for (String pluginClassName : pluginClassList) {
|
||||
try {
|
||||
logger.debug("prepare to enhance class by plugin {}.", pluginClassName);
|
||||
IPlugin plugin = (IPlugin) Class.forName(pluginClassName).newInstance();
|
||||
AbstractClassEnhancePluginDefine plugin =
|
||||
(AbstractClassEnhancePluginDefine) Class.forName(pluginClassName).newInstance();
|
||||
plugins.add(plugin);
|
||||
} catch (Throwable t) {
|
||||
logger.error("prepare to enhance class by plugin [{}] failure.", new Object[] {pluginClassName}, t);
|
||||
|
|
|
|||
|
|
@ -1,8 +1,5 @@
|
|||
package com.ai.cloud.skywalking.plugin;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.boot.IBootPluginDefine;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -13,42 +10,29 @@ public class PluginDefineCategory {
|
|||
|
||||
private final Map<String, AbstractClassEnhancePluginDefine> exactClassEnhancePluginDefineMapping =
|
||||
new HashMap<String, AbstractClassEnhancePluginDefine>();
|
||||
private final List<IBootPluginDefine> IBootPluginDefines =
|
||||
new ArrayList<IBootPluginDefine>();
|
||||
private final Map<String, AbstractClassEnhancePluginDefine> blurryClassEnhancePluginDefineMapping =
|
||||
new HashMap<String, AbstractClassEnhancePluginDefine>();
|
||||
|
||||
private PluginDefineCategory(List<IPlugin> plugins) {
|
||||
for (IPlugin plugin : plugins) {
|
||||
if (plugin instanceof AbstractClassEnhancePluginDefine) {
|
||||
String enhanceClassName = ((AbstractClassEnhancePluginDefine) plugin).enhanceClassName();
|
||||
if (enhanceClassName.endsWith("*")) {
|
||||
// 加上. 为了区分 com.ai.test com.ai.test1
|
||||
blurryClassEnhancePluginDefineMapping
|
||||
.put(enhanceClassName.substring(0, enhanceClassName.length() - 1),
|
||||
(AbstractClassEnhancePluginDefine) plugin);
|
||||
} else {
|
||||
exactClassEnhancePluginDefineMapping
|
||||
.put(enhanceClassName, (AbstractClassEnhancePluginDefine) plugin);
|
||||
}
|
||||
}
|
||||
|
||||
if (plugin instanceof IBootPluginDefine) {
|
||||
IBootPluginDefines.add((IBootPluginDefine) plugin);
|
||||
private PluginDefineCategory(List<AbstractClassEnhancePluginDefine> plugins) {
|
||||
for (AbstractClassEnhancePluginDefine plugin : plugins) {
|
||||
String enhanceClassName = plugin.enhanceClassName();
|
||||
if (enhanceClassName.endsWith("*")) {
|
||||
// 加上. 为了区分 com.ai.test com.ai.test1
|
||||
blurryClassEnhancePluginDefineMapping
|
||||
.put(enhanceClassName.substring(0, enhanceClassName.length() - 1), plugin);
|
||||
} else {
|
||||
exactClassEnhancePluginDefineMapping.put(enhanceClassName, plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PluginDefineCategory category(List<IPlugin> plugins) {
|
||||
public static PluginDefineCategory category(List<AbstractClassEnhancePluginDefine> plugins) {
|
||||
if (pluginDefineCategory == null) {
|
||||
pluginDefineCategory = new PluginDefineCategory(plugins);
|
||||
}
|
||||
return pluginDefineCategory;
|
||||
}
|
||||
|
||||
public List<IBootPluginDefine> getBootPluginsDefines() {
|
||||
return pluginDefineCategory.IBootPluginDefines;
|
||||
}
|
||||
|
||||
public Map<String, AbstractClassEnhancePluginDefine> getExactClassEnhancePluginDefineMapping() {
|
||||
return pluginDefineCategory.exactClassEnhancePluginDefineMapping;
|
||||
|
|
@ -59,12 +43,13 @@ public class PluginDefineCategory {
|
|||
}
|
||||
|
||||
public AbstractClassEnhancePluginDefine findPluginDefine(String enhanceClassName) {
|
||||
if (exactClassEnhancePluginDefineMapping.containsKey(enhanceClassName)){
|
||||
if (exactClassEnhancePluginDefineMapping.containsKey(enhanceClassName)) {
|
||||
return exactClassEnhancePluginDefineMapping.get(enhanceClassName);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, AbstractClassEnhancePluginDefine> entry : blurryClassEnhancePluginDefineMapping.entrySet()){
|
||||
if (enhanceClassName.startsWith(entry.getKey())){
|
||||
for (Map.Entry<String, AbstractClassEnhancePluginDefine> entry : blurryClassEnhancePluginDefineMapping
|
||||
.entrySet()) {
|
||||
if (enhanceClassName.startsWith(entry.getKey())) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package com.ai.cloud.skywalking.plugin;
|
|||
|
||||
import com.ai.cloud.skywalking.logging.LogManager;
|
||||
import com.ai.cloud.skywalking.logging.Logger;
|
||||
import com.ai.cloud.skywalking.plugin.boot.IBootPluginDefine;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.dynamic.ClassFileLocator;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
|
|
@ -25,12 +24,14 @@ public class TracingBootstrap {
|
|||
private TracingBootstrap() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws PluginException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
public static void main(String[] args)
|
||||
throws PluginException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException,
|
||||
IllegalAccessException {
|
||||
if (args.length == 0) {
|
||||
throw new RuntimeException("bootstrap failure. need args[0] to be main class.");
|
||||
}
|
||||
|
||||
List<IPlugin> plugins = null;
|
||||
List<AbstractClassEnhancePluginDefine> plugins = null;
|
||||
|
||||
try {
|
||||
PluginBootstrap bootstrap = new PluginBootstrap();
|
||||
|
|
@ -39,24 +40,18 @@ public class TracingBootstrap {
|
|||
logger.error("PluginBootstrap start failure.", t);
|
||||
}
|
||||
|
||||
for (IPlugin plugin : plugins) {
|
||||
if (plugin instanceof AbstractClassEnhancePluginDefine) {
|
||||
String enhanceClassName = ((AbstractClassEnhancePluginDefine) plugin).enhanceClassName();
|
||||
TypePool.Resolution resolution = TypePool.Default.ofClassPath().describe(enhanceClassName);
|
||||
if (!resolution.isResolved()) {
|
||||
logger.error("Failed to resolve the class " + enhanceClassName);
|
||||
continue;
|
||||
}
|
||||
DynamicType.Builder<?> newClassBuilder = new ByteBuddy().rebase(resolution.resolve(), ClassFileLocator.ForClassLoader.ofClassPath());
|
||||
newClassBuilder = ((AbstractClassEnhancePluginDefine)plugin).define(enhanceClassName, newClassBuilder);
|
||||
newClassBuilder.make().load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();
|
||||
for (AbstractClassEnhancePluginDefine plugin : plugins) {
|
||||
String enhanceClassName = plugin.enhanceClassName();
|
||||
TypePool.Resolution resolution = TypePool.Default.ofClassPath().describe(enhanceClassName);
|
||||
if (!resolution.isResolved()) {
|
||||
logger.error("Failed to resolve the class " + enhanceClassName);
|
||||
continue;
|
||||
}
|
||||
|
||||
if(plugin instanceof IBootPluginDefine){
|
||||
((IBootPluginDefine)plugin).boot();
|
||||
}
|
||||
|
||||
|
||||
DynamicType.Builder<?> newClassBuilder =
|
||||
new ByteBuddy().rebase(resolution.resolve(), ClassFileLocator.ForClassLoader.ofClassPath());
|
||||
newClassBuilder = ((AbstractClassEnhancePluginDefine) plugin).define(enhanceClassName, newClassBuilder);
|
||||
newClassBuilder.make().load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION)
|
||||
.getLoaded();
|
||||
}
|
||||
|
||||
String[] newArgs = Arrays.copyOfRange(args, 1, args.length);
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.boot;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.PluginException;
|
||||
|
||||
public class BootException extends PluginException {
|
||||
private static final long serialVersionUID = 8618884011525098003L;
|
||||
|
||||
public BootException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BootException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.boot;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.IPlugin;
|
||||
|
||||
public interface IBootPluginDefine extends IPlugin {
|
||||
|
||||
void boot() throws BootException;
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,9 @@
|
|||
package test.ai.cloud.matcher;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.*;
|
||||
import com.ai.cloud.skywalking.plugin.AbstractClassEnhancePluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.PluginBootstrap;
|
||||
import com.ai.cloud.skywalking.plugin.PluginDefineCategory;
|
||||
import com.ai.cloud.skywalking.plugin.PluginException;
|
||||
import junit.framework.TestCase;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.dynamic.ClassFileLocator;
|
||||
|
|
@ -14,21 +17,27 @@ import java.util.Map;
|
|||
|
||||
public class ExclusionMatcherTest extends TestCase {
|
||||
@Test
|
||||
public void testMatcher() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, PluginException {
|
||||
List<IPlugin> pluginDefines = new PluginBootstrap().loadPlugins();
|
||||
public void testMatcher()
|
||||
throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException,
|
||||
PluginException {
|
||||
List<AbstractClassEnhancePluginDefine> pluginDefines = new PluginBootstrap().loadPlugins();
|
||||
|
||||
PluginDefineCategory category = PluginDefineCategory.category(pluginDefines);
|
||||
PluginDefineCategory category = PluginDefineCategory.category(pluginDefines);
|
||||
|
||||
|
||||
for (Map.Entry<String, AbstractClassEnhancePluginDefine> entry : category.getExactClassEnhancePluginDefineMapping().entrySet()) {
|
||||
DynamicType.Builder<?> newClassBuilder =
|
||||
new ByteBuddy().rebase(TypePool.Default.ofClassPath().describe(entry.getKey()).resolve(), ClassFileLocator.ForClassLoader.ofClassPath());
|
||||
for (Map.Entry<String, AbstractClassEnhancePluginDefine> entry : category
|
||||
.getExactClassEnhancePluginDefineMapping().entrySet()) {
|
||||
DynamicType.Builder<?> newClassBuilder = new ByteBuddy()
|
||||
.rebase(TypePool.Default.ofClassPath().describe(entry.getKey()).resolve(),
|
||||
ClassFileLocator.ForClassLoader.ofClassPath());
|
||||
|
||||
newClassBuilder = entry.getValue().define(entry.getKey(), newClassBuilder);
|
||||
newClassBuilder.make().load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();
|
||||
newClassBuilder.make().load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION)
|
||||
.getLoaded();
|
||||
}
|
||||
|
||||
TestMatcherClass testMatcherClass = (TestMatcherClass) Class.forName("test.ai.cloud.matcher.TestMatcherClass").newInstance();
|
||||
TestMatcherClass testMatcherClass =
|
||||
(TestMatcherClass) Class.forName("test.ai.cloud.matcher.TestMatcherClass").newInstance();
|
||||
|
||||
testMatcherClass.set();
|
||||
testMatcherClass.seta("a");
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package test.ai.cloud.plugin;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.AbstractClassEnhancePluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.IPlugin;
|
||||
import com.ai.cloud.skywalking.plugin.PluginDefineCategory;
|
||||
import com.ai.cloud.skywalking.plugin.PluginException;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
|
|
@ -41,7 +40,7 @@ public class PluginDefineCategoryTest {
|
|||
return "com.ai.test.*";
|
||||
}
|
||||
});
|
||||
List<IPlugin> plugins = new ArrayList<IPlugin>();
|
||||
List<AbstractClassEnhancePluginDefine> plugins = new ArrayList<AbstractClassEnhancePluginDefine>();
|
||||
plugins.add(define1);
|
||||
|
||||
pluginDefineCategory = PluginDefineCategory.category(plugins);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.plugin.jdbc;
|
|||
|
||||
import com.ai.cloud.skywalking.invoke.monitor.RPCClientInvokeMonitor;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.define.JDBCBuriedPointType;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.plugin.jdbc;
|
|||
|
||||
import com.ai.cloud.skywalking.invoke.monitor.RPCClientInvokeMonitor;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.define.JDBCBuriedPointType;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,48 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.logging.LogManager;
|
||||
import com.ai.cloud.skywalking.logging.Logger;
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootException;
|
||||
import com.ai.cloud.skywalking.plugin.boot.IBootPluginDefine;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class JDBCPluginDefine implements IBootPluginDefine {
|
||||
private static Logger logger = LogManager.getLogger(JDBCPluginDefine.class);
|
||||
|
||||
@Override
|
||||
public void boot() throws BootException {
|
||||
try {
|
||||
Class<?> classes = Class.forName("java.sql.DriverInfo");
|
||||
Object traceDriverInfo = newDriverInfoInstance(classes);
|
||||
Field field = DriverManager.class.getDeclaredField("registeredDrivers");
|
||||
field.setAccessible(true);
|
||||
@SuppressWarnings("unchecked") CopyOnWriteArrayList<Object> copyOnWriteArrayList =
|
||||
(CopyOnWriteArrayList<Object>) field.get(DriverManager.class);
|
||||
copyOnWriteArrayList.add(0, traceDriverInfo);
|
||||
} catch (Throwable e) {
|
||||
// 开启补偿机制
|
||||
logger.error(
|
||||
"Failed to inject TracingDriver to the top of registered Drivers. Need to alter jdbc url to trace.",
|
||||
e);
|
||||
TracingDriver.registerDriver();
|
||||
}
|
||||
}
|
||||
|
||||
private Object newDriverInfoInstance(Class<?> classes)
|
||||
throws NoSuchMethodException, InstantiationException, IllegalAccessException,
|
||||
java.lang.reflect.InvocationTargetException {
|
||||
//DriverInfo 只有一个构造函数
|
||||
Constructor constructor = classes.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
// JDK 1.7 DriverInfo 只有一个Driver入参构造函数
|
||||
if (constructor.getParameterTypes().length == 1) {
|
||||
return constructor.newInstance(new TracingDriver());
|
||||
}
|
||||
// JDK1.8 DriverInfo 有两个入参的构造函数(Driver, DriverAction)
|
||||
return constructor.newInstance(new TracingDriver(), null);
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.plugin.jdbc;
|
|||
|
||||
import com.ai.cloud.skywalking.invoke.monitor.RPCClientInvokeMonitor;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.define.JDBCBuriedPointType;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.plugin.jdbc;
|
|||
|
||||
import com.ai.cloud.skywalking.invoke.monitor.RPCClientInvokeMonitor;
|
||||
import com.ai.cloud.skywalking.model.Identification;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.define.JDBCBuriedPointType;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,100 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.AuthDesc;
|
||||
import com.ai.cloud.skywalking.logging.LogManager;
|
||||
import com.ai.cloud.skywalking.logging.Logger;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Properties;
|
||||
|
||||
public class TracingDriver implements Driver {
|
||||
private static Logger logger = LogManager.getLogger(TracingDriver.class);
|
||||
|
||||
private static final String TRACING_SIGN = "tracing:";
|
||||
|
||||
public static final void registerDriver() {
|
||||
try {
|
||||
DriverManager.registerDriver(new TracingDriver());
|
||||
} catch (SQLException e) {
|
||||
logger.error("register TracingDriver failure.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String convertConnectURLIfNecessary(String url) throws SQLException {
|
||||
if (url.toLowerCase().startsWith(TRACING_SIGN)) {
|
||||
return url.substring(TRACING_SIGN.length());
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
public java.sql.Connection connect(String url, Properties info) throws SQLException {
|
||||
Driver driver = chooseActualDriver(convertConnectURLIfNecessary(url));
|
||||
if (driver == null) {
|
||||
throw new SQLException("Failed to chooseActualDriver driver by url[{}].", convertConnectURLIfNecessary(url));
|
||||
}
|
||||
|
||||
java.sql.Connection conn = driver.
|
||||
connect(convertConnectURLIfNecessary(url), info);
|
||||
|
||||
if (!AuthDesc.isAuth()) {
|
||||
return conn;
|
||||
} else {
|
||||
return new SWConnection(convertConnectURLIfNecessary(url), info, conn);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean acceptsURL(String url) throws SQLException {
|
||||
Driver driver = chooseActualDriver(convertConnectURLIfNecessary(url));
|
||||
if (driver == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return driver.acceptsURL(convertConnectURLIfNecessary(url));
|
||||
}
|
||||
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
|
||||
return chooseActualDriver(convertConnectURLIfNecessary(url)).
|
||||
getPropertyInfo(convertConnectURLIfNecessary(url), info);
|
||||
}
|
||||
|
||||
public int getMajorVersion() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public int getMinorVersion() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean jdbcCompliant() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Driver chooseActualDriver(String url) throws SQLException {
|
||||
try {
|
||||
Driver result = null;
|
||||
for (Enumeration<Driver> drivers = DriverManager.getDrivers(); drivers.hasMoreElements(); ) {
|
||||
Driver driver = drivers.nextElement();
|
||||
if (driver instanceof TracingDriver) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (driver.acceptsURL(url)) {
|
||||
result = driver;
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to chooseActualDriver sql driver with url[" + url + "]", e);
|
||||
throw new SQLException("Failed to chooseActualDriver sql driver with url[" + url + "]", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.define;
|
||||
|
||||
public class H2PluginDefine extends AbstractDatabasePluginDefine {
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return "org.h2.Driver";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
package com.ai.cloud.skywalking.plugin.jdbc.define;
|
||||
|
||||
import com.ai.cloud.skywalking.api.IBuriedPointType;
|
||||
import com.ai.cloud.skywalking.protocol.common.CallType;
|
||||
|
|
@ -10,7 +10,7 @@ import com.ai.cloud.skywalking.plugin.jdbc.SWConnection;
|
|||
import java.sql.Connection;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DatabasePluginInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
public class JDBCDriverInterceptor implements InstanceMethodsAroundInterceptor {
|
||||
@Override
|
||||
public void onConstruct(EnhancedClassInstanceContext context, ConstructorInvokeContext interceptorContext) {
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ public class DatabasePluginInterceptor implements InstanceMethodsAroundIntercept
|
|||
@Override
|
||||
public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext,
|
||||
MethodInterceptResult result) {
|
||||
|
||||
System.out.println("Data/////");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -30,7 +30,8 @@ public class DatabasePluginInterceptor implements InstanceMethodsAroundIntercept
|
|||
|
||||
@Override
|
||||
public void handleMethodException(Throwable t, EnhancedClassInstanceContext context,
|
||||
InstanceMethodInvokeContext interceptorContext, Object ret) {
|
||||
InstanceMethodInvokeContext interceptorContext) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.define;
|
||||
|
||||
/**
|
||||
* Created by xin on 16/8/4.
|
||||
*/
|
||||
public class MysqlPluginDefine extends AbstractDatabasePluginDefine {
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return "com.mysql.jdbc.Driver";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.define;
|
||||
|
||||
/**
|
||||
* Created by xin on 16/8/4.
|
||||
*/
|
||||
public class OraclePluginDefine extends AbstractDatabasePluginDefine {
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return "oracle.jdbc.OracleDriver";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.plugin;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.define.AbstractDatabasePluginDefine;
|
||||
|
||||
public class H2DatabasePluginDefine extends AbstractDatabasePluginDefine {
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return "org.h2.Driver";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
jdbc:mysql=com.mysql.jdbc.Driver
|
||||
jdbc:oracle=oracle.jdbc.driver.OracleDriver
|
||||
jdbc:sybase=com.sybase.jdbc2.jdbc.SybDriver
|
||||
jdbc:microsoft:sqlserver=com.microsoft.jdbc.sqlserver.SQLServerDriver
|
||||
jdbc:sqlserver=com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
jdbc:jtds:sqlserver=net.sourceforge.jtds.jdbc.Driver
|
||||
jdbc:db2=com.ibm.db2.jcc.DB2Driver
|
||||
jdbc:informix-sqli=com.informix.jdbc.IfxDriver
|
||||
jdbc:h2=org.h2.Driver
|
||||
|
|
@ -1 +1,3 @@
|
|||
com.ai.cloud.skywalking.plugin.jdbc.plugin.H2DatabasePluginDefine
|
||||
com.ai.cloud.skywalking.plugin.jdbc.define.H2DatabasePluginDefine
|
||||
com.ai.cloud.skywalking.plugin.jdbc.define.MysqlPluginDefine
|
||||
com.ai.cloud.skywalking.plugin.jdbc.define.OraclePluginDefine
|
||||
|
|
|
|||
Loading…
Reference in New Issue