Golang / Google 云错误报告不起作用

Golang / Google Cloud Error Reporting Not Working

问题:当我尝试使用错误报告功能记录错误时,它没有发送给错误报告器。

Here is the documentation

我这样实例化:

const projectID = "correct-project-id"

var ErrorClient *errorreporting.Client

func MakeErrorReporter() {
    var err error
    ctx := context.Background()
    ErrorClient, err = errorreporting.NewClient(ctx, projectID, errorreporting.Config{
        ServiceName: "easyLanding",
        OnError: func(err error) {
            log.Printf("Could not log error: %v", err)
        },
    })
    if err != nil {
        msg := fmt.Sprintf("this is the fatal err", err.Error())
        log.Fatal(msg)
    }
    defer ErrorClient.Close()
}

然后我这样称呼它:

func LogErr(err error, location string, userId uint) string {
    errCode := RandStringRunes(4)
    msg := fmt.Sprintf("Code: %v | Error: %v | Location: %v", errCode, err.Error(), location)
    var email string
    if userId == 0 {
        email = "system"
    } else {
        email, _ = GetUserEmail(userId)
    }
    if viper.GetString("debug") == "false" {
        logger.Logger.Error().Msgf("Just ran the error with client %v", errorReporting.ErrorClient.Report)
        if errorReporting.ErrorClient == nil {
            logger.Logger.Error().Msg("Error client is nil")
        } else {
            logger.Logger.Error().Msg("Error client is NOT nil")
        }
        email = fmt.Sprintf("%s - %s", email, location)
        errorReporting.ErrorClient.Report(errorreporting.Entry{
            Error: err,
            User:  email,
        })
        label := map[string]string{}
        label["key"] = "simpleMessage"
        logger.InfoLogger.Log(logging.Entry{
            Severity: logging.Info,
            Payload:  msg,
            Labels:   label,
            InsertID: "",
        })
    } else {
        logger.Logger.Error().Msg(msg)
        logger.Logger.Error().Msg(email)
    }
    return errCode
}

日志 Error client is NOT nil 运行,所以我知道它已经启动,但没有出现警报。我还通过一个特殊的 URL 端点触发它,该端点在每次点击时记录一个错误(或应该)。

我在我这边使用了你的代码,一切都按预期工作,如果你在 GCP 控制台上看不到任何错误,这表明错误报告 API 没有启用,因此你可以看不到任何错误。

请启用Error Reporting API