简体   繁体   中英

Keep track of specific errors in Google Cloud Error Logs?

I am using GCP with Cloud Run, SQL etc with a Node.js application. I have a specific type of error that appear quite often, related to a Twitter API call. Quite often I get this "Rate limit exceeded" error from the API which I handle like this:

items.forEach((item) => {
if (item.body.status == 429) {
            throw Error("Rate limit exceeded")
}
}).catch((error) => console.log(error))

This appears in the logs but I would like it to be tracked in GCP Error list https://console.cloud.google.com/errors so I can see if my efforts to limit this specific error is going better. I don't want it to break the script so I also want to catch the error. How can I solve this best?

I haven't been able to reproduce with your exact syntax (I'm not a nodejs expert) but I've been able to get errors reported in both Error Reporting and Logging via the following code:

try {
  items.forEach((item) => {
    if (item.body.status == 429) {
      console.log(new Error("Rate limit exceeded"));
    }
  })
}
catch (error) {
  console.log(error);
}

Note that instead of throwing the error in the if block I just console.log(new Error("Rate limit exceeded")) and Error Reporting automatically picks it up.

The "Rate limit exceeded" does appear for me out of the box in the Error Reporting dashboard: 在此处输入图像描述

and in the logs: 在此处输入图像描述

Also note that the Logging page has an histogram view above the log entries that shows occurrences of the logs over time. By just filtering on the textPayload with the errors, you can view a detailed count of these errors over time in the histogram.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM