【问题标题】:Grouping with Sentry与哨兵分组
【发布时间】:2019-11-10 13:41:31
【问题描述】:

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

【问题讨论】:

    标签: java jakarta-ee sentry


    【解决方案1】:

    您可以获得引发的异常类型,但我对在跟踪中获取与函数相关的参数有疑问

    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 方法,这不是一种简单的方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-17
      • 2020-02-02
      • 2014-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多