Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended support for Aws Mysql and Feign clients #313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;

import com.alibaba.chaosblade.exec.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,12 +28,16 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O
}
String serviceName = getServiceName(object);
String template = getTemplate(methodArguments);
String url = getUrl(methodArguments);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obtain the url when the template is empty. do not need to obtain the url in advance

String templateUrl= StringUtils.isNotEmpty(template)? template:url;
LOGGER.info("feign serviceName:{},template:{},url:{}", serviceName, template,url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log level can be changed to debug


if (LOGGER.isDebugEnabled()) {
LOGGER.info("feign : serviceName:{},template:{}", serviceName, template);
}
MatcherModel matcherModel = new MatcherModel();
matcherModel.add(SERVICE_NAME, serviceName);
matcherModel.add(TEMPLATE_URL, template);
matcherModel.add(TEMPLATE_URL, templateUrl);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("feign template url: {}", template);
}
Expand All @@ -50,5 +55,12 @@ private String getTemplate(Object[] methodArguments) throws Exception {
Object urlBuilderObj = ReflectUtil.getFieldValue(requestTemplateObj, "url", false);
return ReflectUtil.invokeMethod(urlBuilderObj, "toString");
}
private String getUrl(Object[] methodArguments) throws Exception {
//spring-cloud-openfeign 3.0 ,feign-core 10.12
Object requestTemplateObj = methodArguments[0];
Object urlBuilderObj = ReflectUtil.getSuperclassFieldValue(requestTemplateObj, "uriTemplate", false);
String url = ReflectUtil.getSuperclassFieldValue(urlBuilderObj, "template", false);
return url;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.chaosblade.exec.plugin.mysql;

import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.JsonUtil;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;
import com.alibaba.chaosblade.exec.common.util.SQLParserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
* @author yefei
* @create 2020-11-19 16:14
*/
public class MysqlAwsEnhancer extends BeforeEnhancer {

private static final Logger logger = LoggerFactory.getLogger(MysqlAwsEnhancer.class);

@Override
public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, Object object, Method method, Object[] methodArguments) throws Exception {
if (methodArguments == null || object == null) {
logger.warn("The necessary parameters is");
return null;
}

Object callingQuery = methodArguments[0];
Object connection = ReflectUtil.getSuperclassFieldValue(callingQuery, "connection", false);
String sql = String.valueOf(methodArguments[1]);

if (ReflectUtil.isAssignableFrom(classLoader, callingQuery.getClass(), "com.mysql.cj.jdbc.JdbcPreparedStatement")) {
sql = ReflectUtil.invokeMethod(callingQuery, "getPreparedSql", new Object[0], false);
}

String host = ReflectUtil.getFieldValue(connection, "origHostToConnectTo", false);
Integer port = ReflectUtil.getFieldValue(connection, "origPortToConnectTo", false);
String database = ReflectUtil.getFieldValue(connection, "database", false);;

String table = SQLParserUtil.findTableName(sql);
if (database == null && table != null) {
String[] dbAndTable = table.split(".");
if (dbAndTable.length == 2) {
database = dbAndTable[0];
}
}

SQLParserUtil.SqlType type = SQLParserUtil.getSqlType(sql);

MatcherModel matcherModel = new MatcherModel();
matcherModel.add(MysqlConstant.HOST_MATCHER_NAME, host);
matcherModel.add(MysqlConstant.TABLE_MATCHER_NAME, table);
matcherModel.add(MysqlConstant.DATABASE_MATCHER_NAME, database);
if (type != null) {
matcherModel.add(MysqlConstant.SQL_TYPE_MATCHER_NAME, type.name());
}
if (port != null) {
matcherModel.add(MysqlConstant.PORT_MATCHER_NAME, port.toString());
}
if (logger.isDebugEnabled()) {
logger.debug("mysql matchers: {}", JsonUtil.writer().writeValueAsString(matcherModel));
}
return new EnhancerModel(classLoader, matcherModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface MysqlConstant {
String MYSQL8_NATIVE_SESSION_METHOD = "execSQL";


String AWS_MYSQL_NATIVE_SESSION_CLASS = "software.aws.rds.jdbc.mysql.shading.com.mysql.cj.NativeSession";
String AWS_MYSQL_NATIVE_SESSION_METHOD = "execSQL";

String IO_SHARDING_STATEMENT_EXECUTOR_CLASS = "io.shardingsphere.shardingjdbc.executor.AbstractStatementExecutor";
String IO_SHARDING_STATEMENT_EXECUTOR_METHOD = "executeCallback";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class MysqlEnhancer extends BeforeEnhancer {

private final Mysql8Enhancer mysql8Enhancer = new Mysql8Enhancer();

private final MysqlAwsEnhancer mysqlAwsEnhancer = new MysqlAwsEnhancer();

private final IoShardingJdbcEnhancer ioShardingJdbcEnhancer = new IoShardingJdbcEnhancer();

private final ApacheShardingJdbcEnhancer apacheShardingJdbcEnhancer = new ApacheShardingJdbcEnhancer();
Expand All @@ -48,6 +50,8 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O

if (MYSQL_IO_CLASS.equals(className)) {
return mysql5Enhancer.doBeforeAdvice(classLoader, className, object, method, methodArguments);
}else if(AWS_MYSQL_NATIVE_SESSION_CLASS.equals(className)) {
return mysqlAwsEnhancer.doBeforeAdvice(classLoader, className, object, method, methodArguments);
}else if(IO_SHARDING_STATEMENT_EXECUTOR_CLASS.equals(className)) {
return ioShardingJdbcEnhancer.doBeforeAdvice(classLoader, className, object, method, methodArguments);
}else if(APACHE_SHARDING_EXECUTOR_ENGINE_CLASS.equals(className)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public ClassMatcher getClassMatcher() {
return orClassMatcher
.or(new NameClassMatcher(MYSQL_IO_CLASS))
.or(new NameClassMatcher(MYSQL8_NATIVE_SESSION_CLASS))
.or(new NameClassMatcher(AWS_MYSQL_NATIVE_SESSION_CLASS))
.or(new NameClassMatcher(IO_SHARDING_STATEMENT_EXECUTOR_CLASS))
.or(new NameClassMatcher(APACHE_SHARDING_EXECUTOR_ENGINE_CLASS));
}
Expand All @@ -48,6 +49,7 @@ public MethodMatcher getMethodMatcher() {
return orMethodMatcher
.or(new NameMethodMatcher(INTERCEPTOR_PRE_METHOD))
.or(new NameMethodMatcher(MYSQL8_NATIVE_SESSION_METHOD))
.or(new NameMethodMatcher(AWS_MYSQL_NATIVE_SESSION_METHOD))
.or(new NameMethodMatcher(IO_SHARDING_STATEMENT_EXECUTOR_METHOD))
.or(new NameMethodMatcher(APACHE_SHARDING_EXECUTOR_ENGINE_METHOD));
}
Expand Down