🚑 avoid metric bug (#12817)

* 🚑 avoid metric bug

* 🚑 avoid metric bug

* 🚑 avoid metric bug

* 🚑 avoid metric bug

* 🚑 avoid metric bug
This commit is contained in:
songxiaosheng 2023-07-31 14:58:10 +08:00 committed by GitHub
parent 0add70e3f5
commit 1b8688c179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 5 deletions

View File

@ -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 class MetricsEventBus {
*/
public static <T> T post(MetricsEvent event, Supplier<T> targetSupplier, Function<T, Boolean> 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