🚑 avoid metric bug (#12817)
* 🚑 avoid metric bug * 🚑 avoid metric bug * 🚑 avoid metric bug * 🚑 avoid metric bug * 🚑 avoid metric bug
This commit is contained in:
parent
0add70e3f5
commit
1b8688c179
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue