-
Notifications
You must be signed in to change notification settings - Fork 172
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
177a697
commit a2c47bc
Showing
33 changed files
with
814 additions
and
132 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
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
55 changes: 55 additions & 0 deletions
55
...t-metrics/metrics-common/src/main/java/com/huawei/metrics/util/MysqlTablesNameFinder.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,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()); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
...-mariadb2.x-plugin/src/main/java/com/huawei/metrics/declarer/ExecutePreparedDeclarer.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,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()) | ||
}; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...adb2.x-plugin/src/main/java/com/huawei/metrics/declarer/ExecutePreparedQueryDeclarer.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,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()) | ||
}; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...plugin/src/main/java/com/huawei/metrics/interceptor/AbstractQueryProtocolInterceptor.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,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; | ||
} | ||
} |
Oops, something went wrong.