【问题标题】:Golang / Google Cloud Error Reporting Not WorkingGolang / Google Cloud 错误报告不起作用
【发布时间】:2023-03-15 10:19:01
【问题描述】:

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

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 端点触发它,该端点在每次点击时记录一个错误(或应该)。

【问题讨论】:

  • 你认为defer ErrorClient.Close() 会做什么?
  • Hmmmmmnnn .. 可能使它不起作用...我会报告的。谢谢
  • @EmilePels 我删除了它,但它仍然不起作用。
  • 您的代码在哪里运行?在本地设备上还是在 google google 服务中?
  • @JanHernandez 这是 API。非常感谢!如果您愿意,请在下方评论,我会接受它作为答案。

标签: go google-app-engine google-cloud-platform google-cloud-error-reporting


【解决方案1】:

我使用了您的代码,一切正常,如果您在 GCP 控制台上看不到任何错误,则表明错误报告 API 未启用,因此您看不到任何错误它。

请启用Error Reporting API

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2012-04-23
    • 2021-05-20
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    相关资源
    最近更新 更多