This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 073a13dd9b Fix business exception (#12136)
073a13dd9b is described below
commit 073a13dd9b7049638161c37ec3cd0dc0379e4694
Author: Albumen Kevin <[email protected]>
AuthorDate: Tue Apr 25 09:30:46 2023 +0800
Fix business exception (#12136)
* Fix business exception
* opt
* opt
* Catch exception
---
.../metrics/filter/MethodMetricsInterceptor.java | 12 ++++++-----
.../apache/dubbo/metrics/filter/MetricsFilter.java | 23 +++++++++++++++++++---
2 files changed, 27 insertions(+), 8 deletions(-)
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
index 7c61eda55f..98620ccb7a 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
@@ -46,27 +46,28 @@ public class MethodMetricsInterceptor {
private String getSide(Invocation invocation) {
Optional<? extends Invoker<?>> invoker =
Optional.ofNullable(invocation.getInvoker());
- String side = invoker.isPresent() ? invoker.get().getUrl().getSide() :
PROVIDER_SIDE;
- return side;
+ return invoker.isPresent() ? invoker.get().getUrl().getSide() :
PROVIDER_SIDE;
}
public void afterMethod(Invocation invocation, Result result) {
if (result.hasException()) {
- handleMethodException(invocation, result.getException());
+ handleMethodException(invocation, result.getException(), true);
} else {
sampler.incOnEvent(invocation,
MetricsEvent.Type.SUCCEED.getNameByType(getSide(invocation)));
onCompleted(invocation);
}
}
- public void handleMethodException(Invocation invocation, Throwable
throwable) {
+ public void handleMethodException(Invocation invocation, Throwable
throwable, boolean isBusiness) {
if (throwable == null) {
return;
}
String side = getSide(invocation);
MetricsEvent.Type eventType = MetricsEvent.Type.UNKNOWN_FAILED;
- if (throwable instanceof RpcException) {
+ if (isBusiness) {
+ eventType = MetricsEvent.Type.BUSINESS_FAILED;
+ } else if (throwable instanceof RpcException) {
RpcException e = (RpcException) throwable;
if (e.isTimeout()) {
@@ -85,6 +86,7 @@ public class MethodMetricsInterceptor {
eventType = MetricsEvent.Type.NETWORK_EXCEPTION;
}
}
+
sampler.incOnEvent(invocation, eventType.getNameByType(side));
onCompleted(invocation);
sampler.incOnEvent(invocation,
MetricsEvent.Type.TOTAL_FAILED.getNameByType(side));
diff --git
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
index c981f62831..04265e4aff 100644
---
a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
+++
b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
@@ -17,6 +17,8 @@
package org.apache.dubbo.metrics.filter;
import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
import org.apache.dubbo.rpc.BaseFilter;
import org.apache.dubbo.rpc.Filter;
@@ -29,10 +31,13 @@ import org.apache.dubbo.rpc.model.ScopeModelAware;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
+import static
org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
@Activate(group = {CONSUMER, PROVIDER}, order = -1)
public class MetricsFilter implements Filter, BaseFilter.Listener,
ScopeModelAware {
+ private final static ErrorTypeAwareLogger LOGGER =
LoggerFactory.getErrorTypeAwareLogger(MetricsFilter.class);
+
private DefaultMetricsCollector collector = null;
private MethodMetricsInterceptor metricsInterceptor;
@@ -51,7 +56,11 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
return invoker.invoke(invocation);
}
- metricsInterceptor.beforeMethod(invocation);
+ try {
+ metricsInterceptor.beforeMethod(invocation);
+ } catch (Throwable t) {
+ LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when
beforeMethod.", t);
+ }
return invoker.invoke(invocation);
}
@@ -61,7 +70,11 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return;
}
- metricsInterceptor.afterMethod(invocation, result);
+ try {
+ metricsInterceptor.afterMethod(invocation, result);
+ } catch (Throwable t) {
+ LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when
afterMethod.", t);
+ }
}
@Override
@@ -69,7 +82,11 @@ public class MetricsFilter implements Filter,
BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return;
}
- metricsInterceptor.handleMethodException(invocation, t);
+ try {
+ metricsInterceptor.handleMethodException(invocation, t, false);
+ } catch (Throwable t1) {
+ LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when
handleMethodException.", t1);
+ }
}
}