Sentry Android:忽略不包含我的包的堆栈跟踪

Sentry Android: Ignore stacktraces that do not contain my package

我在供其他开发人员使用的 Android 库中使用 Sentry。我从使用我的库的应用程序中得到很多异常,但这些异常与库无关,我真的很想忽略这些。有什么方法可以过滤异常,所以我只报告那些在堆栈跟踪中某处有我的库包的异常吗?

您可以使用 ShouldSendEventCallback:

public static void example() {
    SentryClient client = Sentry.getStoredClient();

    client.addShouldSendEventCallback(new ShouldSendEventCallback() {
        @Override
        public boolean shouldSend(Event event) {
            // decide whether to send the event

            for (Map.Entry<String, SentryInterface> interfaceEntry : event.getSentryInterfaces().entrySet()) {
                if (interfaceEntry.getValue() instanceof ExceptionInterface) {
                    ExceptionInterface i = (ExceptionInterface) interfaceEntry.getValue();
                    for (SentryException sentryException : i.getExceptions()) {
                        // this example checks the exception class
                        if (sentryException.getExceptionClassName().equals("foo")) {
                            // don't send the event
                            return false;
                        }
                    }
                }
            }

            // send event
            return true;
        }
    });
}

有一张票可以让这更容易:https://github.com/getsentry/sentry-java/issues/575

对于任何有我确切问题的人,我修改了 Brett 的答案,以便它检查整个堆栈跟踪,因为异常的原因有时可能被埋在 Android。

SentryClient client = Sentry.getStoredClient();
client.addShouldSendEventCallback(new ShouldSendEventCallback() {
    @Override
    public boolean shouldSend(Event event) {
        for (Map.Entry<String, SentryInterface> interfaceEntry : event.getSentryInterfaces().entrySet()) {
            if (interfaceEntry.getValue() instanceof ExceptionInterface) {
                ExceptionInterface i = (ExceptionInterface) interfaceEntry.getValue();
                for (SentryException sentryException : i.getExceptions()) {
                    for (SentryStackTraceElement element : sentryException.getStackTraceInterface().getStackTrace()) {
                        if (element.getModule().contains("com.example.library")) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
});