java - 如何以编程方式(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,因为显然哨兵配置不是持久的。
解决方案
推荐阅读
- python - 根据多列值合并两个文件
- javascript - 光滑的旋转木马相同高度的幻灯片不起作用
- php - 如何将 jQuery ajax 成功数据转换为 PHP 变量
- r - R中的Apriori算法,而不是负面规则
- python - 重新分配字典值
- embedded-linux - 嵌入式linux中的启动/停止进程
- tensorflow - Tensorflow:将值输入现有占位符或将其删除
- url-rewriting - 将数百万个 URL 从 /forum 重定向到 /forums
- javascript - 更改样式后不显示元素
- javascript - 如何在 Angular 4 中创建 Bootstrap 多级注册页面