java - 与哨兵分组
问题描述
我想用 Sentry 对异常进行分组,异常来自不同的服务器,但我希望将所有异常按类型放在一起,例如,将所有 NPE 分组。我知道你可以扩展 EventBuilderHelper,这就是 sentry 分组的方式,但是 sentry java 不提供发送带有方法指纹、错误类型等的事件的功能,就像其他 SDK 一样,例如 docs.sentry.io 中的这个示例
function makeRequest(method, path, options) {
return fetch(method, path, options).catch(err => {
Sentry.withScope(scope => {
// group errors together based on their request and response
scope.setFingerprint([method, path, err.statusCode]);
Sentry.captureException(err);
});
});
}
这是我尝试做的,但在这个范围内,不了解方法、错误等。
package com.test;
import io.sentry.SentryClient;
import io.sentry.event.EventBuilder;
import io.sentry.event.helper.ContextBuilderHelper;
public class FingerprintEventBuilderHelper extends ContextBuilderHelper {
private static final String EXCEPTION_TYPE = "exception_type";
public FingerprintEventBuilderHelper(SentryClient sentryClient) {
super(sentryClient);
}
@Override
public void helpBuildingEvent(EventBuilder eventBuilder) {
super.helpBuildingEvent(eventBuilder);
//Get the exception type
String exceptionType =
if (exceptionType != null) {
eventBuilder.withTag(EXCEPTION_TYPE, exceptionType);
}
//Get method information and params
if (paramX != null) {
eventBuilder.withTag("PARAM", paramX);
}
}
}
发送到服务器的 json 有一些关于异常的信息,但我不知道如何得到它
...
"release": null,
"dist": null,
"platform": "java",
"culprit": "com.sun.ejb.containers.BaseContainer in checkExceptionClientTx",
"message": "Task execution failed",
"datetime": "2019-06-26T14:13:29.000000Z",
"time_spent": null,
"tags": [
["logger", "com.test.TestService"],
["server_name", "localhost"],
["level", "error"]
],
"errors": [],
"extra": {
"Sentry-Threadname": "MainThread",
"rid": "5ff37e943-f4b4-4hc9-870b-4f8c4d18cf84"
},
"fingerprint": ["{{ default }}"],
"key_id": 3,
"metadata": {
"type": "NullPointerException",
"value": ""
},
...
解决方案
您可以获得引发的异常类型,但我对在跟踪中获取与函数相关的参数有疑问
EventBuilderHelper myEventBuilderHelper = new EventBuilderHelper() {
public void helpBuildingEvent(EventBuilder eventBuilder) {
eventBuilder.withMessage("Overwritten by myEventBuilderHelper!");
Map<String, SentryInterface> ifs = eventBuilder.getEvent().getSentryInterfaces();
if (ifs.containsKey("sentry.interfaces.Exception"))
{
ExceptionInterface exI = (ExceptionInterface) ifs.get("sentry.interfaces.Exception");
for (SentryException ex: exI.getExceptions()){
String exceptionType = ex.getExceptionClassName();
}
}
}
};
如果您查看sendException
客户端的方法,它会ExceptionInterface
使用实际的异常启动
public void sendException(Throwable throwable) {
EventBuilder eventBuilder = (new EventBuilder()).withMessage(throwable.getMessage()).withLevel(Level.ERROR).withSentryInterface(new ExceptionInterface(throwable));
this.sendEvent(eventBuilder);
}
同样的构造函数就像
public ExceptionInterface(Throwable throwable) {
this(SentryException.extractExceptionQueue(throwable));
}
public ExceptionInterface(Deque<SentryException> exceptions) {
this.exceptions = exceptions;
}
因此,每个异常都会转换为 a SentryException
,但不会存储原始异常。因此,如果您还需要参数,则需要使用这些参数引发自定义异常并覆盖该sendException
方法,这不是一种简单的方法
推荐阅读
- python - 模拟时使用 PID 控制器计算不正确
- c# - 类型“XElement”在未引用的程序集中定义
- python - 在环境中运行 spyder 时“找不到 pythonw.exe 入口点”
- visual-studio-code - emmet 在没有缩写的 VScode 中工作
- python-3.x - 在 MacOS 上运行 odoo-bin 时出现问题(ValueError:当前限制超过最大限制)
- python - 我将如何在我的程序中实现堆栈“返回”系统
- c# - 实体框架 | 序列包含多个匹配元素
- google-cloud-platform - gcloud 命令行中这些命令集之间的实际区别是什么?
- javascript - 反应:图像 src 没有通过 API 走路径。图像未定义
- flutter - 如何以特定格式以编程方式编辑和打印模板文本?