Skip to content

Commit b5bbe32

Browse files
authored
Merge pull request #1431 from hanbingleixue/cooperate-with-gala-gopher-alpha
修复功能测试出现的问题
2 parents 626c1cb + 1cbd692 commit b5bbe32

File tree

33 files changed

+814
-134
lines changed

33 files changed

+814
-134
lines changed

sermant-plugins/sermant-metrics/metrics-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<properties>
1818
<maven.compiler.source>8</maven.compiler.source>
1919
<maven.compiler.target>8</maven.compiler.target>
20-
<jsqlparser.version>4.4</jsqlparser.version>
20+
<jsqlparser.version>4.8</jsqlparser.version>
2121
</properties>
2222
<dependencies>
2323
<!--provided依赖-->

sermant-plugins/sermant-metrics/metrics-common/src/main/java/com/huawei/metrics/common/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public class Constants {
128128
*/
129129
public static final int MAX_SUCCESS_CODE = 399;
130130

131+
/**
132+
* 默认的HTTP失败编码
133+
*/
134+
public static final int HTTP_DEFAULT_FAILURE_CODE = 9999;
135+
131136
/**
132137
* 最大客户端失败编码
133138
*/

sermant-plugins/sermant-metrics/metrics-common/src/main/java/com/huawei/metrics/interceptor/AbstractMysqlInterceptor.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import com.huawei.metrics.util.ResultJudgmentUtil;
2424
import com.huawei.metrics.util.SqlParseUtil;
2525

26+
import com.huaweicloud.sermant.core.common.LoggerFactory;
2627
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
2728
import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor;
2829

2930
import java.sql.SQLException;
31+
import java.util.logging.Logger;
3032

3133
/**
3234
* MYSQL拦截器父类
@@ -35,6 +37,11 @@
3537
* @since 2024-01-15
3638
*/
3739
public abstract class AbstractMysqlInterceptor implements Interceptor {
40+
/**
41+
* 日志
42+
*/
43+
public static final Logger LOGGER = LoggerFactory.getLogger();
44+
3845
@Override
3946
public ExecuteContext after(ExecuteContext context) {
4047
return collectMetrics(context);
@@ -58,16 +65,16 @@ public ExecuteContext onThrow(ExecuteContext context) {
5865
*
5966
* @param context 上下文信息
6067
* @param enableSsl 是否开启SSL
61-
* @param serverIp 服务端IP
68+
* @param host 服务端域名或者IP
6269
* @param serverPort 服务端端口
6370
* @param sql 执行的SQL
6471
* @return 指标信息
6572
*/
66-
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String serverIp,
73+
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String host,
6774
int serverPort, String sql) {
6875
MetricsRpcInfo metricsRpcInfo = new MetricsRpcInfo();
6976
metricsRpcInfo.setClientIp(InetAddressUtil.getHostAddress());
70-
metricsRpcInfo.setServerIp(serverIp);
77+
metricsRpcInfo.setServerIp(InetAddressUtil.getHostAddress(host));
7178
metricsRpcInfo.setServerPort(serverPort);
7279
metricsRpcInfo.setProtocol(Constants.MYSQL_PROTOCOL);
7380
metricsRpcInfo.setEnableSsl(enableSsl);

sermant-plugins/sermant-metrics/metrics-common/src/main/java/com/huawei/metrics/util/InetAddressUtil.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.huaweicloud.sermant.core.common.LoggerFactory;
2020
import com.huaweicloud.sermant.core.utils.StringUtils;
2121

22+
import java.net.Inet4Address;
2223
import java.net.InetAddress;
2324
import java.net.NetworkInterface;
2425
import java.net.SocketException;
@@ -61,7 +62,8 @@ public static String getHostAddress() {
6162
for (Enumeration<InetAddress> inetAdd = ni.getInetAddresses();
6263
inetAdd.hasMoreElements(); ) {
6364
InetAddress inetAddress = inetAdd.nextElement();
64-
if (!inetAddress.isLoopbackAddress() && inetAddress.isSiteLocalAddress()) {
65+
if (!inetAddress.isLoopbackAddress() && !inetAddress.isSiteLocalAddress()
66+
&& inetAddress instanceof Inet4Address) {
6567
hostAddress = inetAddress.getHostAddress();
6668
return hostAddress;
6769
}
@@ -75,4 +77,24 @@ public static String getHostAddress() {
7577
}
7678
return hostAddress;
7779
}
80+
81+
/**
82+
* 获取host对应的IP地址
83+
*
84+
* @param host 域名
85+
* @return IP地址
86+
*/
87+
public static String getHostAddress(String host) {
88+
try {
89+
InetAddress inetAddress = InetAddress.getByName(host);
90+
String address = inetAddress.getHostAddress();
91+
if (StringUtils.equals(address, "127.0.0.1")) {
92+
return getHostAddress();
93+
}
94+
return address;
95+
} catch (UnknownHostException e) {
96+
LOGGER.log(Level.SEVERE, "Unable to resolve domain name to IP.");
97+
return StringUtils.EMPTY;
98+
}
99+
}
78100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.huawei.metrics.util;
18+
19+
import com.huaweicloud.sermant.core.utils.StringUtils;
20+
21+
import net.sf.jsqlparser.schema.Table;
22+
import net.sf.jsqlparser.statement.alter.Alter;
23+
import net.sf.jsqlparser.statement.create.index.CreateIndex;
24+
import net.sf.jsqlparser.statement.drop.Drop;
25+
import net.sf.jsqlparser.util.TablesNamesFinder;
26+
27+
/**
28+
* 表名获取工具类
29+
*
30+
* @author zhp
31+
* @since 2024-01-15
32+
*/
33+
public class MysqlTablesNameFinder extends TablesNamesFinder {
34+
private static final String INDEX_TYPE = "INDEX";
35+
36+
@Override
37+
public void visit(Drop drop) {
38+
Table table = drop.getName();
39+
if (StringUtils.equals(drop.getType(), INDEX_TYPE) || table == null
40+
|| StringUtils.isEmpty(table.getName())) {
41+
return;
42+
}
43+
visit(drop.getName());
44+
}
45+
46+
@Override
47+
public void visit(CreateIndex createIndex) {
48+
visit(createIndex.getTable());
49+
}
50+
51+
@Override
52+
public void visit(Alter alter) {
53+
visit(alter.getTable());
54+
}
55+
}

sermant-plugins/sermant-metrics/metrics-common/src/main/java/com/huawei/metrics/util/SqlParseUtil.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import net.sf.jsqlparser.JSQLParserException;
2525
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
2626
import net.sf.jsqlparser.statement.Statement;
27-
import net.sf.jsqlparser.util.TablesNamesFinder;
2827

29-
import java.util.List;
28+
import java.util.Locale;
29+
import java.util.Set;
3030
import java.util.logging.Level;
3131
import java.util.logging.Logger;
3232

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

42+
private static final String STATEMENT_CLASS_NAME_SUFFIX = "Statement";
43+
44+
private static final String STATEMENT_CLASS_NAME_PREFIX = "Plain";
45+
4246
private SqlParseUtil() {
4347
}
4448

4549
/**
46-
* 获取mysql的API维度信息(命令类型_表名
50+
* 获取mysql的API维度信息(命令类型_表名或命令类型
4751
*
4852
* @param sql 执行的SQL语句
4953
* @return API
@@ -52,12 +56,16 @@ public static String getApi(String sql) {
5256
StringBuilder stringBuilder = new StringBuilder();
5357
try {
5458
Statement statement = CCJSqlParserUtil.parse(sql);
55-
stringBuilder.append(statement.getClass().getSimpleName().replace("Statement", StringUtils.EMPTY));
56-
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
57-
List<String> tables = tablesNamesFinder.getTableList(statement);
59+
String className = statement.getClass().getSimpleName();
60+
String commandType = className.replace(STATEMENT_CLASS_NAME_PREFIX, StringUtils.EMPTY)
61+
.replace(STATEMENT_CLASS_NAME_SUFFIX, StringUtils.EMPTY)
62+
.toLowerCase(Locale.ROOT);
63+
stringBuilder.append(commandType);
64+
MysqlTablesNameFinder tablesNamesFinder = new MysqlTablesNameFinder();
65+
Set<String> tables = tablesNamesFinder.getTables(statement);
5866
if (tables != null) {
5967
for (String table : tables) {
60-
stringBuilder.append(Constants.CONNECT).append(table);
68+
stringBuilder.append(Constants.CONNECT).append(table.toLowerCase(Locale.ROOT));
6169
}
6270
}
6371
return stringBuilder.toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.huawei.metrics.declarer;
18+
19+
import com.huawei.metrics.interceptor.ExecutePreparedInterceptor;
20+
21+
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
22+
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
23+
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
24+
25+
/**
26+
* mariadb2.x sql执行方法拦截声明
27+
*
28+
* @author zhp
29+
* @since 2024-01-15
30+
*/
31+
public class ExecutePreparedDeclarer extends AbstractDeclarer {
32+
private static final String ENHANCE_CLASS = "org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol";
33+
34+
@Override
35+
public ClassMatcher getClassMatcher() {
36+
return ClassMatcher.nameEquals(ENHANCE_CLASS);
37+
}
38+
39+
/**
40+
* 获取插件的拦截声明 {@link org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol#prepare(String, boolean)}
41+
*
42+
* @param classLoader 被增强类的类加载器
43+
* @return 拦截声明集
44+
*/
45+
@Override
46+
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
47+
return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals("prepare"),
48+
new ExecutePreparedInterceptor())
49+
};
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.huawei.metrics.declarer;
18+
19+
import com.huawei.metrics.interceptor.ExecutePreparedQueryInterceptor;
20+
21+
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
22+
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
23+
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;
24+
25+
/**
26+
* mariadb2.x sql执行方法拦截声明
27+
*
28+
* @author zhp
29+
* @since 2024-01-15
30+
*/
31+
public class ExecutePreparedQueryDeclarer extends AbstractDeclarer {
32+
private static final String ENHANCE_CLASS = "org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol";
33+
34+
@Override
35+
public ClassMatcher getClassMatcher() {
36+
return ClassMatcher.nameEquals(ENHANCE_CLASS);
37+
}
38+
39+
/**
40+
* 获取插件的拦截声明
41+
* {@link org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol#executePreparedQuery (boolean,
42+
* ServerPrepareResult, Results, ParameterHolder[])}
43+
*
44+
* @param classLoader 被增强类的类加载器
45+
* @return 拦截声明集
46+
*/
47+
@Override
48+
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
49+
return new InterceptDeclarer[]{
50+
InterceptDeclarer.build(MethodMatcher.nameEquals("executePreparedQuery"),
51+
new ExecutePreparedQueryInterceptor())
52+
};
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.huawei.metrics.interceptor;
18+
19+
import com.huawei.metrics.common.Constants;
20+
import com.huawei.metrics.entity.MetricsRpcInfo;
21+
22+
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
23+
24+
import org.mariadb.jdbc.internal.protocol.AbstractQueryProtocol;
25+
26+
/**
27+
* Mariadb2.x SQL执行增强器
28+
*
29+
* @author zhp
30+
* @since 2024-01-15
31+
*/
32+
public abstract class AbstractQueryProtocolInterceptor extends AbstractMysqlInterceptor {
33+
@Override
34+
public ExecuteContext before(ExecuteContext context) {
35+
context.setLocalFieldValue(Constants.START_TIME_KEY, System.nanoTime());
36+
return context;
37+
}
38+
39+
/**
40+
* 创建RPC实体类
41+
*
42+
* @param sql SQL信息
43+
* @param context 上下文信息
44+
* @param latency 时延
45+
* @return 指标数据
46+
*/
47+
public MetricsRpcInfo createRpcInfo(String sql, ExecuteContext context, long latency) {
48+
boolean enableSsl = false;
49+
AbstractQueryProtocol protocol = (AbstractQueryProtocol) context.getObject();
50+
if (protocol.getUrlParser() != null && protocol.getUrlParser() != null
51+
&& protocol.getUrlParser().getOptions() != null) {
52+
enableSsl = protocol.getUrlParser().getOptions().useSsl;
53+
}
54+
MetricsRpcInfo metricsRpcInfo = initMetricsRpcInfo(context, enableSsl, protocol.getHost(), protocol.getPort(),
55+
sql);
56+
metricsRpcInfo.getSumLatency().getAndAdd(latency);
57+
metricsRpcInfo.getLatencyList().add(latency);
58+
return metricsRpcInfo;
59+
}
60+
}

0 commit comments

Comments
 (0)