Skip to content

Commit

Permalink
修改功能测试出现的问题
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed Jan 31, 2024
1 parent 177a697 commit a2c47bc
Show file tree
Hide file tree
Showing 33 changed files with 814 additions and 132 deletions.
2 changes: 1 addition & 1 deletion sermant-plugins/sermant-metrics/metrics-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jsqlparser.version>4.4</jsqlparser.version>
<jsqlparser.version>4.8</jsqlparser.version>
</properties>
<dependencies>
<!--provided依赖-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public class Constants {
*/
public static final int MAX_SUCCESS_CODE = 399;

/**
* 默认的HTTP失败编码
*/
public static final int HTTP_DEFAULT_FAILURE_CODE = 9999;

/**
* 最大客户端失败编码
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import com.huawei.metrics.util.ResultJudgmentUtil;
import com.huawei.metrics.util.SqlParseUtil;

import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor;

import java.sql.SQLException;
import java.util.logging.Logger;

/**
* MYSQL拦截器父类
Expand All @@ -35,6 +37,11 @@
* @since 2024-01-15
*/
public abstract class AbstractMysqlInterceptor implements Interceptor {
/**
* 日志
*/
public static final Logger LOGGER = LoggerFactory.getLogger();

@Override
public ExecuteContext after(ExecuteContext context) {
return collectMetrics(context);
Expand All @@ -58,16 +65,16 @@ public ExecuteContext onThrow(ExecuteContext context) {
*
* @param context 上下文信息
* @param enableSsl 是否开启SSL
* @param serverIp 服务端IP
* @param host 服务端域名或者IP
* @param serverPort 服务端端口
* @param sql 执行的SQL
* @return 指标信息
*/
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String serverIp,
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String host,
int serverPort, String sql) {
MetricsRpcInfo metricsRpcInfo = new MetricsRpcInfo();
metricsRpcInfo.setClientIp(InetAddressUtil.getHostAddress());
metricsRpcInfo.setServerIp(serverIp);
metricsRpcInfo.setServerIp(InetAddressUtil.getHostAddress(host));
metricsRpcInfo.setServerPort(serverPort);
metricsRpcInfo.setProtocol(Constants.MYSQL_PROTOCOL);
metricsRpcInfo.setEnableSsl(enableSsl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.huaweicloud.sermant.core.common.LoggerFactory;
import com.huaweicloud.sermant.core.utils.StringUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
Expand Down Expand Up @@ -61,7 +62,8 @@ public static String getHostAddress() {
for (Enumeration<InetAddress> inetAdd = ni.getInetAddresses();
inetAdd.hasMoreElements(); ) {
InetAddress inetAddress = inetAdd.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
if (!inetAddress.isLoopbackAddress() && !inetAddress.isSiteLocalAddress()
&& inetAddress instanceof Inet4Address) {
hostAddress = inetAddress.getHostAddress();
return hostAddress;
}
Expand All @@ -75,4 +77,24 @@ public static String getHostAddress() {
}
return hostAddress;
}

/**
* 获取host对应的IP地址
*
* @param host 域名
* @return IP地址
*/
public static String getHostAddress(String host) {
try {
InetAddress inetAddress = InetAddress.getByName(host);
String address = inetAddress.getHostAddress();
if (StringUtils.equals(address, "127.0.0.1")) {
return getHostAddress();
}
return address;
} catch (UnknownHostException e) {
LOGGER.log(Level.SEVERE, "Unable to resolve domain name to IP.");
return StringUtils.EMPTY;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* 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.huawei.metrics.util;

import com.huaweicloud.sermant.core.utils.StringUtils;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.jsqlparser.statement.create.index.CreateIndex;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.jsqlparser.util.TablesNamesFinder;

/**
* 表名获取工具类
*
* @author zhp
* @since 2024-01-15
*/
public class MysqlTablesNameFinder extends TablesNamesFinder {
private static final String INDEX_TYPE = "INDEX";

@Override
public void visit(Drop drop) {
Table table = drop.getName();
if (StringUtils.equals(drop.getType(), INDEX_TYPE) || table == null
|| StringUtils.isEmpty(table.getName())) {
return;
}
visit(drop.getName());
}

@Override
public void visit(CreateIndex createIndex) {
visit(createIndex.getTable());
}

@Override
public void visit(Alter alter) {
visit(alter.getTable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.util.TablesNamesFinder;

import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -39,11 +39,15 @@
public class SqlParseUtil {
private static final Logger LOGGER = LoggerFactory.getLogger();

private static final String STATEMENT_CLASS_NAME_SUFFIX = "Statement";

private static final String STATEMENT_CLASS_NAME_PREFIX = "Plain";

private SqlParseUtil() {
}

/**
* 获取mysql的API维度信息(命令类型_表名
* 获取mysql的API维度信息(命令类型_表名或命令类型
*
* @param sql 执行的SQL语句
* @return API
Expand All @@ -52,12 +56,16 @@ public static String getApi(String sql) {
StringBuilder stringBuilder = new StringBuilder();
try {
Statement statement = CCJSqlParserUtil.parse(sql);
stringBuilder.append(statement.getClass().getSimpleName().replace("Statement", StringUtils.EMPTY));
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tables = tablesNamesFinder.getTableList(statement);
String className = statement.getClass().getSimpleName();
String commandType = className.replace(STATEMENT_CLASS_NAME_PREFIX, StringUtils.EMPTY)
.replace(STATEMENT_CLASS_NAME_SUFFIX, StringUtils.EMPTY)
.toLowerCase(Locale.ROOT);
stringBuilder.append(commandType);
MysqlTablesNameFinder tablesNamesFinder = new MysqlTablesNameFinder();
Set<String> tables = tablesNamesFinder.getTables(statement);
if (tables != null) {
for (String table : tables) {
stringBuilder.append(Constants.CONNECT).append(table);
stringBuilder.append(Constants.CONNECT).append(table.toLowerCase(Locale.ROOT));
}
}
return stringBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* 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.huawei.metrics.declarer;

import com.huawei.metrics.interceptor.ExecutePreparedInterceptor;

import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;

/**
* mariadb2.x sql执行方法拦截声明
*
* @author zhp
* @since 2024-01-15
*/
public class ExecutePreparedDeclarer extends AbstractDeclarer {
private static final String ENHANCE_CLASS = "org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol";

@Override
public ClassMatcher getClassMatcher() {
return ClassMatcher.nameEquals(ENHANCE_CLASS);
}

/**
* 获取插件的拦截声明 {@link org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol#prepare(String, boolean)}
*
* @param classLoader 被增强类的类加载器
* @return 拦截声明集
*/
@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals("prepare"),
new ExecutePreparedInterceptor())
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* 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.huawei.metrics.declarer;

import com.huawei.metrics.interceptor.ExecutePreparedQueryInterceptor;

import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;

/**
* mariadb2.x sql执行方法拦截声明
*
* @author zhp
* @since 2024-01-15
*/
public class ExecutePreparedQueryDeclarer extends AbstractDeclarer {
private static final String ENHANCE_CLASS = "org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol";

@Override
public ClassMatcher getClassMatcher() {
return ClassMatcher.nameEquals(ENHANCE_CLASS);
}

/**
* 获取插件的拦截声明
* {@link org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol#executePreparedQuery (boolean,
* ServerPrepareResult, Results, ParameterHolder[])}
*
* @param classLoader 被增强类的类加载器
* @return 拦截声明集
*/
@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[]{
InterceptDeclarer.build(MethodMatcher.nameEquals("executePreparedQuery"),
new ExecutePreparedQueryInterceptor())
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* 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.huawei.metrics.interceptor;

import com.huawei.metrics.common.Constants;
import com.huawei.metrics.entity.MetricsRpcInfo;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;

import org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol;

/**
* Mariadb2.x SQL执行增强器
*
* @author zhp
* @since 2024-01-15
*/
public abstract class AbstractQueryProtocolInterceptor extends AbstractMysqlInterceptor {
@Override
public ExecuteContext before(ExecuteContext context) {
context.setLocalFieldValue(Constants.START_TIME_KEY, System.nanoTime());
return context;
}

/**
* 创建RPC实体类
*
* @param sql SQL信息
* @param context 上下文信息
* @param latency 时延
* @return 指标数据
*/
public MetricsRpcInfo createRpcInfo(String sql, ExecuteContext context, long latency) {
boolean enableSsl = false;
AbstractQueryProtocol protocol = (AbstractQueryProtocol) context.getObject();
if (protocol.getUrlParser() != null && protocol.getUrlParser() != null
&& protocol.getUrlParser().getOptions() != null) {
enableSsl = protocol.getUrlParser().getOptions().useSsl;
}
MetricsRpcInfo metricsRpcInfo = initMetricsRpcInfo(context, enableSsl, protocol.getHost(), protocol.getPort(),
sql);
metricsRpcInfo.getSumLatency().getAndAdd(latency);
metricsRpcInfo.getLatencyList().add(latency);
return metricsRpcInfo;
}
}
Loading

0 comments on commit a2c47bc

Please sign in to comment.