Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚑 avoid metric bug #12817

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading