java - 与 Sentry 分组

标签 java jakarta-ee sentry

我想用 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": ""
    },
...

最佳答案

可以获取抛出的异常类型,但是我对获取trace中函数相关的参数存疑

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;
}

所以每个异常都被转换为一个SentryException,但是原始的异常并没有被存储。因此,如果您还需要参数,则需要使用这些参数抛出自定义异常并覆盖 sendException 方法,这不是一种简单的方法

关于java - 与 Sentry 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809585/

相关文章:

java - 如何使 LibGDX 应用程序以与浏览器框架相同的分辨率启动

JAVA : Output text from a class to main frame

java - 如何捕获 LTPA WebSphere 异常

multithreading - 如何停止/终止/恢复受管执行人服务

java - MongoDB如何使用Java驱动程序计算数组元素

java - 如何找到给定时间的时差

java - 使用 ResponseEntity Spring MVC

python - 使用 djangos `manage.py shell` 时禁用 Sentry 报告

sentry - Sentry 中的客户端 key 在哪里

android - 使用 Sentry 监控错误 - Android