Problem: When I attempt to use the Error Reporting function to log errors, it doesn't send to the error reporter.
I instantiate like this:
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()
}
And then I call it like this:
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
}
The logs Error client is NOT nil
runs, so I know it's started, but no alerts come up. I also trigger it through a special URL endpoint that logs an error (or should) on every hit.
I used your code on my side and all works as expected, if you can't see any error on GCP console,this indicates that the Error reporting API is not enabled and for this reason you can't see any error on it.
Please enable Error Reporting API