-
Notifications
You must be signed in to change notification settings - Fork 218
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
jerry-ye-20230331
wants to merge
1
commit into
chaosblade-io:master
Choose a base branch
from
jerry-ye-20230331:chaos-jerry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -27,12 +28,16 @@ public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, O | |
} | ||
String serviceName = getServiceName(object); | ||
String template = getTemplate(methodArguments); | ||
String url = getUrl(methodArguments); | ||
String templateUrl= StringUtils.isNotEmpty(template)? template:url; | ||
LOGGER.info("feign serviceName:{},template:{},url:{}", serviceName, template,url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
@@ -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; | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...plugin-mysql/src/main/java/com/alibaba/chaosblade/exec/plugin/mysql/MysqlAwsEnhancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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