Skip to content

Commit

Permalink
Track root exception in event util
Browse files Browse the repository at this point in the history
  • Loading branch information
Flanker32 committed Sep 29, 2022
1 parent 45b32bf commit 903e0fe
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
import com.azure.core.management.AzureEnvironment;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.account.IAzureAccount;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Consumer;

import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_CLASSNAME;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_CODE;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_MSG;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_ROOT_CLASSNAME;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_ROOT_MSG;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_STACKTRACE;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.ERROR_TYPE;
import static com.microsoft.azure.toolkit.lib.common.telemetry.AzureTelemeter.OPERATION_NAME;
Expand Down Expand Up @@ -213,14 +217,17 @@ public static boolean isAbleToCollectErrorStacks() {
private static void logError(String serviceName, String operName, ErrorType errorType, Throwable e,
Map<String, String> properties, Map<String, Double> metrics, boolean logErrorTraces) {
try {
final Throwable rootCause = Optional.ofNullable(e).map(ExceptionUtils::getRootCause).orElse(null);
Map<String, String> mutableProps = properties == null ? new HashMap<>() : new HashMap<>(properties);
mutableProps.put(OPERATION_NAME, operName);
mutableProps.put(OPERATION_ID, UUID.randomUUID().toString());
mutableProps.put(ERROR_CODE, "1");
mutableProps.put(ERROR_CLASSNAME, e != null ? e.getClass().getName() : "");
mutableProps.put(ERROR_TYPE, errorType.name());
mutableProps.put(ERROR_ROOT_CLASSNAME, Optional.ofNullable(rootCause).map(Throwable::getClass).map(Class::getName).orElse(StringUtils.EMPTY));
if (logErrorTraces && isAbleToCollectErrorStacks()) {
mutableProps.put(ERROR_MSG, e != null ? e.getMessage() : "");
mutableProps.put(ERROR_ROOT_MSG, Optional.ofNullable(rootCause).map(Throwable::getMessage).orElse(StringUtils.EMPTY));
mutableProps.put(ERROR_STACKTRACE, ExceptionUtils.getStackTrace(e));
}
sendTelemetry(EventType.error, serviceName, mergeProperties(mutableProps), metrics);
Expand Down

0 comments on commit 903e0fe

Please sign in to comment.