diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java index 6c514bafdab..8757454e025 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java @@ -17,15 +17,22 @@ package org.apache.dubbo.metrics.event; +import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; +import org.apache.dubbo.common.logger.LoggerFactory; + import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; +import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_METRICS_COLLECTOR_EXCEPTION; + /** * Dispatches events to listeners, and provides ways for listeners to register themselves. */ public class MetricsEventBus { + private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(MetricsEventBus.class); + /** * Posts an event to all registered subscribers and only once. * @@ -63,27 +70,46 @@ public static T post(MetricsEvent event, Supplier targetSupplier) { */ public static T post(MetricsEvent event, Supplier targetSupplier, Function trFunction) { T result; - before(event); + tryInvoke(() -> { + before(event); + }); if (trFunction == null) { try { result = targetSupplier.get(); } catch (Throwable e) { - error(event); + tryInvoke(() -> { + error(event); + }); throw e; } - after(event, result); + tryInvoke(() -> { + after(event, result); + }); } else { // Custom failure status result = targetSupplier.get(); if (trFunction.apply(result)) { - after(event, result); + tryInvoke(() -> { + after(event, result); + }); } else { - error(event); + tryInvoke(() -> { + error(event); + }); } } return result; } + public static void tryInvoke(Runnable runnable) { + try { + runnable.run(); + } catch (Throwable e) { + logger.warn(COMMON_METRICS_COLLECTOR_EXCEPTION, "" + + "", "", "invoke metric event error" + e.getMessage()); + } + } + /** * Applicable to the scene where execution and return are separated, * eventSaveRunner saves the event, so that the calculation rt is introverted