Search code examples
javaandroidsentry

Sentry Android: Ignore stacktraces that do not contain my package


I use Sentry in an Android library which is for use by other developers. I get a lot of exceptions from apps using my library but which have nothing to do with the library, and would really like to ignore these. Is there some way to filter exceptions so I only report those that have my library's package somewhere in the stacktrace?


Solution

  • You can use a 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;
            }
        });
    }
    

    There is a ticket to make this easier: https://github.com/getsentry/sentry-java/issues/575