首页 > 解决方案 > 如何以编程方式(java)防止将特定错误消息发送到哨兵

问题描述

如何以编程方式(java)防止将特定错误消息发送到哨兵?例如,我希望不要向 Sentry 发送带有“any_example_word”一词的错误。重要的是要知道在用户界面中未启用按错误消息进行过滤。

我使用的是 Sentry 1.7.23,但我能找到的所有示例都使用最新版本 (4.*),它们完全不同。他们使用旧版本中不存在的类和方法。

我不知道这是否相关,但我的应用程序在 thorntail 上运行并且它使用 jdk 8。

编辑:
我正在尝试这样做:

@WebListener
public class MyContextListener implements ServletContextListener {
    private static SentryClient sentryClient = SentryClientFactory.sentryClient();

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        Sentry.init();

        String testStrings = "ipsis litteris;some_error_message";
        String[] messagesToIgnore = StringUtils.split(testStrings, ';');

        sentryClient.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 (String msgToIgnore : messagesToIgnore) {
                                if (StringUtils.contains(sentryException.getExceptionMessage(), msgToIgnore)) {
                                    return false;
                                }
                            }
                        }
                    }
                }

                return true;
            }
        });

        Sentry.setStoredClient(sentryClient);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
    }
}

问题 1)这是初始化 Sentry 的正确位置吗?

问题 2) 为什么ShouldSendEventCallback会丢失?看着 io.sentry.SentryClient

public void sendEvent(Event event) {
    for (ShouldSendEventCallback shouldSendEventCallback : shouldSendEventCallbacks) {
        if (!shouldSendEventCallback.shouldSend(event)) {
            logger.trace("Not sending Event because of ShouldSendEventCallback: {}", shouldSendEventCallback);
            return;
        }
    }

    try {
        connection.send(event);
    } catch (LockedDownException | TooManyRequestsException e) {
        logger.debug("Dropping an Event due to lockdown: " + event);
    } catch (Exception e) {
        logger.error("An exception occurred while sending the event to Sentry.", e);
    } finally {
        getContext().setLastEventId(event.getId());
    }
}

在应用程序执行期间的某个时刻,sentryClient重新初始化并shouldSendEventCallbacks变为空,是什么导致我的消息未被过滤。

所以我回到问题 1,因为显然哨兵配置不是持久的。

标签: javasentry

解决方案


推荐阅读