Skip to content

Commit

Permalink
🚑 avoid metric bug (#12817)
Browse files Browse the repository at this point in the history
* 🚑 avoid metric bug

* 🚑 avoid metric bug

* 🚑 avoid metric bug

* 🚑 avoid metric bug

* 🚑 avoid metric bug
  • Loading branch information
songxiaosheng authored Jul 31, 2023
1 parent 0add70e commit 1b8688c
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -63,27 +70,46 @@ public static <T> T post(MetricsEvent event, Supplier<T> targetSupplier) {
*/
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
Expand Down

0 comments on commit 1b8688c

Please sign in to comment.