Skip to content

Commit

Permalink
Add metric collection function in router plugin
Browse files Browse the repository at this point in the history
Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed Oct 29, 2024
1 parent 2cc3bcb commit 9022abb
Show file tree
Hide file tree
Showing 28 changed files with 769 additions and 18 deletions.
2 changes: 2 additions & 0 deletions sermant-plugins/sermant-router/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ router.plugin:
enabled-spring-zone-router: false
# compatibility router config 1.0, default is false not support
enabled-previous-rule: false
# Whether to Enable Metrics Collection
enable-metric: false
transmit.plugin:
# Whether to transmit the label on the direct new thread
enabled-thread: true
Expand Down
2 changes: 1 addition & 1 deletion sermant-plugins/sermant-router/dubbo-router-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-api</artifactId>
<version>${dubbo.version}</version>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.router.dubbo.declarer;

import io.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import io.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import io.sermant.core.plugin.agent.matcher.ClassMatcher;
import io.sermant.core.plugin.agent.matcher.MethodMatcher;
import io.sermant.router.dubbo.interceptor.AlibabaDubboMonitorFilterInterceptor;

/**
* Enhanced Declarer of the MonitorFilter
*
* @author zhp
* @since 2024-10-23
*/
public class AlibabaDubboMonitorFilterDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "com.alibaba.dubbo.monitor.support.MonitorFilter";

private static final String METHOD_NAME = "invoke";

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

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[]{
InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME),
new AlibabaDubboMonitorFilterInterceptor())
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.router.dubbo.declarer;

import io.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import io.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import io.sermant.core.plugin.agent.matcher.ClassMatcher;
import io.sermant.core.plugin.agent.matcher.MethodMatcher;
import io.sermant.router.dubbo.interceptor.ApacheDubboMonitorFilterInterceptor;

/**
* Enhanced Declarer of the MonitorFilter
*
* @author zhp
* @since 2024-10-23
*/
public class ApacheDubboMonitorFilterDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "org.apache.dubbo.monitor.support.MonitorFilter";

private static final String METHOD_NAME = "invoke";

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

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[]{
InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME),
new ApacheDubboMonitorFilterInterceptor())
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.sermant.core.common.LoggerFactory;
import io.sermant.core.plugin.service.PluginServiceManager;
import io.sermant.router.common.constants.RouterConstant;
import io.sermant.router.common.metric.MetricsManager;
import io.sermant.router.common.utils.CollectionUtils;
import io.sermant.router.common.utils.DubboReflectUtils;
import io.sermant.router.dubbo.service.LaneContextFilterService;
Expand Down Expand Up @@ -77,6 +78,13 @@ public Map<String, List<String>> getRequestTag(Object invoker, Object invocation
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("Lane is " + requestTag);
}
laneTag.forEach((key, values) -> {
if (CollectionUtils.isEmpty(values)) {
return;
}
MetricsManager.addOrUpdateMetricValue(RouterConstant.LANE_COUNT, RouterConstant.LANE_TAG_NAME,
key + ":" + values.get(0), 1);
});
return requestTag;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.router.dubbo.interceptor;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invoker;

import io.sermant.core.plugin.agent.entity.ExecuteContext;
import io.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import io.sermant.router.common.constants.RouterConstant;
import io.sermant.router.common.metric.MetricsManager;

/**
* Interceptor of the MonitorFilter, Collecting Metric Information
*
* @author zhp
* @since 2024-10-23
*/
public class AlibabaDubboMonitorFilterInterceptor extends AbstractInterceptor {
@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
return context;
}

@Override
public ExecuteContext after(ExecuteContext context) throws Exception {
Object[] arguments = context.getArguments();
if (arguments == null || arguments.length == 0) {
return context;
}
if (arguments[0] instanceof Invoker) {
Invoker<?> invoker = (Invoker<?>) arguments[0];
URL url = invoker.getUrl();
String address = url.getHost() + RouterConstant.URL_CONNECTOR + url.getPort();
MetricsManager.addOrUpdateMetricValue(RouterConstant.REQUEST_COUNT, RouterConstant.REQUEST_COUNT_TAG_NAME,
address, 1);
}
return context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. 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 io.sermant.router.dubbo.interceptor;

import io.sermant.core.plugin.agent.entity.ExecuteContext;
import io.sermant.core.plugin.agent.interceptor.AbstractInterceptor;
import io.sermant.router.common.constants.RouterConstant;
import io.sermant.router.common.metric.MetricsManager;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invoker;

/**
* Interceptor of the MonitorFilter, Collecting Metric Information
*
* @author zhp
* @since 2024-10-23
*/
public class ApacheDubboMonitorFilterInterceptor extends AbstractInterceptor {
@Override
public ExecuteContext before(ExecuteContext context) throws Exception {
return context;
}

@Override
public ExecuteContext after(ExecuteContext context) throws Exception {
Object[] arguments = context.getArguments();
if (arguments == null || arguments.length == 0) {
return context;
}
if (arguments[0] instanceof Invoker) {
Invoker<?> invoker = (Invoker<?>) arguments[0];
URL url = invoker.getUrl();
String address = url.getHost() + RouterConstant.URL_CONNECTOR + url.getPort();
MetricsManager.addOrUpdateMetricValue(RouterConstant.ROUTE_COUNT_TAG_NAME, RouterConstant.REQUEST_COUNT,
address, 1);
}
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ io.sermant.router.dubbo.declarer.ClusterUtilsDeclarer
io.sermant.router.dubbo.declarer.ContextFilterDeclarer
io.sermant.router.dubbo.declarer.SpringApplicationDeclarer
io.sermant.router.dubbo.declarer.AbstractConfigDeclarer
io.sermant.router.dubbo.declarer.ApacheDubboMonitorFilterDeclarer
io.sermant.router.dubbo.declarer.AlibabaDubboMonitorFilterDeclarer
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public class RouterConfig implements PluginConfig {
@ConfigFieldKey("enabled-previous-rule")
private boolean enabledPreviousRule = false;

/**
* Whether to Enable Metrics Collection
*/
@ConfigFieldKey("enable-metric")
private boolean enableMetric = false;

/**
* Constructor
*/
Expand Down Expand Up @@ -225,4 +231,12 @@ public boolean isEnabledSpringCloudXdsRouteSecure() {
public void setEnabledSpringCloudXdsRouteSecure(boolean enabledSpringCloudXdsRouteSecure) {
this.enabledSpringCloudXdsRouteSecure = enabledSpringCloudXdsRouteSecure;
}

public boolean isEnableMetric() {
return enableMetric;
}

public void setEnableMetric(boolean enableMetric) {

Check warning on line 239 in sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/config/RouterConfig.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议1.2 布尔变量名或方法以is开头(has have does do did will should can may must could等疑问助动词也可以,Android允许m作为疑问助动词前缀) Raw Output: /home/runner/work/Sermant/Sermant/./sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/config/RouterConfig.java:239:0: warning: 编程规范-建议1.2 布尔变量名或方法以is开头(has have does do did will should can may must could等疑问助动词也可以,Android允许m作为疑问助动词前缀) (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck)
this.enableMetric = enableMetric;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,47 @@ public class RouterConstant {
*/
public static final String ESCAPED_POINT = "\\.";

/**
* Metric Name for Request Count
*/
public static final String REQUEST_COUNT = "requestCount";

/**
* Metric Name for Route Count
*/
public static final String ROUTE_COUNT = "routeCount";

/**
* Metric Name for lane Count
*/
public static final String LANE_COUNT = "laneCount";

/**
* Tag Name for Request Count
*/
public static final String REQUEST_COUNT_TAG_NAME = "address";

/**
* Tag Name for Route Count
*/
public static final String ROUTE_COUNT_TAG_NAME = "routeTag";

/**
* Tag Name for match Strategy
*/
public static final String MATCH_STRATEGY_TAG_NAME = "matchType";

/**
* Tag Name for lane Count
*/
public static final String LANE_TAG_NAME = "laneTag";

Check warning on line 230 in sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/constants/RouterConstant.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 编程规范-建议3.6 减少不必要的空行,保持代码紧凑(最多一个空行)。 Raw Output: /home/runner/work/Sermant/Sermant/./sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/constants/RouterConstant.java:230:0: warning: 编程规范-建议3.6 减少不必要的空行,保持代码紧凑(最多一个空行)。 (com.puppycrawl.tools.checkstyle.checks.regexp.RegexpMultilineCheck)

/**
* URL Connector
*/
public static final String URL_CONNECTOR = ":";

private RouterConstant() {
}
}
Loading

0 comments on commit 9022abb

Please sign in to comment.