上传部分jdbc驱动的修改,需要后续完善。
This commit is contained in:
parent
99d25290d3
commit
0b47d1c44a
|
|
@ -1,36 +1,39 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc;
|
||||
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootException;
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootPluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.driver.TracingDriver;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class JDBCPluginDefine extends BootPluginDefine {
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
private Logger logger = LogManager.getLogger(JDBCPluginDefine.class);
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootException;
|
||||
import com.ai.cloud.skywalking.plugin.boot.BootPluginDefine;
|
||||
import com.ai.cloud.skywalking.plugin.jdbc.driver.TracingDriver;
|
||||
|
||||
public class JDBCPluginDefine extends BootPluginDefine {
|
||||
private static Logger logger = LogManager.getLogger(JDBCPluginDefine.class);
|
||||
|
||||
@Override
|
||||
protected void boot() throws BootException {
|
||||
try {
|
||||
Class classes = Class.forName("java.sql.DriverInfo");
|
||||
Constructor constructor = classes.getDeclaredConstructor(Driver.class);
|
||||
Class<?> classes = Class.forName("java.sql.DriverInfo");
|
||||
Constructor<?> constructor = classes.getDeclaredConstructor(Driver.class);
|
||||
constructor.setAccessible(true);
|
||||
Object traceDriverInfo = constructor.newInstance(new TracingDriver());
|
||||
Field field = DriverManager.class.getDeclaredField("registeredDrivers");
|
||||
field.setAccessible(true);
|
||||
CopyOnWriteArrayList copyOnWriteArrayList = (CopyOnWriteArrayList) field.get(DriverManager.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
CopyOnWriteArrayList<Object> copyOnWriteArrayList = (CopyOnWriteArrayList<Object>) field.get(DriverManager.class);
|
||||
copyOnWriteArrayList.add(0, traceDriverInfo);
|
||||
} catch (Exception e) {
|
||||
// 开启补偿机制
|
||||
logger.error("Failed to change the byte code of DriverManger, Will open compensation mechanism.", e);
|
||||
TracingDriver.registerDriver();
|
||||
}
|
||||
}
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,16 +5,20 @@ import java.sql.Driver;
|
|||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class DriverChooser {
|
||||
private static Logger logger = LogManager.getLogger(DriverChooser.class);
|
||||
|
||||
private static Properties urlDriverMapping = new Properties();
|
||||
|
||||
static {
|
||||
InputStream inputStream = DriverChooser.class.getResourceAsStream("/conurl-driver-mapping.properties");
|
||||
InputStream inputStream = DriverChooser.class.getResourceAsStream("/driver-mapping-url.properties");
|
||||
try {
|
||||
urlDriverMapping.load(inputStream);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load conurl-driver-mapping.properties");
|
||||
logger.error("Failed to load driver-mapping-url.properties");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -24,7 +28,7 @@ public class DriverChooser {
|
|||
Driver driver = null;
|
||||
for (Map.Entry<Object, Object> entry : urlDriverMapping.entrySet()) {
|
||||
if (url.startsWith(entry.getValue().toString())) {
|
||||
Class driverClass = Class.forName(entry.getKey().toString());
|
||||
Class<?> driverClass = Class.forName(entry.getKey().toString());
|
||||
driver = (Driver) driverClass.newInstance();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,107 +1,115 @@
|
|||
package com.ai.cloud.skywalking.plugin.jdbc.driver;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.AuthDesc;
|
||||
|
||||
import java.sql.*;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.DriverPropertyInfo;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
||||
import com.ai.cloud.skywalking.conf.AuthDesc;
|
||||
|
||||
public class TracingDriver implements Driver {
|
||||
private static org.apache.logging.log4j.Logger logger = LogManager.getLogger(TracingDriver.class);
|
||||
|
||||
private static final String TRACING_SIGN = "tracing:";
|
||||
private static final String TRACING_SIGN = "tracing:";
|
||||
|
||||
private static boolean isOpenCompensation = false;
|
||||
private static boolean isOpenCompensation = false;
|
||||
|
||||
public static final void registerDriver() {
|
||||
public static final void registerDriver() {
|
||||
try {
|
||||
DriverManager.registerDriver(new TracingDriver());
|
||||
isOpenCompensation = true;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("register "
|
||||
+ TracingDriver.class.getName() + " driver failure.");
|
||||
logger.error("register TracingDriver failure.", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Driver realDriver;
|
||||
private Driver realDriver;
|
||||
|
||||
private Driver chooseDriver(String url) throws IllegalAccessException,
|
||||
InstantiationException, ClassNotFoundException, SQLException {
|
||||
if (realDriver == null) {
|
||||
this.realDriver = DriverChooser.choose(url);
|
||||
}
|
||||
return realDriver;
|
||||
}
|
||||
private Driver chooseDriver(String url) throws IllegalAccessException,
|
||||
InstantiationException, ClassNotFoundException, SQLException {
|
||||
if (realDriver == null) {
|
||||
this.realDriver = DriverChooser.choose(url);
|
||||
}
|
||||
return realDriver;
|
||||
}
|
||||
|
||||
private String getRealUrl(String url) throws SQLException {
|
||||
if (!isOpenCompensation) {
|
||||
return url;
|
||||
} else {
|
||||
if (url.toLowerCase().startsWith(TRACING_SIGN)) {
|
||||
return url.substring(TRACING_SIGN.length());
|
||||
} else {
|
||||
throw new SQLException("tracing jdbc url must start with 'tracing:'");
|
||||
}
|
||||
}
|
||||
}
|
||||
private String getRealUrl(String url) throws SQLException {
|
||||
if (!isOpenCompensation) {
|
||||
return url;
|
||||
} else {
|
||||
if (url.toLowerCase().startsWith(TRACING_SIGN)) {
|
||||
return url.substring(TRACING_SIGN.length());
|
||||
} else {
|
||||
throw new SQLException(
|
||||
"tracing jdbc url must start with 'tracing:'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public java.sql.Connection connect(String url, Properties info) throws SQLException {
|
||||
java.sql.Connection conn = null;
|
||||
public java.sql.Connection connect(String url, Properties info)
|
||||
throws SQLException {
|
||||
java.sql.Connection conn = null;
|
||||
|
||||
try {
|
||||
conn = chooseDriver(getRealUrl(url)).connect(getRealUrl(url), info);
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
try {
|
||||
conn = chooseDriver(getRealUrl(url)).connect(getRealUrl(url), info);
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
|
||||
if (!AuthDesc.isAuth()) {
|
||||
return conn;
|
||||
} else {
|
||||
return new SWConnection(getRealUrl(url), info, conn);
|
||||
}
|
||||
}
|
||||
if (!AuthDesc.isAuth()) {
|
||||
return conn;
|
||||
} else {
|
||||
return new SWConnection(getRealUrl(url), info, conn);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean acceptsURL(String url) throws SQLException {
|
||||
Driver driver = null;
|
||||
try {
|
||||
driver = chooseDriver(getRealUrl(url));
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return driver.acceptsURL(getRealUrl(url));
|
||||
}
|
||||
public boolean acceptsURL(String url) throws SQLException {
|
||||
Driver driver = null;
|
||||
try {
|
||||
driver = chooseDriver(getRealUrl(url));
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return driver.acceptsURL(getRealUrl(url));
|
||||
}
|
||||
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
|
||||
throws SQLException {
|
||||
Driver driver = null;
|
||||
try {
|
||||
driver = chooseDriver(getRealUrl(url));
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return driver.getPropertyInfo(getRealUrl(url), info);
|
||||
}
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
|
||||
throws SQLException {
|
||||
Driver driver = null;
|
||||
try {
|
||||
driver = chooseDriver(getRealUrl(url));
|
||||
} catch (Exception e) {
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return driver.getPropertyInfo(getRealUrl(url), info);
|
||||
}
|
||||
|
||||
public int getMajorVersion() {
|
||||
return safeIntParse("1");
|
||||
}
|
||||
public int getMajorVersion() {
|
||||
return safeIntParse("1");
|
||||
}
|
||||
|
||||
public int getMinorVersion() {
|
||||
return safeIntParse("0");
|
||||
}
|
||||
public int getMinorVersion() {
|
||||
return safeIntParse("0");
|
||||
}
|
||||
|
||||
public boolean jdbcCompliant() {
|
||||
return false;
|
||||
}
|
||||
public boolean jdbcCompliant() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static int safeIntParse(String intAsString) {
|
||||
try {
|
||||
return Integer.parseInt(intAsString);
|
||||
} catch (NumberFormatException nfe) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
private static int safeIntParse(String intAsString) {
|
||||
try {
|
||||
return Integer.parseInt(intAsString);
|
||||
} catch (NumberFormatException nfe) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.ai.cloud.skywalking.jedis.v2.plugin;
|
|||
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
|
||||
import java.util.Set;
|
||||
|
|
@ -15,7 +16,8 @@ public class JedisClusterInterceptor extends JedisBaseInterceptor {
|
|||
StringBuilder redisConnInfo = new StringBuilder();
|
||||
if (interceptorContext.allArguments().length > 0) {
|
||||
if (interceptorContext.allArguments()[0] instanceof Set) {
|
||||
Set<HostAndPort> hostAndPorts = (Set<HostAndPort>) interceptorContext.allArguments()[0];
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<HostAndPort> hostAndPorts = (Set<HostAndPort>) interceptorContext.allArguments()[0];
|
||||
for (HostAndPort hostAndPort : hostAndPorts) {
|
||||
redisConnInfo.append(hostAndPort.toString()).append(";");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package com.ai.cloud.skywalking.jedis.v2.plugin;
|
||||
|
||||
import com.ai.cloud.skywalking.buriedpoint.RPCBuriedPointSender;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
import java.net.URI;
|
||||
|
||||
import redis.clients.jedis.JedisShardInfo;
|
||||
|
||||
import java.net.URI;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.ConstructorInvokeContext;
|
||||
import com.ai.cloud.skywalking.plugin.interceptor.EnhancedClassInstanceContext;
|
||||
|
||||
public class JedisInterceptor extends JedisBaseInterceptor {
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue