Jua*_*vid 5 java java-ee sentry
我想用Sentry对异常进行分组,该异常来自不同的服务器,但是我希望所有异常按类型分类在一起,例如,将所有NPE分组。我知道您可以扩展EventBuilderHelper,这就是哨兵分组的方式,但是哨兵java不提供发送带有方法,错误类型等指纹的事件的功能,就像docs.sentry.io中的其他SDK一样
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);
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试做的,但是在这个范围内,没有关于方法,错误等的知识。
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
发送到服务器的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": ""
},
...
Run Code Online (Sandbox Code Playgroud)
您可以获取引发的异常类型,但我对获取与跟踪中的函数相关的参数存有疑问
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();
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
如果您查看sendException客户端的方法,它会ExceptionInterface使用实际的异常启动
public void sendException(Throwable throwable) {
EventBuilder eventBuilder = (new EventBuilder()).withMessage(throwable.getMessage()).withLevel(Level.ERROR).withSentryInterface(new ExceptionInterface(throwable));
this.sendEvent(eventBuilder);
}
Run Code Online (Sandbox Code Playgroud)
其构造函数就像
public ExceptionInterface(Throwable throwable) {
this(SentryException.extractExceptionQueue(throwable));
}
public ExceptionInterface(Deque<SentryException> exceptions) {
this.exceptions = exceptions;
}
Run Code Online (Sandbox Code Playgroud)
因此每个异常都会转换为 a SentryException,但原始异常不会被存储。因此,如果您还需要参数,则需要使用这些参数引发自定义异常并覆盖该sendException方法,这不是一种简单的方法