From bec29926ef8c1d895faac67b65c5541a1a5d984d Mon Sep 17 00:00:00 2001 From: hanbingleixue Date: Mon, 6 Nov 2023 14:31:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8C=87=E6=A0=87=E9=87=87?= =?UTF-8?q?=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/plugins.yaml | 2 +- sermant-plugins/pom.xml | 6 +- .../declarer/alibaba/HandlerDeclarer.java | 48 --- .../jsse/declarer/alibaba/InvokeDeclarer.java | 47 --- .../jsse/declarer/apache/HandlerDeclarer.java | 48 --- .../jsse/declarer/apache/InvokeDeclarer.java | 47 --- .../interceptor/alibaba/CodecInterceptor.java | 143 ------- .../alibaba/HandlerInterceptor.java | 124 ------ .../alibaba/InvokeInterceptor.java | 134 ------- .../interceptor/apache/CodecInterceptor.java | 144 ------- .../apache/HandlerInterceptor.java | 124 ------ .../interceptor/apache/InvokeInterceptor.java | 134 ------- .../com/huawei/jsse/manager/JsseManager.java | 76 ---- ...ud.sermant.core.plugin.config.PluginConfig | 1 - .../com/huawei/jsse/service/JsseService.java | 331 ---------------- .../config/config.yaml | 4 +- .../metrics-plugin}/pom.xml | 8 +- .../com/huawei/metrics}/common/Constants.java | 52 ++- .../huawei/metrics/config/MetricsConfig.java} | 8 +- .../metrics}/declarer/AbstractDeclarer.java | 8 +- .../alibaba/ExchangeCodecDeclarer.java} | 25 +- .../alibaba/MonitorFilterDeclarer.java} | 16 +- .../apache/ExchangeCodecDeclarer.java} | 25 +- .../apache/MonitorFilterDeclarer.java} | 16 +- .../huawei/metrics/entity/MetricsInfo.java} | 6 +- .../metrics/entity/MetricsLinkInfo.java} | 19 +- .../metrics/entity/MetricsRpcInfo.java} | 6 +- .../interceptor/AbstractCodecInterceptor.java | 198 ++++++++++ .../AbstractFilterInterceptor.java | 159 ++++++++ .../alibaba/ExchangeCodecInterceptor.java | 74 ++++ .../alibaba/MonitorFilterInterceptor.java | 50 +++ .../apache/ExchangeCodecInterceptor.java | 74 ++++ .../apache/MonitorFilterInterceptor.java | 50 +++ .../metrics/manager/MetricsManager.java | 76 ++++ ....core.plugin.agent.declarer.PluginDeclarer | 12 +- ...ud.sermant.core.plugin.config.PluginConfig | 1 + .../metrics-service}/pom.xml | 6 +- .../metrics/service/MetricsService.java | 364 ++++++++++++++++++ ....sermant.core.plugin.service.PluginService | 2 +- .../{sermant-jsse => sermant-metrics}/pom.xml | 16 +- 40 files changed, 1195 insertions(+), 1489 deletions(-) delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/HandlerDeclarer.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/InvokeDeclarer.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/HandlerDeclarer.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/InvokeDeclarer.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/CodecInterceptor.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/HandlerInterceptor.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/InvokeInterceptor.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/CodecInterceptor.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/HandlerInterceptor.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/InvokeInterceptor.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/manager/JsseManager.java delete mode 100644 sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig delete mode 100644 sermant-plugins/sermant-jsse/jsse-service/src/main/java/com/huawei/jsse/service/JsseService.java rename sermant-plugins/{sermant-jsse => sermant-metrics}/config/config.yaml (61%) rename sermant-plugins/{sermant-jsse/jsse-plugin => sermant-metrics/metrics-plugin}/pom.xml (93%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics}/common/Constants.java (64%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/config/JsseConfig.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/config/MetricsConfig.java} (92%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics}/declarer/AbstractDeclarer.java (83%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/DecodeDeclarer.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/ExchangeCodecDeclarer.java} (59%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/EncodeDeclarer.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/MonitorFilterDeclarer.java} (76%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/DecodeDeclarer.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/ExchangeCodecDeclarer.java} (59%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/EncodeDeclarer.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/MonitorFilterDeclarer.java} (76%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseInfo.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsInfo.java} (97%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseLinkInfo.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsLinkInfo.java} (78%) rename sermant-plugins/{sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseRpcInfo.java => sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsRpcInfo.java} (95%) create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractCodecInterceptor.java create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractFilterInterceptor.java create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/ExchangeCodecInterceptor.java create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/MonitorFilterInterceptor.java create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/ExchangeCodecInterceptor.java create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/MonitorFilterInterceptor.java create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/manager/MetricsManager.java rename sermant-plugins/{sermant-jsse/jsse-plugin => sermant-metrics/metrics-plugin}/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer (62%) create mode 100644 sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig rename sermant-plugins/{sermant-jsse/jsse-service => sermant-metrics/metrics-service}/pom.xml (93%) create mode 100644 sermant-plugins/sermant-metrics/metrics-service/src/main/java/com/huawei/metrics/service/MetricsService.java rename sermant-plugins/{sermant-jsse/jsse-service => sermant-metrics/metrics-service}/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService (93%) rename sermant-plugins/{sermant-jsse => sermant-metrics}/pom.xml (73%) diff --git a/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml b/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml index 3dc499bbe2..f5b3e710f1 100644 --- a/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml +++ b/sermant-agentcore/sermant-agentcore-config/config/plugins.yaml @@ -14,7 +14,7 @@ plugins: # agentmain启动时生效,配置支持动态安装插件,active类型插件将会主动启用, passive类型插件需通过指令或接口调用启用,允许卸载 dynamicPlugins: active: - - jsse + - metrics # - active-plugin passive: # - passive-plugin diff --git a/sermant-plugins/pom.xml b/sermant-plugins/pom.xml index 043b177688..aff240f7b8 100644 --- a/sermant-plugins/pom.xml +++ b/sermant-plugins/pom.xml @@ -42,7 +42,7 @@ sermant-service-removal sermant-spring-beans-deal sermant-tag-transmission - sermant-jsse + sermant-metrics @@ -75,7 +75,7 @@ sermant-service-removal sermant-spring-beans-deal sermant-tag-transmission - sermant-jsse + sermant-metrics @@ -108,7 +108,7 @@ sermant-service-removal sermant-spring-beans-deal sermant-tag-transmission - sermant-jsse + sermant-metrics diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/HandlerDeclarer.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/HandlerDeclarer.java deleted file mode 100644 index 97b227faab..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/HandlerDeclarer.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.jsse.declarer.alibaba; - -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.alibaba.HandlerInterceptor; - -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; - -/** - * dubbo服务处理增强声明 - * - * @author zhp - * @since 2023-10-17 - */ -public class HandlerDeclarer extends AbstractDeclarer { - private static final String ENHANCE_CLASS = - "com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler"; - - private static final String METHODS_NAME = "handleRequest"; - - @Override - public ClassMatcher getClassMatcher() { - return ClassMatcher.nameEquals(ENHANCE_CLASS); - } - - @Override - public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME), - new HandlerInterceptor())}; - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/InvokeDeclarer.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/InvokeDeclarer.java deleted file mode 100644 index 6ce164d6ff..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/InvokeDeclarer.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.jsse.declarer.alibaba; - -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.alibaba.InvokeInterceptor; - -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; - -/** - * dubbo调用增强声明 - * - * @author zhp - * @since 2023-10-17 - */ -public class InvokeDeclarer extends AbstractDeclarer { - private static final String ENHANCE_CLASS = "com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker"; - - private static final String METHODS_NAME = "doInvoke"; - - @Override - public ClassMatcher getClassMatcher() { - return ClassMatcher.nameEquals(ENHANCE_CLASS); - } - - @Override - public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME), - new InvokeInterceptor())}; - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/HandlerDeclarer.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/HandlerDeclarer.java deleted file mode 100644 index a6a2ead3d2..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/HandlerDeclarer.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.jsse.declarer.apache; - -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.apache.HandlerInterceptor; - -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; - -/** - * dubbo服务处理增强声明 - * - * @author zhp - * @since 2023-10-17 - */ -public class HandlerDeclarer extends AbstractDeclarer { - private static final String ENHANCE_CLASS = - "org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler"; - - private static final String METHODS_NAME = "handleRequest"; - - @Override - public ClassMatcher getClassMatcher() { - return ClassMatcher.nameEquals(ENHANCE_CLASS); - } - - @Override - public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME), - new HandlerInterceptor())}; - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/InvokeDeclarer.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/InvokeDeclarer.java deleted file mode 100644 index ff853f0c8d..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/InvokeDeclarer.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.jsse.declarer.apache; - -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.apache.InvokeInterceptor; - -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; - -/** - * dubbo调用增强声明 - * - * @author zhp - * @since 2023-10-17 - */ -public class InvokeDeclarer extends AbstractDeclarer { - private static final String ENHANCE_CLASS = "org.apache.dubbo.rpc.protocol.dubbo.DubboInvoker"; - - private static final String METHODS_NAME = "doInvoke"; - - @Override - public ClassMatcher getClassMatcher() { - return ClassMatcher.nameEquals(ENHANCE_CLASS); - } - - @Override - public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME), - new InvokeInterceptor())}; - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/CodecInterceptor.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/CodecInterceptor.java deleted file mode 100644 index cb73a83877..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/CodecInterceptor.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * 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.jsse.interceptor.alibaba; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.entity.JsseLinkInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.utils.NetUtils; -import com.alibaba.dubbo.remoting.Channel; -import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; - -import java.net.InetSocketAddress; - -/** - * dubbo报文加解码拦截器 - * - * @author zhp - * @since 2023-10-17 - */ -public class CodecInterceptor implements Interceptor { - private static final int PARAM_COUNT = 2; - - private static final String TCP_PROTOCOL = "dubbo,rmi,http,https"; - - private static final String ENCODE_METHOD_NAME = "encode"; - - private int writeIndex; - - private int readIndex; - - private String key; - - @Override - public ExecuteContext before(ExecuteContext context) { - Object[] arguments = context.getArguments(); - if (arguments == null || arguments.length < PARAM_COUNT - || !(arguments[0] instanceof Channel && arguments[1] instanceof ChannelBuffer)) { - return context; - } - Channel channel = (Channel) arguments[0]; - ChannelBuffer buffer = (ChannelBuffer) arguments[1]; - InetSocketAddress localAddress = channel.getLocalAddress(); - InetSocketAddress remoteAddress = channel.getRemoteAddress(); - key = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getAddress(); - JsseLinkInfo jsseLinkInfo = JsseManager.getJsseLink(key); - if (StringUtils.isEmpty(jsseLinkInfo.getClientIp())) { - initJsseLinkInfo(jsseLinkInfo, channel); - } - writeIndex = buffer.writerIndex(); - readIndex = buffer.readerIndex(); - return context; - } - - @Override - public ExecuteContext after(ExecuteContext context) { - Object[] arguments = context.getArguments(); - if (arguments == null || arguments.length < PARAM_COUNT - || !(arguments[0] instanceof Channel && arguments[1] instanceof ChannelBuffer)) { - return context; - } - ChannelBuffer buffer = (ChannelBuffer) arguments[1]; - JsseLinkInfo jsseLinkInfo = JsseManager.getJsseLink(key); - if (StringUtils.equals(context.getMethod().getName(), ENCODE_METHOD_NAME)) { - jsseLinkInfo.getSentBytes().addAndGet(buffer.writerIndex() - writeIndex); - jsseLinkInfo.getSentMessages().incrementAndGet(); - return context; - } - if (buffer.readerIndex() - readIndex > 0) { - jsseLinkInfo.getReceiveBytes().addAndGet(buffer.readerIndex() - readIndex); - jsseLinkInfo.getReceiveMessages().incrementAndGet(); - } - return context; - } - - @Override - public ExecuteContext onThrow(ExecuteContext context) { - return context; - } - - /** - * 初始化jsse连接信息 - * - * @param jsseLinkInfo 连接信息 - * @param channel 渠道信息 - */ - private void initJsseLinkInfo(JsseLinkInfo jsseLinkInfo, Channel channel) { - jsseLinkInfo.setClientIp(channel.getLocalAddress().getHostName()); - jsseLinkInfo.setServerIp(channel.getRemoteAddress().getHostName()); - jsseLinkInfo.setServerPort(StringUtils.getString(channel.getRemoteAddress().getPort())); - jsseLinkInfo.setProtocol(channel.getUrl().getProtocol()); - if (isClientSide(channel)) { - jsseLinkInfo.setL7Role(Constants.CLIENT_ROLE); - } else { - jsseLinkInfo.setL7Role(Constants.SERVER_ROLE); - } - if (TCP_PROTOCOL.contains(jsseLinkInfo.getProtocol())) { - jsseLinkInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + jsseLinkInfo.getL7Role()); - } else { - jsseLinkInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + jsseLinkInfo.getL7Role()); - } - jsseLinkInfo.setEnableSsl(Boolean.parseBoolean(channel.getUrl().getParameter(Constants.SSL_ENABLE))); - } - - /** - * 是否为客户端 - * - * @param channel 渠道信息 - * @return 是否为客户端的结果 - */ - private boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(com.alibaba.dubbo.common.Constants.SIDE_KEY); - if (Constants.CLIENT_ROLE.equals(side)) { - return true; - } - if (Constants.SERVER_ROLE.equals(side)) { - return false; - } - InetSocketAddress address = channel.getRemoteAddress(); - URL url = channel.getUrl(); - return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) - .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/HandlerInterceptor.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/HandlerInterceptor.java deleted file mode 100644 index 409d19aafe..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/HandlerInterceptor.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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.jsse.interceptor.alibaba; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.entity.JsseRpcInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.utils.NetUtils; -import com.alibaba.dubbo.remoting.Channel; -import com.alibaba.dubbo.remoting.exchange.ExchangeChannel; - -import java.net.InetSocketAddress; - -/** - * dubbo请求处理拦截器 - * - * @author zhp - * @since 2023-10-17 - */ -public class HandlerInterceptor implements Interceptor { - private long startTime; - - private String key; - - @Override - public ExecuteContext before(ExecuteContext context) { - if (context.getArguments() == null || !(context.getArguments()[0] instanceof ExchangeChannel)) { - return context; - } - startTime = System.currentTimeMillis(); - ExchangeChannel channel = (ExchangeChannel) context.getArguments()[0]; - JsseRpcInfo jsseRpcInfo = initJsseRpcInfo(channel); - jsseRpcInfo.getReqCount().incrementAndGet(); - return context; - } - - @Override - public ExecuteContext after(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getResponseCount().incrementAndGet(); - long latency = System.currentTimeMillis() - startTime; - jsseRpcInfo.getSumLatency().addAndGet(latency); - jsseRpcInfo.getLatencyList().add(latency); - return context; - } - - @Override - public ExecuteContext onThrow(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getReqErrorCount().incrementAndGet(); - return context; - } - - /** - * 初始化jsse调用信息 - * - * @param channel 渠道信息 - * @return 调用信息 - */ - private JsseRpcInfo initJsseRpcInfo(Channel channel) { - InetSocketAddress localAddress = channel.getLocalAddress(); - InetSocketAddress remoteAddress = channel.getRemoteAddress(); - key = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getAddress(); - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - if (StringUtils.isEmpty(jsseRpcInfo.getClientIp())) { - jsseRpcInfo.setClientIp(channel.getLocalAddress().getHostName()); - jsseRpcInfo.setServerIp(channel.getRemoteAddress().getHostName()); - jsseRpcInfo.setServerPort(StringUtils.getString(channel.getRemoteAddress().getPort())); - jsseRpcInfo.setProtocol(channel.getUrl().getProtocol()); - if (isClientSide(channel)) { - jsseRpcInfo.setL7Role(Constants.CLIENT_ROLE); - } else { - jsseRpcInfo.setL7Role(Constants.SERVER_ROLE); - } - if (Constants.TCP_PROTOCOLS.contains(jsseRpcInfo.getProtocol())) { - jsseRpcInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } else { - jsseRpcInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } - jsseRpcInfo.setEnableSsl(Boolean.parseBoolean(channel.getUrl().getParameter(Constants.SSL_ENABLE))); - } - return jsseRpcInfo; - } - - /** - * 是否为客户端 - * - * @param channel 渠道信息 - * @return 是否为客户端的结果 - */ - private boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(com.alibaba.dubbo.common.Constants.SIDE_KEY); - if (Constants.CLIENT_ROLE.equals(side)) { - return true; - } else if (Constants.SERVER_ROLE.equals(side)) { - return false; - } else { - InetSocketAddress address = channel.getRemoteAddress(); - URL url = channel.getUrl(); - return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) - .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); - } - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/InvokeInterceptor.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/InvokeInterceptor.java deleted file mode 100644 index 9cba7b8b76..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/alibaba/InvokeInterceptor.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.jsse.interceptor.alibaba; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.entity.JsseRpcInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; -import com.huaweicloud.sermant.core.utils.ReflectUtils; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.utils.AtomicPositiveInteger; -import com.alibaba.dubbo.common.utils.NetUtils; -import com.alibaba.dubbo.remoting.Channel; -import com.alibaba.dubbo.remoting.exchange.ExchangeClient; - -import java.net.InetSocketAddress; -import java.util.Optional; - -/** - * dubbo服务调用拦截器 - * - * @author zhp - * @since 2023-10-17 - */ -public class InvokeInterceptor implements Interceptor { - private long startTime; - - private String key; - - @Override - public ExecuteContext before(ExecuteContext context) { - Optional indexOptional = ReflectUtils.getFieldValue(context.getObject(), Constants.CLIENT_INDEX); - Optional clientOptional = ReflectUtils.getFieldValue(context.getObject(), Constants.CLIENTS_NAME); - if (!indexOptional.isPresent() || !clientOptional.isPresent() - || !(indexOptional.get() instanceof AtomicPositiveInteger) - || !(clientOptional.get() instanceof ExchangeClient[])) { - return context; - } - AtomicPositiveInteger index = (AtomicPositiveInteger) indexOptional.get(); - ExchangeClient[] clients = (ExchangeClient[]) clientOptional.get(); - ExchangeClient client = clients[index.get() % clients.length]; - startTime = System.currentTimeMillis(); - JsseRpcInfo jsseRpcInfo = initJsseRpcInfo(client); - jsseRpcInfo.getReqCount().incrementAndGet(); - return context; - } - - @Override - public ExecuteContext after(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getResponseCount().incrementAndGet(); - long latency = System.currentTimeMillis() - startTime; - jsseRpcInfo.getSumLatency().addAndGet(latency); - jsseRpcInfo.getLatencyList().add(latency); - return context; - } - - @Override - public ExecuteContext onThrow(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getReqErrorCount().incrementAndGet(); - return context; - } - - /** - * 初始化jsse调用信息 - * - * @param channel 渠道信息 - * @return 调用信息 - */ - private JsseRpcInfo initJsseRpcInfo(Channel channel) { - InetSocketAddress localAddress = channel.getLocalAddress(); - InetSocketAddress remoteAddress = channel.getRemoteAddress(); - key = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getAddress(); - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - if (!StringUtils.isEmpty(jsseRpcInfo.getClientIp())) { - return jsseRpcInfo; - } - jsseRpcInfo.setClientIp(channel.getLocalAddress().getHostName()); - jsseRpcInfo.setServerIp(channel.getRemoteAddress().getHostName()); - jsseRpcInfo.setServerPort(StringUtils.getString(channel.getRemoteAddress().getPort())); - jsseRpcInfo.setProtocol(channel.getUrl().getProtocol()); - if (isClientSide(channel)) { - jsseRpcInfo.setL7Role(Constants.CLIENT_ROLE); - } else { - jsseRpcInfo.setL7Role(Constants.SERVER_ROLE); - } - if (Constants.TCP_PROTOCOLS.contains(jsseRpcInfo.getProtocol())) { - jsseRpcInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } else { - jsseRpcInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } - jsseRpcInfo.setEnableSsl(Boolean.parseBoolean(channel.getUrl().getParameter(Constants.SSL_ENABLE))); - return jsseRpcInfo; - } - - /** - * 是否为客户端 - * - * @param channel 渠道信息 - * @return 是否为客户端的结果 - */ - private boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(com.alibaba.dubbo.common.Constants.SIDE_KEY); - if (Constants.CLIENT_ROLE.equals(side)) { - return true; - } else if (Constants.SERVER_ROLE.equals(side)) { - return false; - } else { - InetSocketAddress address = channel.getRemoteAddress(); - URL url = channel.getUrl(); - return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) - .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); - } - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/CodecInterceptor.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/CodecInterceptor.java deleted file mode 100644 index 77bb243e66..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/CodecInterceptor.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * 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.jsse.interceptor.apache; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.entity.JsseLinkInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.constants.CommonConstants; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.buffer.ChannelBuffer; - -import java.net.InetSocketAddress; - -/** - * dubbo报文加解码拦截器 - * - * @author zhp - * @since 2023-10-17 - */ -public class CodecInterceptor implements Interceptor { - private static final int PARAM_COUNT = 2; - - private static final String ENCODE_METHOD_NAME = "encode"; - - private int writeIndex; - - private int readIndex; - - private String key; - - @Override - public ExecuteContext before(ExecuteContext context) { - Object[] arguments = context.getArguments(); - if (arguments == null || arguments.length < PARAM_COUNT - || !(arguments[0] instanceof Channel - && arguments[1] instanceof ChannelBuffer)) { - return context; - } - Channel channel = (Channel) arguments[0]; - ChannelBuffer buffer = (ChannelBuffer) arguments[1]; - InetSocketAddress localAddress = channel.getLocalAddress(); - InetSocketAddress remoteAddress = channel.getRemoteAddress(); - key = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getAddress(); - JsseLinkInfo jsseLinkInfo = JsseManager.getJsseLink(key); - if (StringUtils.isEmpty(jsseLinkInfo.getClientIp())) { - initJsseLinkInfo(jsseLinkInfo, channel); - } - writeIndex = buffer.writerIndex(); - readIndex = buffer.readerIndex(); - return context; - } - - @Override - public ExecuteContext after(ExecuteContext context) { - Object[] arguments = context.getArguments(); - if (arguments == null || arguments.length < PARAM_COUNT - || !(arguments[0] instanceof Channel - && arguments[1] instanceof ChannelBuffer)) { - return context; - } - ChannelBuffer buffer = (ChannelBuffer) arguments[1]; - JsseLinkInfo jsseLinkInfo = JsseManager.getJsseLink(key); - if (StringUtils.equals(context.getMethod().getName(), ENCODE_METHOD_NAME)) { - jsseLinkInfo.getSentBytes().addAndGet(buffer.writerIndex() - writeIndex); - jsseLinkInfo.getSentMessages().incrementAndGet(); - return context; - } - if (buffer.readerIndex() - readIndex > 0) { - jsseLinkInfo.getReceiveBytes().addAndGet(buffer.readerIndex() - readIndex); - jsseLinkInfo.getReceiveMessages().incrementAndGet(); - } - return context; - } - - @Override - public ExecuteContext onThrow(ExecuteContext context) { - return context; - } - - /** - * 初始化jsse连接信息 - * - * @param jsseLinkInfo 连接信息 - * @param channel 渠道信息 - */ - private void initJsseLinkInfo(JsseLinkInfo jsseLinkInfo, Channel channel) { - jsseLinkInfo.setClientIp(channel.getLocalAddress().getHostName()); - jsseLinkInfo.setServerIp(channel.getRemoteAddress().getHostName()); - jsseLinkInfo.setServerPort(StringUtils.getString(channel.getRemoteAddress().getPort())); - jsseLinkInfo.setProtocol(channel.getUrl().getProtocol()); - if (isClientSide(channel)) { - jsseLinkInfo.setL7Role(Constants.CLIENT_ROLE); - } else { - jsseLinkInfo.setL7Role(Constants.SERVER_ROLE); - } - if (Constants.TCP_PROTOCOLS.contains(jsseLinkInfo.getProtocol())) { - jsseLinkInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + jsseLinkInfo.getL7Role()); - } else { - jsseLinkInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + jsseLinkInfo.getL7Role()); - } - jsseLinkInfo.setEnableSsl(Boolean.parseBoolean(channel.getUrl().getParameter(Constants.SSL_ENABLE))); - } - - /** - * 是否为客户端 - * - * @param channel 渠道信息 - * @return 是否为客户端的结果 - */ - private boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(CommonConstants.SIDE_KEY); - if (Constants.CLIENT_ROLE.equals(side)) { - return true; - } - if (Constants.SERVER_ROLE.equals(side)) { - return false; - } - InetSocketAddress address = channel.getRemoteAddress(); - URL url = channel.getUrl(); - return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) - .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/HandlerInterceptor.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/HandlerInterceptor.java deleted file mode 100644 index ce72b930f6..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/HandlerInterceptor.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * 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.jsse.interceptor.apache; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.entity.JsseRpcInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.exchange.ExchangeChannel; - -import java.net.InetSocketAddress; - -/** - * dubbo请求处理拦截器 - * - * @author zhp - * @since 2023-10-17 - */ -public class HandlerInterceptor implements Interceptor { - private long startTime; - - private String key; - - @Override - public ExecuteContext before(ExecuteContext context) { - if (context.getArguments() == null || !(context.getArguments()[0] instanceof ExchangeChannel)) { - return context; - } - startTime = System.currentTimeMillis(); - ExchangeChannel channel = (ExchangeChannel) context.getArguments()[0]; - JsseRpcInfo jsseRpcInfo = initJsseRpcInfo(channel); - jsseRpcInfo.getReqCount().incrementAndGet(); - return context; - } - - @Override - public ExecuteContext after(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getResponseCount().incrementAndGet(); - long latency = System.currentTimeMillis() - startTime; - jsseRpcInfo.getSumLatency().addAndGet(latency); - jsseRpcInfo.getLatencyList().add(latency); - return context; - } - - @Override - public ExecuteContext onThrow(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getReqErrorCount().incrementAndGet(); - return context; - } - - /** - * 初始化jsse调用信息 - * - * @param channel 渠道信息 - * @return 调用信息 - */ - private JsseRpcInfo initJsseRpcInfo(Channel channel) { - InetSocketAddress localAddress = channel.getLocalAddress(); - InetSocketAddress remoteAddress = channel.getRemoteAddress(); - key = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getAddress(); - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - if (StringUtils.isEmpty(jsseRpcInfo.getClientIp())) { - jsseRpcInfo.setClientIp(channel.getLocalAddress().getHostName()); - jsseRpcInfo.setServerIp(channel.getRemoteAddress().getHostName()); - jsseRpcInfo.setServerPort(StringUtils.getString(channel.getRemoteAddress().getPort())); - jsseRpcInfo.setProtocol(channel.getUrl().getProtocol()); - if (isClientSide(channel)) { - jsseRpcInfo.setL7Role(Constants.CLIENT_ROLE); - } else { - jsseRpcInfo.setL7Role(Constants.SERVER_ROLE); - } - if (Constants.TCP_PROTOCOLS.contains(jsseRpcInfo.getProtocol())) { - jsseRpcInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } else { - jsseRpcInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } - jsseRpcInfo.setEnableSsl(Boolean.parseBoolean(channel.getUrl().getParameter(Constants.SSL_ENABLE))); - } - return jsseRpcInfo; - } - - /** - * 是否为客户端 - * - * @param channel 渠道信息 - * @return 是否为客户端的结果 - */ - private boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(com.alibaba.dubbo.common.Constants.SIDE_KEY); - if (Constants.CLIENT_ROLE.equals(side)) { - return true; - } else if (Constants.SERVER_ROLE.equals(side)) { - return false; - } else { - InetSocketAddress address = channel.getRemoteAddress(); - URL url = channel.getUrl(); - return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) - .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); - } - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/InvokeInterceptor.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/InvokeInterceptor.java deleted file mode 100644 index 2cab8b93b6..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/interceptor/apache/InvokeInterceptor.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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.jsse.interceptor.apache; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.entity.JsseRpcInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; -import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; -import com.huaweicloud.sermant.core.utils.ReflectUtils; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.utils.AtomicPositiveInteger; -import org.apache.dubbo.common.utils.NetUtils; -import org.apache.dubbo.remoting.Channel; -import org.apache.dubbo.remoting.exchange.ExchangeClient; - -import java.net.InetSocketAddress; -import java.util.Optional; - -/** - * dubbo服务调用拦截器 - * - * @author zhp - * @since 2023-10-17 - */ -public class InvokeInterceptor implements Interceptor { - private long startTime; - - private String key; - - @Override - public ExecuteContext before(ExecuteContext context) { - Optional indexOptional = ReflectUtils.getFieldValue(context.getObject(), Constants.CLIENT_INDEX); - Optional clientOptional = ReflectUtils.getFieldValue(context.getObject(), Constants.CLIENTS_NAME); - if (!indexOptional.isPresent() || !clientOptional.isPresent() - || !(indexOptional.get() instanceof AtomicPositiveInteger) - || !(clientOptional.get() instanceof ExchangeClient[])) { - return context; - } - AtomicPositiveInteger index = (AtomicPositiveInteger) indexOptional.get(); - ExchangeClient[] clients = (ExchangeClient[]) clientOptional.get(); - ExchangeClient client = clients[index.get() % clients.length]; - startTime = System.currentTimeMillis(); - JsseRpcInfo jsseRpcInfo = initJsseRpcInfo(client); - jsseRpcInfo.getReqCount().incrementAndGet(); - return context; - } - - @Override - public ExecuteContext after(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getResponseCount().incrementAndGet(); - long latency = System.currentTimeMillis() - startTime; - jsseRpcInfo.getSumLatency().addAndGet(latency); - jsseRpcInfo.getLatencyList().add(latency); - return context; - } - - @Override - public ExecuteContext onThrow(ExecuteContext context) { - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - jsseRpcInfo.getReqErrorCount().incrementAndGet(); - return context; - } - - /** - * 初始化jsse调用信息 - * - * @param channel 渠道信息 - * @return 调用信息 - */ - private JsseRpcInfo initJsseRpcInfo(Channel channel) { - InetSocketAddress localAddress = channel.getLocalAddress(); - InetSocketAddress remoteAddress = channel.getRemoteAddress(); - key = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getAddress(); - JsseRpcInfo jsseRpcInfo = JsseManager.getJsseRpc(key); - if (!StringUtils.isEmpty(jsseRpcInfo.getClientIp())) { - return jsseRpcInfo; - } - jsseRpcInfo.setClientIp(channel.getLocalAddress().getHostName()); - jsseRpcInfo.setServerIp(channel.getRemoteAddress().getHostName()); - jsseRpcInfo.setServerPort(StringUtils.getString(channel.getRemoteAddress().getPort())); - jsseRpcInfo.setProtocol(channel.getUrl().getProtocol()); - if (isClientSide(channel)) { - jsseRpcInfo.setL7Role(Constants.CLIENT_ROLE); - } else { - jsseRpcInfo.setL7Role(Constants.SERVER_ROLE); - } - if (Constants.TCP_PROTOCOLS.contains(jsseRpcInfo.getProtocol())) { - jsseRpcInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } else { - jsseRpcInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + jsseRpcInfo.getL7Role()); - } - jsseRpcInfo.setEnableSsl(Boolean.parseBoolean(channel.getUrl().getParameter(Constants.SSL_ENABLE))); - return jsseRpcInfo; - } - - /** - * 是否为客户端 - * - * @param channel 渠道信息 - * @return 是否为客户端的结果 - */ - private boolean isClientSide(Channel channel) { - String side = (String) channel.getAttribute(com.alibaba.dubbo.common.Constants.SIDE_KEY); - if (Constants.CLIENT_ROLE.equals(side)) { - return true; - } else if (Constants.SERVER_ROLE.equals(side)) { - return false; - } else { - InetSocketAddress address = channel.getRemoteAddress(); - URL url = channel.getUrl(); - return url.getPort() == address.getPort() && NetUtils.filterLocalHost(url.getIp()) - .equals(NetUtils.filterLocalHost(address.getAddress().getHostAddress())); - } - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/manager/JsseManager.java b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/manager/JsseManager.java deleted file mode 100644 index e64ad2c52a..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/manager/JsseManager.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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.jsse.manager; - -import com.huawei.jsse.entity.JsseLinkInfo; -import com.huawei.jsse.entity.JsseRpcInfo; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * JSSE管理类 - * - * @author zhp - * @since 2023-10-17 - */ -public class JsseManager { - private static final Map JSSE_LINK_INFO_MAP = new ConcurrentHashMap<>(); - - private static final Map JSSE_RPC_INFO_MAP = new ConcurrentHashMap<>(); - - private JsseManager() { - } - - /** - * 获取JSSE连接信息 - * - * @return JSSE连接信息 - */ - public static Map getJsseLinkMap() { - return JSSE_LINK_INFO_MAP; - } - - /** - * 获取JSSE连接信息 - * - * @return JSSE连接信息 - */ - public static Map getJsseRpcMap() { - return JSSE_RPC_INFO_MAP; - } - - /** - * 获取JSSE连接信息 - * - * @param key JSSE MAP的密钥信息 - * @return Jsse信息 - */ - public static JsseLinkInfo getJsseLink(String key) { - return JSSE_LINK_INFO_MAP.computeIfAbsent(key, s -> new JsseLinkInfo()); - } - - /** - * 获取JSSE RPC信息 - * - * @param key JSSE MAP的密钥信息 - * @return Jsse RPC信息 - */ - public static JsseRpcInfo getJsseRpc(String key) { - return JSSE_RPC_INFO_MAP.computeIfAbsent(key, s -> new JsseRpcInfo()); - } -} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig b/sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig deleted file mode 100644 index 289f576152..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig +++ /dev/null @@ -1 +0,0 @@ -com.huawei.jsse.config.JsseConfig \ No newline at end of file diff --git a/sermant-plugins/sermant-jsse/jsse-service/src/main/java/com/huawei/jsse/service/JsseService.java b/sermant-plugins/sermant-jsse/jsse-service/src/main/java/com/huawei/jsse/service/JsseService.java deleted file mode 100644 index d6cb782820..0000000000 --- a/sermant-plugins/sermant-jsse/jsse-service/src/main/java/com/huawei/jsse/service/JsseService.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * 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.jsse.service; - -import com.huawei.jsse.common.Constants; -import com.huawei.jsse.config.JsseConfig; -import com.huawei.jsse.entity.JsseLinkInfo; -import com.huawei.jsse.entity.JsseRpcInfo; -import com.huawei.jsse.manager.JsseManager; - -import com.huaweicloud.sermant.core.common.LoggerFactory; -import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; -import com.huaweicloud.sermant.core.plugin.service.PluginService; -import com.huaweicloud.sermant.core.utils.StringUtils; - -import com.alibaba.fastjson.JSONObject; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.RandomAccessFile; -import java.lang.management.ManagementFactory; -import java.lang.management.RuntimeMXBean; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.net.InetAddress; -import java.nio.channels.FileLock; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Locale; -import java.util.Map.Entry; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * JSSE服务类,上报指标数据 - * - * @author zhp - * @since 2023-10-17 - */ -public class JsseService implements PluginService { - private static final Logger LOGGER = LoggerFactory.getLogger(); - - private static final JsseConfig JSSE_CONFIG = PluginConfigManager.getPluginConfig(JsseConfig.class); - - private static final int CONTAINER_NAME_INDEX = 2; - - private static final int ENABLE_NUM = 2; - - /** - * 换行符。Gopher解析时需要用到 - * System.lineSeparator()在UNIX返回"\n" 在Windows 返回"\r\n". 因此不能修改为System.lineSeparator(); - */ - private static final String LINE_BREAK = "|\r\n"; - - private static final BigDecimal MULTIPLYING_POWER = new BigDecimal(1000); - - private static final int[] RANGE = {0, 3, 10, 50, 100, 500, 1000, 10000}; - - /** - * 进程Id - */ - private String pid; - - /** - * 进程名称 - */ - private String processName; - - /** - * 机器Id - */ - private String machineId; - - /** - * 容器ID - */ - private String containerId; - - /** - * 容器名称 - */ - private String containerName; - - /** - * 容器IP - */ - private String containerIp; - - /** - * 文件名称 - */ - private String jsseFileName; - - private ScheduledExecutorService executorService; - - @Override - public void start() { - if (!JSSE_CONFIG.isEnable()) { - return; - } - RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); - String name = runtimeMxBean.getName(); - int index = name.indexOf(Constants.PROCESS_NAME_LINK); - if (index != -1) { - pid = name.substring(0, index); - processName = name.substring(index + 1); - } else { - pid = "-1"; - processName = StringUtils.EMPTY; - } - initMachineId(); - initContainerInfo(); - String filePath = JSSE_CONFIG.getFilePath() + pid; - jsseFileName = filePath + Constants.FILE_PATH_LINK + JSSE_CONFIG.getFileName(); - try { - createTmpFile(filePath, jsseFileName); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "create metric file failed", e); - return; - } - executorService = Executors.newScheduledThreadPool(1); - executorService.scheduleAtFixedRate(this::writeMetricData, JSSE_CONFIG.getDelayTime(), JSSE_CONFIG.getPeriod(), - TimeUnit.MILLISECONDS); - } - - @Override - public void stop() { - if (executorService != null) { - executorService.shutdown(); - } - } - - /** - * 把指标数据写进文件里 - */ - private void writeMetricData() { - if (JsseManager.getJsseLinkMap().isEmpty() && JsseManager.getJsseRpcMap().isEmpty()) { - return; - } - try (RandomAccessFile raf = new RandomAccessFile(jsseFileName, "rw"); - FileLock lock = raf.getChannel().lock()) { - raf.seek(raf.length()); - writeLinkData(raf); - writeRpcData(raf); - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Exception writing indicator data to file.", e); - } - } - - /** - * 写连接数据 - * - * @param raf 文件连接 - * @throws IOException 文件写入异常 - */ - private void writeLinkData(RandomAccessFile raf) throws IOException { - for (Entry entry : JsseManager.getJsseLinkMap().entrySet()) { - JsseLinkInfo oldJsseInfo = entry.getValue(); - if (oldJsseInfo.getSentMessages().get() == 0 && oldJsseInfo.getReceiveMessages().get() == 0) { - continue; - } - JsseLinkInfo jsseLinkInfo = JSONObject.parseObject(JSONObject.toJSONString(oldJsseInfo), - JsseLinkInfo.class); - raf.write(String.format(Locale.ROOT, "|sermant_l7_link|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s", - pid, jsseLinkInfo.getClientIp(), jsseLinkInfo.getServerIp(), jsseLinkInfo.getServerPort(), - jsseLinkInfo.getL4Role(), jsseLinkInfo.getL7Role(), jsseLinkInfo.getProtocol(), - jsseLinkInfo.isEnableSsl() ? Constants.SSL_OPEN : Constants.SSL_CLOSE, - jsseLinkInfo.getSentBytes().get(), jsseLinkInfo.getSentMessages().get(), - jsseLinkInfo.getReceiveBytes().get(), jsseLinkInfo.getReceiveMessages().get() - ).getBytes(Charset.defaultCharset())); - raf.write(LINE_BREAK.getBytes(Charset.defaultCharset())); - oldJsseInfo.getReceiveMessages().getAndAdd(-jsseLinkInfo.getReceiveMessages().get()); - oldJsseInfo.getReceiveBytes().getAndAdd(-jsseLinkInfo.getReceiveBytes().get()); - oldJsseInfo.getSentMessages().getAndAdd(-jsseLinkInfo.getSentMessages().get()); - oldJsseInfo.getSentBytes().getAndAdd(-jsseLinkInfo.getSentBytes().get()); - } - } - - /** - * 写RPC数据 - * - * @param raf 文件连接 - * @throws IOException 文件写入异常 - */ - private void writeRpcData(RandomAccessFile raf) throws IOException { - for (Entry entry : JsseManager.getJsseRpcMap().entrySet()) { - JsseRpcInfo oldJsseInfo = entry.getValue(); - if (oldJsseInfo.getReqCount().get() == 0 && oldJsseInfo.getResponseCount().get() == 0) { - continue; - } - JsseRpcInfo jsseRpcInfo = JSONObject.parseObject(JSONObject.toJSONString(oldJsseInfo), - JsseRpcInfo.class); - BigDecimal reqCount = new BigDecimal(jsseRpcInfo.getReqCount().get()); - BigDecimal resCount = new BigDecimal(jsseRpcInfo.getResponseCount().get()); - BigDecimal errorCount = new BigDecimal(jsseRpcInfo.getReqErrorCount().get()); - BigDecimal errorRatio = errorCount.divide(reqCount, ENABLE_NUM, RoundingMode.HALF_UP); - BigDecimal sumLatency = new BigDecimal(jsseRpcInfo.getSumLatency().get()); - BigDecimal avgLatency = sumLatency.divide(reqCount, ENABLE_NUM, RoundingMode.HALF_UP); - BigDecimal reqThroughout = reqCount.multiply(MULTIPLYING_POWER).divide(avgLatency, ENABLE_NUM, - RoundingMode.HALF_UP); - BigDecimal resThroughout = resCount.multiply(MULTIPLYING_POWER).divide(avgLatency, ENABLE_NUM, - RoundingMode.HALF_UP); - String latencyHistogram = getLatencyHistogram(jsseRpcInfo.getLatencyList()); - raf.write(String.format(Locale.ROOT, "|sermant_l7_rpc|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s" - + "|%s|%s|%s", pid, jsseRpcInfo.getClientIp(), jsseRpcInfo.getServerIp(), - jsseRpcInfo.getServerPort(), jsseRpcInfo.getL4Role(), jsseRpcInfo.getL7Role(), - jsseRpcInfo.getProtocol(), jsseRpcInfo.isEnableSsl() ? Constants.SSL_OPEN : Constants.SSL_CLOSE, - reqThroughout, resThroughout, reqCount, resCount, avgLatency, latencyHistogram, - sumLatency, errorRatio, errorCount).getBytes(Charset.defaultCharset())); - raf.write(LINE_BREAK.getBytes(Charset.defaultCharset())); - oldJsseInfo.getReqErrorCount().getAndAdd(-jsseRpcInfo.getReqErrorCount().get()); - oldJsseInfo.getSumLatency().getAndAdd(-jsseRpcInfo.getSumLatency().get()); - oldJsseInfo.getResponseCount().getAndAdd(-jsseRpcInfo.getResponseCount().get()); - oldJsseInfo.getReqCount().getAndAdd(-jsseRpcInfo.getReqCount().get()); - oldJsseInfo.getLatencyList().removeAll(jsseRpcInfo.getLatencyList()); - } - } - - /** - * 初始化机器Id - */ - private void initMachineId() { - try { - ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "dmidecode -s system-uuid"); - Process process = processBuilder.start(); - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { - String line; - while ((line = bufferedReader.readLine()) != null) { - if (StringUtils.isExist(line)) { - machineId = line.trim(); - break; - } - } - } - int exitCode = process.waitFor(); - if (exitCode != 0) { - LOGGER.log(Level.WARNING, "Failed to execute command: dmidecode -s system-uuid."); - } - } catch (IOException e) { - LOGGER.log(Level.WARNING, "Error executing command: dmidecode -s system-uuid.", e); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - LOGGER.log(Level.WARNING, "Command execution interrupted.", e); - } - machineId = StringUtils.EMPTY; - } - - /** - * 初始化容器信息 - */ - private void initContainerInfo() { - try { - containerIp = InetAddress.getLocalHost().getHostAddress(); - ProcessBuilder processBuilder = new ProcessBuilder("cat", "/proc/self/cgroup"); - Process process = processBuilder.start(); - BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - String line; - while ((line = reader.readLine()) != null) { - if (line.contains("docker") || line.contains("kubepods")) { - String[] parts = line.split("/"); - containerId = parts[parts.length - 1]; - containerName = parts[parts.length - CONTAINER_NAME_INDEX]; - break; - } - } - int exitCode = process.waitFor(); - if (exitCode != 0) { - LOGGER.log(Level.WARNING, "Failed to get container ID."); - } - } catch (IOException | InterruptedException e) { - LOGGER.log(Level.WARNING, "Failed to get container ID.", e); - } - containerId = StringUtils.EMPTY; - } - - /** - * 创建文件 - * - * @param filePath 文件路径 - * @param fileName 文件名称 - * @throws IOException 文件创建异常 - */ - private void createTmpFile(String filePath, String fileName) throws IOException { - File dir = new File(filePath); - if (!dir.exists()) { - dir.mkdirs(); - } - File file = new File(fileName); - if (!file.exists()) { - file.createNewFile(); - } - } - - /** - * 生成时延直方图 - * - * @param latencyList 时延信息 - * @return 时延直方图 - */ - private String getLatencyHistogram(List latencyList) { - if (latencyList == null || latencyList.isEmpty()) { - return StringUtils.EMPTY; - } - StringBuilder latencyHistogram = new StringBuilder(String.valueOf(RANGE.length - 1)); - for (int index = 0; index < RANGE.length - 1; index++) { - final int latencyIndex = index; - latencyHistogram.append(" ").append(RANGE[index + 1]).append(" ").append((int) latencyList.stream().filter( - v -> v > RANGE[latencyIndex] && v < RANGE[latencyIndex + 1]).count()); - } - return latencyHistogram.toString(); - } -} diff --git a/sermant-plugins/sermant-jsse/config/config.yaml b/sermant-plugins/sermant-metrics/config/config.yaml similarity index 61% rename from sermant-plugins/sermant-jsse/config/config.yaml rename to sermant-plugins/sermant-metrics/config/config.yaml index 5158b5c98d..7fa5a4a442 100644 --- a/sermant-plugins/sermant-jsse/config/config.yaml +++ b/sermant-plugins/sermant-metrics/config/config.yaml @@ -1,6 +1,6 @@ -jsse.config: +metrics.config: enable: false delayTime: 1000 period: 1000 filePath: /tmp/java-data- - fileName: jsse-metric.txt + fileName: sermant-metrics.txt diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/pom.xml b/sermant-plugins/sermant-metrics/metrics-plugin/pom.xml similarity index 93% rename from sermant-plugins/sermant-jsse/jsse-plugin/pom.xml rename to sermant-plugins/sermant-metrics/metrics-plugin/pom.xml index 597feb0b3f..524c6db844 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/pom.xml +++ b/sermant-plugins/sermant-metrics/metrics-plugin/pom.xml @@ -4,15 +4,15 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> com.huaweicloud.sermant - sermant-jsse + sermant-metrics 1.0.0 4.0.0 - jsse-plugin + metrics-plugin - jsse-plugin - The plugin module of jsse. + metrics-plugin + The plugin module of metrics. false diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/common/Constants.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/common/Constants.java similarity index 64% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/common/Constants.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/common/Constants.java index 7d0da26472..f30b8fbc48 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/common/Constants.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/common/Constants.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huawei.jsse.common; +package com.huawei.metrics.common; /** * 常量类 @@ -98,6 +98,56 @@ public class Constants { */ public static final String SSL_CLOSE = "no_ssl"; + /** + * 消费者标识 + */ + public static final String CONSUMER_SIDE = "consumer"; + + /** + * 提供者标识 + */ + public static final String PROVIDER_SIDE = "provider"; + + /** + * 客户端 服务端标识的KEY + */ + public static final String SIDE_KEY = "side"; + + /** + * 字符串写下标保存到context局部变量中的key + */ + public static final String WRITE_INDEX_KEY = "writeIndex"; + + /** + * 字符串读下标保存到context局部变量中的key + */ + public static final String READ_INDEX_KEY = "readIndex"; + + /** + * 连接信息保存到context局部变量中的key + */ + public static final String LINK_INFO_KEY = "linkInfo"; + + /** + * RPC信息保存到context局部变量中的key + */ + public static final String RPC_INFO_KEY = "rpcInfo"; + + /** + * 服务调用开始时间保存到context局部变量中的key + */ + public static final String START_TIME_KEY = "startTime"; + + /** + * 异常线程ID + */ + public static final String EXCEPTION_PID = "-1"; + + /** + * 指标值的连接符 + */ + public static final String METRICS_LINK = "|"; + private Constants() { } } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/config/JsseConfig.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/config/MetricsConfig.java similarity index 92% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/config/JsseConfig.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/config/MetricsConfig.java index 0e7b0e13ba..abf44aa2a2 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/config/JsseConfig.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/config/MetricsConfig.java @@ -14,19 +14,19 @@ * limitations under the License. */ -package com.huawei.jsse.config; +package com.huawei.metrics.config; import com.huaweicloud.sermant.core.config.common.ConfigTypeKey; import com.huaweicloud.sermant.core.plugin.config.PluginConfig; /** - * JSSE配置类 + * Metrics配置类 * * @author zhp * @since 2023-10-17 */ -@ConfigTypeKey(value = "jsse.config") -public class JsseConfig implements PluginConfig { +@ConfigTypeKey(value = "metrics.config") +public class MetricsConfig implements PluginConfig { private boolean enable; /** diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/AbstractDeclarer.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/AbstractDeclarer.java similarity index 83% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/AbstractDeclarer.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/AbstractDeclarer.java index e9a752ad75..3033eb7313 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/AbstractDeclarer.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/AbstractDeclarer.java @@ -14,9 +14,9 @@ * limitations under the License. */ -package com.huawei.jsse.declarer; +package com.huawei.metrics.declarer; -import com.huawei.jsse.config.JsseConfig; +import com.huawei.metrics.config.MetricsConfig; import com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer; import com.huaweicloud.sermant.core.plugin.agent.declarer.SuperTypeDeclarer; @@ -29,7 +29,7 @@ * @since 2023-10-17 */ public abstract class AbstractDeclarer implements PluginDeclarer { - private static final JsseConfig JSSE_CONFIG = PluginConfigManager.getPluginConfig(JsseConfig.class); + private static final MetricsConfig METRICS_CONFIG = PluginConfigManager.getPluginConfig(MetricsConfig.class); @Override public SuperTypeDeclarer[] getSuperTypeDeclarers() { @@ -38,6 +38,6 @@ public SuperTypeDeclarer[] getSuperTypeDeclarers() { @Override public boolean isEnabled() { - return JSSE_CONFIG.isEnable(); + return METRICS_CONFIG.isEnable(); } } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/DecodeDeclarer.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/ExchangeCodecDeclarer.java similarity index 59% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/DecodeDeclarer.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/ExchangeCodecDeclarer.java index 89229f762e..6667b1f27e 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/DecodeDeclarer.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/ExchangeCodecDeclarer.java @@ -14,27 +14,31 @@ * limitations under the License. */ -package com.huawei.jsse.declarer.alibaba; +package com.huawei.metrics.declarer.alibaba; -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.alibaba.CodecInterceptor; +import com.huawei.metrics.declarer.AbstractDeclarer; +import com.huawei.metrics.interceptor.alibaba.ExchangeCodecInterceptor; 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; /** - * dubbo报文解码增强声明 + * dubbo报文转码、解码增强声明 * * @author zhp * @since 2023-10-17 */ -public class DecodeDeclarer extends AbstractDeclarer { +public class ExchangeCodecDeclarer extends AbstractDeclarer { private static final String ENHANCE_CLASS = "com.alibaba.dubbo.remoting.exchange.codec.ExchangeCodec"; - private static final String METHODS_NAME = "decode"; + private static final String ENCODE_METHODS_NAMES = "encode"; - private static final int PARAM_COUNT = 2; + private static final String DECODE_METHODS_NAMES = "decode"; + + private static final int ENCODE_PARAM_COUNT = 3; + + private static final int DECODE_PARAM_COUNT = 2; @Override public ClassMatcher getClassMatcher() { @@ -43,7 +47,10 @@ public ClassMatcher getClassMatcher() { @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME) - .and(MethodMatcher.paramCountEquals(PARAM_COUNT)), new CodecInterceptor())}; + return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(ENCODE_METHODS_NAMES) + .and(MethodMatcher.paramCountEquals(ENCODE_PARAM_COUNT)), new ExchangeCodecInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameEquals(DECODE_METHODS_NAMES) + .and(MethodMatcher.paramCountEquals(DECODE_PARAM_COUNT)), new ExchangeCodecInterceptor()) + }; } } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/EncodeDeclarer.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/MonitorFilterDeclarer.java similarity index 76% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/EncodeDeclarer.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/MonitorFilterDeclarer.java index a759a88864..78f632730e 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/alibaba/EncodeDeclarer.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/alibaba/MonitorFilterDeclarer.java @@ -14,25 +14,25 @@ * limitations under the License. */ -package com.huawei.jsse.declarer.alibaba; +package com.huawei.metrics.declarer.alibaba; -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.alibaba.CodecInterceptor; +import com.huawei.metrics.declarer.AbstractDeclarer; +import com.huawei.metrics.interceptor.alibaba.MonitorFilterInterceptor; 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; /** - * dubbo报文编码增强声明 + * dubbo监控过滤器增强声明 * * @author zhp * @since 2023-10-17 */ -public class EncodeDeclarer extends AbstractDeclarer { - private static final String ENHANCE_CLASS = "com.alibaba.dubbo.remoting.exchange.codec.ExchangeCodec"; +public class MonitorFilterDeclarer extends AbstractDeclarer { + private static final String ENHANCE_CLASS = "com.alibaba.dubbo.monitor.support.MonitorFilter"; - private static final String METHODS_NAME = "encode"; + private static final String METHODS_NAME = "invoke"; @Override public ClassMatcher getClassMatcher() { @@ -42,6 +42,6 @@ public ClassMatcher getClassMatcher() { @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME), - new CodecInterceptor())}; + new MonitorFilterInterceptor())}; } } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/DecodeDeclarer.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/ExchangeCodecDeclarer.java similarity index 59% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/DecodeDeclarer.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/ExchangeCodecDeclarer.java index c7522dcc3f..d0c9e62e1f 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/DecodeDeclarer.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/ExchangeCodecDeclarer.java @@ -14,27 +14,31 @@ * limitations under the License. */ -package com.huawei.jsse.declarer.apache; +package com.huawei.metrics.declarer.apache; -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.apache.CodecInterceptor; +import com.huawei.metrics.declarer.AbstractDeclarer; +import com.huawei.metrics.interceptor.apache.ExchangeCodecInterceptor; 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; /** - * dubbo报文解码增强声明 + * dubbo报文转码、解码增强声明 * * @author zhp * @since 2023-10-17 */ -public class DecodeDeclarer extends AbstractDeclarer { +public class ExchangeCodecDeclarer extends AbstractDeclarer { private static final String ENHANCE_CLASS = "org.apache.dubbo.remoting.exchange.codec.ExchangeCodec"; - private static final String METHODS_NAME = "decode"; + private static final String ENCODE_METHODS_NAMES = "encode"; - private static final int PARAM_COUNT = 2; + private static final String DECODE_METHODS_NAMES = "decode"; + + private static final int ENCODE_PARAM_COUNT = 3; + + private static final int DECODE_PARAM_COUNT = 2; @Override public ClassMatcher getClassMatcher() { @@ -43,7 +47,10 @@ public ClassMatcher getClassMatcher() { @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { - return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME) - .and(MethodMatcher.paramCountEquals(PARAM_COUNT)), new CodecInterceptor())}; + return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(ENCODE_METHODS_NAMES) + .and(MethodMatcher.paramCountEquals(ENCODE_PARAM_COUNT)), new ExchangeCodecInterceptor()), + InterceptDeclarer.build(MethodMatcher.nameEquals(DECODE_METHODS_NAMES) + .and(MethodMatcher.paramCountEquals(DECODE_PARAM_COUNT)), new ExchangeCodecInterceptor()) + }; } } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/EncodeDeclarer.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/MonitorFilterDeclarer.java similarity index 76% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/EncodeDeclarer.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/MonitorFilterDeclarer.java index ddc62c8ea0..21b2f936b2 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/declarer/apache/EncodeDeclarer.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/declarer/apache/MonitorFilterDeclarer.java @@ -14,25 +14,25 @@ * limitations under the License. */ -package com.huawei.jsse.declarer.apache; +package com.huawei.metrics.declarer.apache; -import com.huawei.jsse.declarer.AbstractDeclarer; -import com.huawei.jsse.interceptor.apache.CodecInterceptor; +import com.huawei.metrics.declarer.AbstractDeclarer; +import com.huawei.metrics.interceptor.apache.MonitorFilterInterceptor; 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; /** - * dubbo报文编码增强声明 + * dubbo监控过滤器增强声明 * * @author zhp * @since 2023-10-17 */ -public class EncodeDeclarer extends AbstractDeclarer { - private static final String ENHANCE_CLASS = "org.apache.dubbo.remoting.exchange.codec.ExchangeCodec"; +public class MonitorFilterDeclarer extends AbstractDeclarer { + private static final String ENHANCE_CLASS = "org.apache.dubbo.monitor.support.MonitorFilter"; - private static final String METHODS_NAME = "encode"; + private static final String METHODS_NAME = "invoke"; @Override public ClassMatcher getClassMatcher() { @@ -42,6 +42,6 @@ public ClassMatcher getClassMatcher() { @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { return new InterceptDeclarer[]{InterceptDeclarer.build(MethodMatcher.nameEquals(METHODS_NAME), - new CodecInterceptor())}; + new MonitorFilterInterceptor())}; } } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseInfo.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsInfo.java similarity index 97% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseInfo.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsInfo.java index f7bf783530..bc5c035558 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseInfo.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsInfo.java @@ -14,15 +14,15 @@ * limitations under the License. */ -package com.huawei.jsse.entity; +package com.huawei.metrics.entity; /** - * jsse公共指标信息 + * 公共指标信息 * * @author zhp * @since 2023-10-18 */ -public class JsseInfo { +public class MetricsInfo { /** * 进程Id */ diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseLinkInfo.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsLinkInfo.java similarity index 78% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseLinkInfo.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsLinkInfo.java index f6bd3346e5..23657af56d 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseLinkInfo.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsLinkInfo.java @@ -14,26 +14,27 @@ * limitations under the License. */ -package com.huawei.jsse.entity; +package com.huawei.metrics.entity; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; /** - * Jsse连接信息 + * 连接信息 * * @author zhp * @since 2023-10-17 */ -public class JsseLinkInfo extends JsseInfo { +public class MetricsLinkInfo extends MetricsInfo { /** * 发送的字节数 */ - private AtomicInteger sentBytes = new AtomicInteger(); + private AtomicLong sentBytes = new AtomicLong(); /** * 接收的字节数 */ - private AtomicInteger receiveBytes = new AtomicInteger(); + private AtomicLong receiveBytes = new AtomicLong(); /** * 发送的报文数 @@ -45,19 +46,19 @@ public class JsseLinkInfo extends JsseInfo { */ private AtomicInteger receiveMessages = new AtomicInteger(); - public AtomicInteger getSentBytes() { + public AtomicLong getSentBytes() { return sentBytes; } - public void setSentBytes(AtomicInteger sentBytes) { + public void setSentBytes(AtomicLong sentBytes) { this.sentBytes = sentBytes; } - public AtomicInteger getReceiveBytes() { + public AtomicLong getReceiveBytes() { return receiveBytes; } - public void setReceiveBytes(AtomicInteger receiveBytes) { + public void setReceiveBytes(AtomicLong receiveBytes) { this.receiveBytes = receiveBytes; } diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseRpcInfo.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsRpcInfo.java similarity index 95% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseRpcInfo.java rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsRpcInfo.java index 4a731e8df7..f961d31e9f 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/java/com/huawei/jsse/entity/JsseRpcInfo.java +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/entity/MetricsRpcInfo.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.huawei.jsse.entity; +package com.huawei.metrics.entity; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -22,12 +22,12 @@ import java.util.concurrent.atomic.AtomicLong; /** - * JSSE RPC调用信息 + * RPC调用信息 * * @author zhp * @since 2023-10-17 */ -public class JsseRpcInfo extends JsseInfo { +public class MetricsRpcInfo extends MetricsInfo { private AtomicInteger reqCount = new AtomicInteger(); private AtomicInteger responseCount = new AtomicInteger(); diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractCodecInterceptor.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractCodecInterceptor.java new file mode 100644 index 0000000000..c677e956a4 --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractCodecInterceptor.java @@ -0,0 +1,198 @@ +/* + * 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.entity.MetricsLinkInfo; +import com.huawei.metrics.manager.MetricsManager; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; +import com.huaweicloud.sermant.core.utils.StringUtils; + +import java.net.InetSocketAddress; +import java.util.HashSet; +import java.util.Set; + +/** + * dubbo报文加解码拦截器 + * + * @author zhp + * @since 2023-10-17 + */ +public abstract class AbstractCodecInterceptor implements Interceptor { + private static final Set TCP_PROTOCOL = new HashSet() { + { + add("dubbo"); + add("rmi"); + add("http"); + add("https"); + } + }; + + private static final String ENCODE_METHOD_NAME = "encode"; + + @Override + public ExecuteContext before(ExecuteContext context) { + if (!isValid(context)) { + return context; + } + MetricsLinkInfo metricsLinkInfo = initLinkInfo(context); + initIndexInfo(context); + context.setLocalFieldValue(Constants.LINK_INFO_KEY, metricsLinkInfo); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + if (!isValid(context)) { + return context; + } + fillMessageInfo(context); + return context; + } + + @Override + public ExecuteContext onThrow(ExecuteContext context) { + return context; + } + + /** + * 是否为客户端 + * + * @param side 客户端、服务端标识 + * @return 是否为客户端的结果 + */ + private boolean isClientSide(String side) { + return Constants.CONSUMER_SIDE.equals(side); + } + + /** + * 校验 + * + * @param context 上下文信息 + * @return 校验结果 + */ + public abstract boolean isValid(ExecuteContext context); + + /** + * 初始化连接信息 + * + * @param context 上下文信息 + * @return 连接信息 + */ + public abstract MetricsLinkInfo initLinkInfo(ExecuteContext context); + + /** + * 初始化连接信息 + * + * @param localAddress 本地地址 + * @param remoteAddress 远程地址 + * @param side 消费端或者服务端标识 + * @param sslEnable 是否开启SSL + * @param protocol 协议信息 + * @return 连接信息 + */ + public MetricsLinkInfo initLinkInfo(InetSocketAddress localAddress, InetSocketAddress remoteAddress, + String side, boolean sslEnable, String protocol) { + String metricsKey = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getHostName() + + Constants.CONNECT + remoteAddress.getPort(); + MetricsLinkInfo metricsLinkInfo = MetricsManager.getLinkInfo(metricsKey); + if (!StringUtils.isEmpty(metricsLinkInfo.getClientIp())) { + return metricsLinkInfo; + } + metricsLinkInfo.setProtocol(protocol); + if (isClientSide(side)) { + initAddressAndRole(metricsLinkInfo, Constants.CLIENT_ROLE, localAddress, remoteAddress); + } else { + initAddressAndRole(metricsLinkInfo, Constants.SERVER_ROLE, remoteAddress, localAddress); + } + if (TCP_PROTOCOL.contains(metricsLinkInfo.getProtocol())) { + metricsLinkInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + metricsLinkInfo.getL7Role()); + } else { + metricsLinkInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + metricsLinkInfo.getL7Role()); + } + metricsLinkInfo.setEnableSsl(sslEnable); + return metricsLinkInfo; + } + + /** + * 初始化地址和角色信息 + * + * @param metricsLinkInfo 连接信息 + * @param role 角色信息 + * @param clientAddress 客户端地址 + * @param serverAddress 服务端地址 + */ + private void initAddressAndRole(MetricsLinkInfo metricsLinkInfo, String role, InetSocketAddress clientAddress, + InetSocketAddress serverAddress) { + metricsLinkInfo.setL7Role(role); + metricsLinkInfo.setClientIp(clientAddress.getAddress().getHostAddress()); + metricsLinkInfo.setServerIp(serverAddress.getAddress().getHostAddress()); + metricsLinkInfo.setServerPort(StringUtils.getString(serverAddress.getPort())); + } + + /** + * 初始化渠道缓存的下标信息 + * + * @param context 上下文信息 + */ + public abstract void initIndexInfo(ExecuteContext context); + + /** + * 填充报文信息 + * + * @param currentWriteIndex 当前读索引下标 + * @param currentReadIndex 当前写索引下标 + * @param context 上下文信息 + */ + protected void fillMessageInfo(int currentWriteIndex, int currentReadIndex, ExecuteContext context) { + Object linkInfoObject = context.getLocalFieldValue(Constants.LINK_INFO_KEY); + if (!(linkInfoObject instanceof MetricsLinkInfo)) { + return; + } + MetricsLinkInfo metricsLinkInfo = (MetricsLinkInfo) linkInfoObject; + if (StringUtils.equals(context.getMethod().getName(), ENCODE_METHOD_NAME)) { + Object writeIndex = context.getLocalFieldValue(Constants.WRITE_INDEX_KEY); + if (!(writeIndex instanceof Integer)) { + return; + } + int addWriteIndex = currentWriteIndex - Integer.parseInt(StringUtils.getString(writeIndex)); + if (addWriteIndex > 0) { + metricsLinkInfo.getSentBytes().addAndGet(addWriteIndex); + metricsLinkInfo.getSentMessages().incrementAndGet(); + } + return; + } + Object readIndex = context.getLocalFieldValue(Constants.READ_INDEX_KEY); + if (!(readIndex instanceof Integer)) { + return; + } + int addReadIndex = currentReadIndex - Integer.parseInt(StringUtils.getString(readIndex)); + if (addReadIndex > 0) { + metricsLinkInfo.getReceiveBytes().addAndGet(addReadIndex); + metricsLinkInfo.getReceiveMessages().incrementAndGet(); + } + } + + /** + * 填充报文信息 + * + * @param context 上下文信息 + */ + protected abstract void fillMessageInfo(ExecuteContext context); +} diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractFilterInterceptor.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractFilterInterceptor.java new file mode 100644 index 0000000000..ad3ff95117 --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/AbstractFilterInterceptor.java @@ -0,0 +1,159 @@ +/* + * 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.entity.MetricsRpcInfo; +import com.huawei.metrics.manager.MetricsManager; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.Interceptor; +import com.huaweicloud.sermant.core.utils.StringUtils; + +import java.net.InetSocketAddress; + +/** + * dubbo服务过滤器拦截器 + * + * @author zhp + * @since 2023-10-17 + */ +public abstract class AbstractFilterInterceptor implements Interceptor { + @Override + public ExecuteContext before(ExecuteContext context) { + if (!isValid(context)) { + return context; + } + long startTime = System.nanoTime(); + MetricsRpcInfo metricsRpcInfo = initRpcInfo(context); + metricsRpcInfo.getReqCount().incrementAndGet(); + context.setLocalFieldValue(Constants.START_TIME_KEY, startTime); + context.setLocalFieldValue(Constants.RPC_INFO_KEY, metricsRpcInfo); + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + if (!isValid(context)) { + return context; + } + Object startTimeObject = context.getLocalFieldValue(Constants.START_TIME_KEY); + if (!(startTimeObject instanceof Long)) { + return context; + } + Object metrcisObject = context.getLocalFieldValue(Constants.RPC_INFO_KEY); + if (!(metrcisObject instanceof MetricsRpcInfo)) { + return context; + } + long startTime = Long.parseLong(StringUtils.getString(startTimeObject)); + MetricsRpcInfo metricsRpcInfo = (MetricsRpcInfo) metrcisObject; + metricsRpcInfo.getResponseCount().incrementAndGet(); + long latency = System.nanoTime() - startTime; + metricsRpcInfo.getSumLatency().addAndGet(latency); + metricsRpcInfo.getLatencyList().add(latency); + return context; + } + + @Override + public ExecuteContext onThrow(ExecuteContext context) { + if (!isValid(context)) { + return context; + } + Object metrcisObject = context.getLocalFieldValue(Constants.RPC_INFO_KEY); + if (!(metrcisObject instanceof MetricsRpcInfo)) { + return context; + } + MetricsRpcInfo metricsRpcInfo = (MetricsRpcInfo) metrcisObject; + metricsRpcInfo.getReqErrorCount().incrementAndGet(); + return context; + } + + /** + * 是否为客户端 + * + * @param side 客户端服务端标识 + * @return 是否为客户端的结果 + */ + private boolean isClientSide(String side) { + return Constants.CONSUMER_SIDE.equals(side); + } + + /** + * 校验 + * + * @param context 上下文信息 + * @return 校验结果 + */ + public abstract boolean isValid(ExecuteContext context); + + /** + * 初始化调用信息 + * + * @param context 上下文信息 + * @return 调用信息 + */ + public abstract MetricsRpcInfo initRpcInfo(ExecuteContext context); + + /** + * 初始化PRC信息 + * + * @param localAddress 本地地址 + * @param remoteAddress 远程地址 + * @param side 消费端或者服务端标识 + * @param sslEnable 是否开启SSL + * @param protocol 协议信息 + * @return 填充的RPC信息 + */ + public MetricsRpcInfo initRpcInfo(InetSocketAddress localAddress, InetSocketAddress remoteAddress, + String side, boolean sslEnable, String protocol) { + String metricsKey = localAddress.getHostName() + Constants.CONNECT + remoteAddress.getHostName() + + Constants.CONNECT + remoteAddress.getPort(); + MetricsRpcInfo metricsRpcInfo = MetricsManager.getRpcInfo(metricsKey); + if (!StringUtils.isEmpty(metricsRpcInfo.getClientIp())) { + return metricsRpcInfo; + } + metricsRpcInfo.setProtocol(protocol); + if (isClientSide(side)) { + initAddressAndRole(metricsRpcInfo, localAddress, remoteAddress, Constants.CLIENT_ROLE); + } else { + initAddressAndRole(metricsRpcInfo, remoteAddress, localAddress, Constants.SERVER_ROLE); + } + if (Constants.TCP_PROTOCOLS.contains(metricsRpcInfo.getProtocol())) { + metricsRpcInfo.setL4Role(Constants.TCP_PROTOCOL + Constants.CONNECT + metricsRpcInfo.getL7Role()); + } else { + metricsRpcInfo.setL4Role(Constants.UDP_PROTOCOL + Constants.CONNECT + metricsRpcInfo.getL7Role()); + } + metricsRpcInfo.setEnableSsl(sslEnable); + return metricsRpcInfo; + } + + /** + * 初始化地址信息和角色 + * + * @param metricsRpcInfo RPC信息 + * @param clientAddress 客户端地址信息 + * @param serverAddress 服务端地址信息 + * @param role 角色信息 + */ + private void initAddressAndRole(MetricsRpcInfo metricsRpcInfo, InetSocketAddress clientAddress, + InetSocketAddress serverAddress, String role) { + metricsRpcInfo.setClientIp(clientAddress.getHostName()); + metricsRpcInfo.setServerIp(serverAddress.getHostName()); + metricsRpcInfo.setServerPort(StringUtils.getString(serverAddress.getPort())); + metricsRpcInfo.setL7Role(role); + } +} diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/ExchangeCodecInterceptor.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/ExchangeCodecInterceptor.java new file mode 100644 index 0000000000..596ef3be1b --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/ExchangeCodecInterceptor.java @@ -0,0 +1,74 @@ +/* + * 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.alibaba; + +import com.huawei.metrics.common.Constants; +import com.huawei.metrics.entity.MetricsLinkInfo; +import com.huawei.metrics.interceptor.AbstractCodecInterceptor; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.remoting.Channel; +import com.alibaba.dubbo.remoting.buffer.ChannelBuffer; + +/** + * dubbo报文转码、解码拦截器 + * + * @author zhp + * @since 2023-10-17 + */ +public class ExchangeCodecInterceptor extends AbstractCodecInterceptor { + private static final int PARAM_COUNT = 2; + + @Override + public boolean isValid(ExecuteContext context) { + Object[] arguments = context.getArguments(); + if (arguments == null || arguments.length < PARAM_COUNT) { + return false; + } + if (!(arguments[0] instanceof Channel)) { + return false; + } + if (!(arguments[1] instanceof ChannelBuffer)) { + return false; + } + return ((Channel) arguments[0]).getUrl() != null; + } + + @Override + public MetricsLinkInfo initLinkInfo(ExecuteContext context) { + Channel channel = (Channel) context.getArguments()[0]; + URL url = channel.getUrl(); + boolean sslEnable = Boolean.parseBoolean(url.getParameter(Constants.SSL_ENABLE)); + return initLinkInfo(channel.getLocalAddress(), channel.getRemoteAddress(), + url.getParameter(Constants.SIDE_KEY), sslEnable, url.getProtocol()); + } + + @Override + public void initIndexInfo(ExecuteContext context) { + ChannelBuffer buffer = (ChannelBuffer) context.getArguments()[1]; + context.setLocalFieldValue(Constants.WRITE_INDEX_KEY, buffer.writerIndex()); + context.setLocalFieldValue(Constants.READ_INDEX_KEY, buffer.readerIndex()); + } + + @Override + protected void fillMessageInfo(ExecuteContext context) { + ChannelBuffer buffer = (ChannelBuffer) context.getArguments()[1]; + fillMessageInfo(buffer.writerIndex(), buffer.readerIndex(), context); + } +} diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/MonitorFilterInterceptor.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/MonitorFilterInterceptor.java new file mode 100644 index 0000000000..c0afcbffeb --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/alibaba/MonitorFilterInterceptor.java @@ -0,0 +1,50 @@ +/* + * 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.alibaba; + +import com.huawei.metrics.common.Constants; +import com.huawei.metrics.entity.MetricsRpcInfo; +import com.huawei.metrics.interceptor.AbstractFilterInterceptor; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.rpc.RpcContext; + +/** + * dubbo服务监控过滤器拦截器 + * + * @author zhp + * @since 2023-10-17 + */ +public class MonitorFilterInterceptor extends AbstractFilterInterceptor { + @Override + public boolean isValid(ExecuteContext context) { + RpcContext rpcContext = RpcContext.getContext(); + return rpcContext != null && rpcContext.getLocalAddress() != null && rpcContext.getRemoteAddress() != null + && rpcContext.getUrl() != null; + } + + @Override + public MetricsRpcInfo initRpcInfo(ExecuteContext context) { + RpcContext rpcContext = RpcContext.getContext(); + URL url = rpcContext.getUrl(); + boolean sslEnable = Boolean.parseBoolean(url.getParameter(Constants.SSL_ENABLE)); + return initRpcInfo(rpcContext.getLocalAddress(), rpcContext.getRemoteAddress(), + url.getParameter(Constants.SIDE_KEY), sslEnable, url.getProtocol()); + } +} diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/ExchangeCodecInterceptor.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/ExchangeCodecInterceptor.java new file mode 100644 index 0000000000..9004828bab --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/ExchangeCodecInterceptor.java @@ -0,0 +1,74 @@ +/* + * 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.apache; + +import com.huawei.metrics.common.Constants; +import com.huawei.metrics.entity.MetricsLinkInfo; +import com.huawei.metrics.interceptor.AbstractCodecInterceptor; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.remoting.Channel; +import org.apache.dubbo.remoting.buffer.ChannelBuffer; + +/** + * dubbo报文转码、解码拦截器 + * + * @author zhp + * @since 2023-10-17 + */ +public class ExchangeCodecInterceptor extends AbstractCodecInterceptor { + private static final int PARAM_COUNT = 2; + + @Override + public boolean isValid(ExecuteContext context) { + Object[] arguments = context.getArguments(); + if (arguments == null || arguments.length < PARAM_COUNT) { + return false; + } + if (!(arguments[0] instanceof Channel)) { + return false; + } + if (!(arguments[1] instanceof ChannelBuffer)) { + return false; + } + return ((Channel) arguments[0]).getUrl() != null; + } + + @Override + public MetricsLinkInfo initLinkInfo(ExecuteContext context) { + Channel channel = (Channel) context.getArguments()[0]; + URL url = channel.getUrl(); + boolean sslEnable = Boolean.parseBoolean(url.getParameter(Constants.SSL_ENABLE)); + return initLinkInfo(channel.getLocalAddress(), channel.getRemoteAddress(), + url.getParameter(Constants.SIDE_KEY), sslEnable, url.getProtocol()); + } + + @Override + public void initIndexInfo(ExecuteContext context) { + ChannelBuffer buffer = (ChannelBuffer) context.getArguments()[1]; + context.setLocalFieldValue(Constants.WRITE_INDEX_KEY, buffer.writerIndex()); + context.setLocalFieldValue(Constants.READ_INDEX_KEY, buffer.readerIndex()); + } + + @Override + protected void fillMessageInfo(ExecuteContext context) { + ChannelBuffer buffer = (ChannelBuffer) context.getArguments()[1]; + fillMessageInfo(buffer.writerIndex(), buffer.readerIndex(), context); + } +} diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/MonitorFilterInterceptor.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/MonitorFilterInterceptor.java new file mode 100644 index 0000000000..70aa2efc5f --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/interceptor/apache/MonitorFilterInterceptor.java @@ -0,0 +1,50 @@ +/* + * 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.apache; + +import com.huawei.metrics.common.Constants; +import com.huawei.metrics.entity.MetricsRpcInfo; +import com.huawei.metrics.interceptor.AbstractFilterInterceptor; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.rpc.RpcContext; + +/** + * dubbo服务监控过滤器增强类 + * + * @author zhp + * @since 2023-10-17 + */ +public class MonitorFilterInterceptor extends AbstractFilterInterceptor { + @Override + public boolean isValid(ExecuteContext context) { + RpcContext rpcContext = RpcContext.getContext(); + return rpcContext != null && rpcContext.getLocalAddress() != null && rpcContext.getRemoteAddress() != null + && rpcContext.getUrl() != null; + } + + @Override + public MetricsRpcInfo initRpcInfo(ExecuteContext context) { + RpcContext rpcContext = RpcContext.getContext(); + URL url = rpcContext.getUrl(); + boolean sslEnable = Boolean.parseBoolean(url.getParameter(Constants.SSL_ENABLE)); + return initRpcInfo(rpcContext.getLocalAddress(), rpcContext.getRemoteAddress(), + url.getParameter(Constants.SIDE_KEY), sslEnable, url.getProtocol()); + } +} diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/manager/MetricsManager.java b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/manager/MetricsManager.java new file mode 100644 index 0000000000..d2010d6a04 --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/java/com/huawei/metrics/manager/MetricsManager.java @@ -0,0 +1,76 @@ +/* + * 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.manager; + +import com.huawei.metrics.entity.MetricsLinkInfo; +import com.huawei.metrics.entity.MetricsRpcInfo; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Metrics管理类 + * + * @author zhp + * @since 2023-10-17 + */ +public class MetricsManager { + private static final Map METRICS_LINK_INFO_MAP = new ConcurrentHashMap<>(); + + private static final Map METRICS_RPC_INFO_MAP = new ConcurrentHashMap<>(); + + private MetricsManager() { + } + + /** + * 获取全部连接信息 + * + * @return 连接信息 + */ + public static Map getLinkInfoMap() { + return METRICS_LINK_INFO_MAP; + } + + /** + * 获取全部RPC信息 + * + * @return 连接信息 + */ + public static Map getRpcInfoMap() { + return METRICS_RPC_INFO_MAP; + } + + /** + * 获取连接信息 + * + * @param key MAP的密钥信息 + * @return 连接信息 + */ + public static MetricsLinkInfo getLinkInfo(String key) { + return METRICS_LINK_INFO_MAP.computeIfAbsent(key, s -> new MetricsLinkInfo()); + } + + /** + * 获取RPC信息 + * + * @param key MAP的密钥信息 + * @return RPC信息 + */ + public static MetricsRpcInfo getRpcInfo(String key) { + return METRICS_RPC_INFO_MAP.computeIfAbsent(key, s -> new MetricsRpcInfo()); + } +} diff --git a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer similarity index 62% rename from sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer rename to sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 078e4d6a3c..67b4daa9be 100644 --- a/sermant-plugins/sermant-jsse/jsse-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -15,11 +15,7 @@ # # -com.huawei.jsse.declarer.alibaba.DecodeDeclarer -com.huawei.jsse.declarer.alibaba.EncodeDeclarer -com.huawei.jsse.declarer.alibaba.HandlerDeclarer -com.huawei.jsse.declarer.alibaba.InvokeDeclarer -com.huawei.jsse.declarer.apache.DecodeDeclarer -com.huawei.jsse.declarer.apache.EncodeDeclarer -com.huawei.jsse.declarer.apache.HandlerDeclarer -com.huawei.jsse.declarer.apache.InvokeDeclarer \ No newline at end of file +com.huawei.metrics.declarer.alibaba.ExchangeCodecDeclarer +com.huawei.metrics.declarer.alibaba.MonitorFilterDeclarer +com.huawei.metrics.declarer.apache.ExchangeCodecDeclarer +com.huawei.metrics.declarer.apache.MonitorFilterDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig new file mode 100644 index 0000000000..a14f635e76 --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.config.PluginConfig @@ -0,0 +1 @@ +com.huawei.metrics.config.MetricsConfig \ No newline at end of file diff --git a/sermant-plugins/sermant-jsse/jsse-service/pom.xml b/sermant-plugins/sermant-metrics/metrics-service/pom.xml similarity index 93% rename from sermant-plugins/sermant-jsse/jsse-service/pom.xml rename to sermant-plugins/sermant-metrics/metrics-service/pom.xml index 47530b656a..b0fcf27666 100644 --- a/sermant-plugins/sermant-jsse/jsse-service/pom.xml +++ b/sermant-plugins/sermant-metrics/metrics-service/pom.xml @@ -3,13 +3,13 @@ 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"> - sermant-jsse + sermant-metrics com.huaweicloud.sermant 1.0.0 4.0.0 - jsse-service + metrics-service false @@ -29,7 +29,7 @@ com.huaweicloud.sermant - jsse-plugin + metrics-plugin ${project.version} provided diff --git a/sermant-plugins/sermant-metrics/metrics-service/src/main/java/com/huawei/metrics/service/MetricsService.java b/sermant-plugins/sermant-metrics/metrics-service/src/main/java/com/huawei/metrics/service/MetricsService.java new file mode 100644 index 0000000000..e33080fece --- /dev/null +++ b/sermant-plugins/sermant-metrics/metrics-service/src/main/java/com/huawei/metrics/service/MetricsService.java @@ -0,0 +1,364 @@ +/* + * 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.service; + +import com.huawei.metrics.common.Constants; +import com.huawei.metrics.config.MetricsConfig; +import com.huawei.metrics.entity.MetricsLinkInfo; +import com.huawei.metrics.entity.MetricsRpcInfo; +import com.huawei.metrics.manager.MetricsManager; + +import com.huaweicloud.sermant.core.common.LoggerFactory; +import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; +import com.huaweicloud.sermant.core.plugin.service.PluginService; +import com.huaweicloud.sermant.core.utils.StringUtils; +import com.huaweicloud.sermant.core.utils.ThreadFactoryUtils; + +import com.alibaba.fastjson.JSONObject; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.RandomAccessFile; +import java.lang.management.ManagementFactory; +import java.lang.management.RuntimeMXBean; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.net.InetAddress; +import java.nio.channels.FileLock; +import java.nio.charset.Charset; +import java.util.List; +import java.util.Map.Entry; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * 指标服务类,上报指标数据 + * + * @author zhp + * @since 2023-10-17 + */ +public class MetricsService implements PluginService { + private static final Logger LOGGER = LoggerFactory.getLogger(); + + private static final MetricsConfig METRICS_CONFIG = PluginConfigManager.getPluginConfig(MetricsConfig.class); + + private static final int CONTAINER_NAME_INDEX = 2; + + private static final int ENABLE_NUM = 2; + + /** + * 换行符。Gopher解析时需要用到 System.lineSeparator()在UNIX返回"\n" 在Windows 返回"\r\n". 因此不能修改为System.lineSeparator(); + */ + private static final String LINE_BREAK = "|\r\n"; + + private static final BigDecimal MS_TO_S = new BigDecimal(1000); + + private static final BigDecimal MS_TO_NS = new BigDecimal(1000000); + + private static final BigDecimal TO_PERCENT = new BigDecimal(100); + + private static final int[] RANGE = {0, 3, 10, 50, 100, 500, 1000, 10000}; + + /** + * 进程Id + */ + private String pid; + + /** + * 进程名称 + */ + private String processName; + + /** + * 机器Id + */ + private String machineId; + + /** + * 容器ID + */ + private String containerId; + + /** + * 容器名称 + */ + private String containerName; + + /** + * 容器IP + */ + private String containerIp; + + /** + * 文件名称 + */ + private String metricsFileName; + + private ScheduledExecutorService executorService; + + @Override + public void start() { + if (!METRICS_CONFIG.isEnable()) { + return; + } + RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); + String name = runtimeMxBean.getName(); + int index = name.indexOf(Constants.PROCESS_NAME_LINK); + if (index != -1) { + pid = name.substring(0, index); + processName = name.substring(index + 1); + } else { + pid = Constants.EXCEPTION_PID; + processName = StringUtils.EMPTY; + } + initMachineId(); + initContainerInfo(); + String filePath = METRICS_CONFIG.getFilePath() + pid; + metricsFileName = filePath + Constants.FILE_PATH_LINK + METRICS_CONFIG.getFileName(); + try { + createTmpFile(filePath, metricsFileName); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Create metric file failed.", e); + return; + } + executorService = Executors.newScheduledThreadPool(1, new ThreadFactoryUtils("metrics")); + executorService.scheduleAtFixedRate(this::writeMetricData, METRICS_CONFIG.getDelayTime(), + METRICS_CONFIG.getPeriod(), TimeUnit.MILLISECONDS); + } + + @Override + public void stop() { + if (executorService != null) { + executorService.shutdown(); + } + } + + /** + * 把指标数据写进文件里 + */ + private void writeMetricData() { + if (MetricsManager.getLinkInfoMap().isEmpty() && MetricsManager.getRpcInfoMap().isEmpty()) { + return; + } + try (RandomAccessFile raf = new RandomAccessFile(metricsFileName, "rw"); + FileLock lock = raf.getChannel().lock()) { + raf.seek(raf.length()); + writeLinkData(raf); + writeRpcData(raf); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Exception writing indicator data to file.", e); + } + } + + /** + * 写连接数据 + * + * @param raf 文件连接 + * @throws IOException 文件写入异常 + */ + private void writeLinkData(RandomAccessFile raf) throws IOException { + StringBuilder stringBuilder = new StringBuilder(Constants.METRICS_LINK + Constants.LINK_HEAD); + for (Entry entry : MetricsManager.getLinkInfoMap().entrySet()) { + MetricsLinkInfo originalLinkInfo = entry.getValue(); + if (originalLinkInfo.getSentMessages().get() == 0 || originalLinkInfo.getReceiveMessages().get() == 0) { + continue; + } + MetricsLinkInfo metricsLinkInfo = JSONObject.parseObject(JSONObject.toJSONString(originalLinkInfo), + MetricsLinkInfo.class); + String metricsInfoStr = stringBuilder.append(Constants.METRICS_LINK).append(pid) + .append(Constants.METRICS_LINK).append(metricsLinkInfo.getClientIp()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getServerIp()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getServerPort()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getL4Role()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getL7Role()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getProtocol()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.isEnableSsl() ? Constants.SSL_OPEN : Constants.SSL_CLOSE) + .append(Constants.METRICS_LINK).append(metricsLinkInfo.getSentBytes().get()) + .append(Constants.METRICS_LINK).append(metricsLinkInfo.getSentMessages().get()) + .append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getReceiveBytes().get()).append(Constants.METRICS_LINK) + .append(metricsLinkInfo.getReceiveMessages().get()).toString(); + raf.write(metricsInfoStr.getBytes(Charset.defaultCharset())); + raf.write(LINE_BREAK.getBytes(Charset.defaultCharset())); + stringBuilder.setLength(0); + originalLinkInfo.getReceiveMessages().getAndAdd(-metricsLinkInfo.getReceiveMessages().get()); + originalLinkInfo.getReceiveBytes().getAndAdd(-metricsLinkInfo.getReceiveBytes().get()); + originalLinkInfo.getSentMessages().getAndAdd(-metricsLinkInfo.getSentMessages().get()); + originalLinkInfo.getSentBytes().getAndAdd(-metricsLinkInfo.getSentBytes().get()); + } + } + + /** + * 写RPC数据 + * + * @param raf 文件连接 + * @throws IOException 文件写入异常 + */ + private void writeRpcData(RandomAccessFile raf) throws IOException { + StringBuilder stringBuilder = new StringBuilder(); + for (Entry entry : MetricsManager.getRpcInfoMap().entrySet()) { + MetricsRpcInfo originalRpcInfo = entry.getValue(); + if (originalRpcInfo.getReqCount().get() == 0 || originalRpcInfo.getResponseCount().get() == 0 + || originalRpcInfo.getSumLatency().get() == 0) { + continue; + } + MetricsRpcInfo metricsRpcInfo = JSONObject.parseObject(JSONObject.toJSONString(originalRpcInfo), + MetricsRpcInfo.class); + BigDecimal reqCount = new BigDecimal(metricsRpcInfo.getReqCount().get()); + BigDecimal resCount = new BigDecimal(metricsRpcInfo.getResponseCount().get()); + BigDecimal errorCount = new BigDecimal(metricsRpcInfo.getReqErrorCount().get()); + BigDecimal errorRatio = errorCount.multiply(TO_PERCENT).divide(resCount, ENABLE_NUM, RoundingMode.HALF_UP); + BigDecimal sumLatency = new BigDecimal(metricsRpcInfo.getSumLatency().get()); + BigDecimal avgLatency = sumLatency.divide(resCount, ENABLE_NUM, RoundingMode.HALF_UP); + BigDecimal reqThroughout = reqCount.multiply(MS_TO_S).divide(new BigDecimal(METRICS_CONFIG.getDelayTime()), + ENABLE_NUM, RoundingMode.HALF_UP); + BigDecimal resThroughout = resCount.multiply(MS_TO_S).divide(new BigDecimal(METRICS_CONFIG.getDelayTime()), + ENABLE_NUM, RoundingMode.HALF_UP); + String latencyHistogram = getLatencyHistogram(metricsRpcInfo.getLatencyList()); + String sslFlag = metricsRpcInfo.isEnableSsl() ? Constants.SSL_OPEN : Constants.SSL_CLOSE; + String metricsInfoStr = stringBuilder.append(Constants.METRICS_LINK).append(Constants.RPC_HEAD) + .append(Constants.METRICS_LINK).append(pid).append(Constants.METRICS_LINK) + .append(metricsRpcInfo.getClientIp()).append(Constants.METRICS_LINK) + .append(metricsRpcInfo.getServerIp()).append(Constants.METRICS_LINK) + .append(metricsRpcInfo.getServerPort()).append(Constants.METRICS_LINK) + .append(metricsRpcInfo.getL4Role()).append(Constants.METRICS_LINK) + .append(metricsRpcInfo.getL7Role()).append(Constants.METRICS_LINK) + .append(metricsRpcInfo.getProtocol()).append(Constants.METRICS_LINK) + .append(sslFlag).append(Constants.METRICS_LINK).append(reqThroughout).append(Constants.METRICS_LINK) + .append(resThroughout).append(Constants.METRICS_LINK).append(reqCount) + .append(Constants.METRICS_LINK).append(resCount).append(Constants.METRICS_LINK).append(avgLatency) + .append(Constants.METRICS_LINK).append(latencyHistogram).append(Constants.METRICS_LINK) + .append(sumLatency).append(Constants.METRICS_LINK).append(errorRatio).append(Constants.METRICS_LINK) + .append(errorCount).toString(); + raf.write(metricsInfoStr.getBytes(Charset.defaultCharset())); + raf.write(LINE_BREAK.getBytes(Charset.defaultCharset())); + stringBuilder.setLength(0); + originalRpcInfo.getReqErrorCount().getAndAdd(-metricsRpcInfo.getReqErrorCount().get()); + originalRpcInfo.getSumLatency().getAndAdd(-metricsRpcInfo.getSumLatency().get()); + originalRpcInfo.getResponseCount().getAndAdd(-metricsRpcInfo.getResponseCount().get()); + originalRpcInfo.getReqCount().getAndAdd(-metricsRpcInfo.getReqCount().get()); + originalRpcInfo.getLatencyList().removeAll(metricsRpcInfo.getLatencyList()); + } + } + + /** + * 初始化机器Id + */ + private void initMachineId() { + try { + ProcessBuilder processBuilder = new ProcessBuilder("/bin/sh", "-c", "dmidecode -s system-uuid"); + Process process = processBuilder.start(); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { + String line; + while ((line = bufferedReader.readLine()) != null) { + if (StringUtils.isExist(line)) { + machineId = line.trim(); + break; + } + } + } + int exitCode = process.waitFor(); + if (exitCode != 0) { + LOGGER.log(Level.WARNING, "Failed to execute command: dmidecode -s system-uuid."); + } + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Error executing command: dmidecode -s system-uuid.", e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOGGER.log(Level.SEVERE, "Command execution interrupted.", e); + } + if (StringUtils.isEmpty(machineId)) { + machineId = StringUtils.EMPTY; + } + } + + /** + * 初始化容器信息 + */ + private void initContainerInfo() { + try { + containerIp = InetAddress.getLocalHost().getHostAddress(); + ProcessBuilder processBuilder = new ProcessBuilder("cat", "/proc/self/cgroup"); + Process process = processBuilder.start(); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + if (line.contains("docker") || line.contains("kubepods")) { + String[] parts = line.split("/"); + containerId = parts[parts.length - 1]; + containerName = parts[parts.length - CONTAINER_NAME_INDEX]; + break; + } + } + int exitCode = process.waitFor(); + if (exitCode != 0) { + LOGGER.log(Level.WARNING, "Failed to get container ID."); + } + } catch (IOException | InterruptedException e) { + LOGGER.log(Level.SEVERE, "Failed to get container ID.", e); + } + if (StringUtils.isEmpty(containerId)) { + containerId = StringUtils.EMPTY; + } + if (StringUtils.isEmpty(containerName)) { + containerName = StringUtils.EMPTY; + } + } + + /** + * 创建文件 + * + * @param filePath 文件路径 + * @param fileName 文件名称 + * @throws IOException 文件创建异常 + */ + private void createTmpFile(String filePath, String fileName) throws IOException { + File dir = new File(filePath); + if (!dir.exists()) { + dir.mkdirs(); + } + File file = new File(fileName); + if (!file.exists()) { + file.createNewFile(); + } + } + + /** + * 生成时延直方图 + * + * @param latencyList 时延信息 + * @return 时延直方图 + */ + private String getLatencyHistogram(List latencyList) { + if (latencyList == null || latencyList.isEmpty()) { + return StringUtils.EMPTY; + } + StringBuilder latencyHistogram = new StringBuilder(String.valueOf(RANGE.length - 1)); + for (int index = 0; index < RANGE.length - 1; index++) { + final int latencyIndex = index; + latencyHistogram.append(" ").append(MS_TO_NS.longValue() * RANGE[index + 1]).append(" ").append( + (int) latencyList.stream().filter(value -> value > RANGE[0] + && value < (MS_TO_NS.longValue() * RANGE[latencyIndex + 1])).count()); + } + return latencyHistogram.toString(); + } +} diff --git a/sermant-plugins/sermant-jsse/jsse-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService b/sermant-plugins/sermant-metrics/metrics-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService similarity index 93% rename from sermant-plugins/sermant-jsse/jsse-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService rename to sermant-plugins/sermant-metrics/metrics-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService index 6153922e0d..793e0c23e6 100644 --- a/sermant-plugins/sermant-jsse/jsse-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService +++ b/sermant-plugins/sermant-metrics/metrics-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -com.huawei.jsse.service.JsseService \ No newline at end of file +com.huawei.metrics.service.MetricsService \ No newline at end of file diff --git a/sermant-plugins/sermant-jsse/pom.xml b/sermant-plugins/sermant-metrics/pom.xml similarity index 73% rename from sermant-plugins/sermant-jsse/pom.xml rename to sermant-plugins/sermant-metrics/pom.xml index 452de4c804..90ddf871e4 100644 --- a/sermant-plugins/sermant-jsse/pom.xml +++ b/sermant-plugins/sermant-metrics/pom.xml @@ -10,11 +10,11 @@ 4.0.0 pom - sermant-jsse + sermant-metrics ${pom.basedir}/../../.. - jsse + metrics @@ -24,22 +24,22 @@ true - jsse-plugin - jsse-service + metrics-plugin + metrics-service test - jsse-plugin - jsse-service + metrics-plugin + metrics-service release - jsse-plugin - jsse-service + metrics-plugin + metrics-service