forked from sermant-io/Sermant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hanbingleixue <[email protected]>
- Loading branch information
1 parent
c83aa32
commit 149b0af
Showing
39 changed files
with
1,171 additions
and
66 deletions.
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<artifactId>sermant-metrics</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>metrics-common</artifactId> | ||
|
||
<name>metrics-common</name> | ||
<description>The common module of metrics.</description> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
<jsqlparser.version>4.4</jsqlparser.version> | ||
</properties> | ||
<dependencies> | ||
<!--provided依赖--> | ||
<dependency> | ||
<groupId>com.huaweicloud.sermant</groupId> | ||
<artifactId>sermant-agentcore-core</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!--SQL解析第三方依赖--> | ||
<dependency> | ||
<groupId>com.github.jsqlparser</groupId> | ||
<artifactId>jsqlparser</artifactId> | ||
<version>${jsqlparser.version}</version> | ||
</dependency> | ||
<!--test依赖--> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-inline</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>${mockito-core.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
95 changes: 95 additions & 0 deletions
95
...metrics-common/src/main/java/com/huawei/metrics/interceptor/AbstractMysqlInterceptor.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,95 @@ | ||
/* | ||
* Copyright (C) 2023-2023 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.common.ResultType; | ||
import com.huawei.metrics.entity.MetricsRpcInfo; | ||
import com.huawei.metrics.util.InetAddressUtil; | ||
import com.huawei.metrics.util.ResultJudgmentUtil; | ||
import com.huawei.metrics.util.SqlParseUtil; | ||
|
||
import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; | ||
import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; | ||
|
||
import java.sql.SQLException; | ||
|
||
/** | ||
* MYSQL拦截器父类 | ||
* | ||
* @author zhp | ||
* @since 2023-12-15 | ||
*/ | ||
public abstract class AbstractMysqlInterceptor implements Interceptor { | ||
@Override | ||
public ExecuteContext after(ExecuteContext context) { | ||
return collectMetrics(context); | ||
} | ||
|
||
/** | ||
* 采集指标信息 | ||
* | ||
* @param context 上下文信息 | ||
* @return ExecuteContext 上下文信息 | ||
*/ | ||
public abstract ExecuteContext collectMetrics(ExecuteContext context); | ||
|
||
@Override | ||
public ExecuteContext onThrow(ExecuteContext context) { | ||
return collectMetrics(context); | ||
} | ||
|
||
/** | ||
* 初始化指标数据 | ||
* | ||
* @param context 上下文信息 | ||
* @param enableSsl 是否开启SSL | ||
* @param serverIp 服务端IP | ||
* @param serverPort 服务端端口 | ||
* @param sql 执行的SQL | ||
* @return 指标信息 | ||
*/ | ||
public MetricsRpcInfo initMetricsRpcInfo(ExecuteContext context, boolean enableSsl, String serverIp, | ||
int serverPort, String sql) { | ||
MetricsRpcInfo metricsRpcInfo = new MetricsRpcInfo(); | ||
metricsRpcInfo.setClientIp(InetAddressUtil.getHostAddress()); | ||
metricsRpcInfo.setServerIp(serverIp); | ||
metricsRpcInfo.setServerPort(serverPort); | ||
metricsRpcInfo.setProtocol(Constants.MYSQL_PROTOCOL); | ||
metricsRpcInfo.setEnableSsl(enableSsl); | ||
metricsRpcInfo.setL7Role(Constants.CLIENT_ROLE); | ||
metricsRpcInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + metricsRpcInfo.getL7Role()); | ||
metricsRpcInfo.getReqCount().getAndIncrement(); | ||
metricsRpcInfo.getResponseCount().getAndIncrement(); | ||
metricsRpcInfo.setUrl(SqlParseUtil.getApi(sql)); | ||
if (context.getThrowable() == null) { | ||
return metricsRpcInfo; | ||
} | ||
metricsRpcInfo.getReqErrorCount().getAndIncrement(); | ||
if (!(context.getThrowable() instanceof SQLException)) { | ||
return metricsRpcInfo; | ||
} | ||
SQLException exception = (SQLException) context.getThrowable(); | ||
int value = ResultJudgmentUtil.judgeMysqlResult(exception.getErrorCode()); | ||
if (ResultType.CLIENT_ERROR.getValue() == value) { | ||
metricsRpcInfo.getClientErrorCount().getAndIncrement(); | ||
} else if (ResultType.SERVER_ERROR.getValue() == value) { | ||
metricsRpcInfo.getServerErrorCount().getAndIncrement(); | ||
} | ||
return metricsRpcInfo; | ||
} | ||
} |
File renamed without changes.
58 changes: 58 additions & 0 deletions
58
...sermant-metrics/metrics-common/src/main/java/com/huawei/metrics/util/InetAddressUtil.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,58 @@ | ||
/* | ||
* Copyright (C) 2023-2023 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 java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* 网络地址工具类 | ||
* | ||
* @author zhp | ||
* @since 2023-12-15 | ||
*/ | ||
public class InetAddressUtil { | ||
private static final Logger LOGGER = Logger.getLogger(InetAddressUtil.class.getName()); | ||
|
||
private static volatile String hostAddress; | ||
|
||
private InetAddressUtil() { | ||
} | ||
|
||
/** | ||
* 获取本机IP地址 | ||
* | ||
* @return IP地址 | ||
*/ | ||
public static String getHostAddress() { | ||
if (hostAddress == null) { | ||
synchronized (InetAddressUtil.class) { | ||
if (hostAddress == null) { | ||
try { | ||
InetAddress localhost = InetAddress.getLocalHost(); | ||
hostAddress = localhost.getHostAddress(); | ||
} catch (UnknownHostException e) { | ||
LOGGER.log(Level.SEVERE, "Unable to obtain local host address."); | ||
} | ||
} | ||
} | ||
} | ||
return hostAddress; | ||
} | ||
} |
File renamed without changes.
69 changes: 69 additions & 0 deletions
69
...ns/sermant-metrics/metrics-common/src/main/java/com/huawei/metrics/util/SqlParseUtil.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,69 @@ | ||
/* | ||
* Copyright (C) 2023-2023 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.huawei.metrics.common.Constants; | ||
|
||
import com.huaweicloud.sermant.core.common.LoggerFactory; | ||
import com.huaweicloud.sermant.core.utils.StringUtils; | ||
|
||
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.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* SQL解析工具类 | ||
* | ||
* @author zhp | ||
* @since 2023-12-15 | ||
*/ | ||
public class SqlParseUtil { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(); | ||
|
||
private SqlParseUtil() { | ||
} | ||
|
||
/** | ||
* 获取mysql的API维度信息(命令类型_表名) | ||
* | ||
* @param sql 执行的SQL语句 | ||
* @return API | ||
*/ | ||
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); | ||
if (tables != null) { | ||
for (String table : tables) { | ||
stringBuilder.append(Constants.CONNECT).append(table); | ||
} | ||
} | ||
return stringBuilder.toString(); | ||
} catch (JSQLParserException | UnsupportedOperationException e) { | ||
LOGGER.log(Level.INFO, "There is currently no table data in SQL."); | ||
return stringBuilder.toString(); | ||
} | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
.../main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig
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 @@ | ||
com.huawei.metrics.config.MetricsConfig |
Oops, something went wrong.