2

I would simply like to know if it's possible and how to get notified when my Cloud Function is logging an entry using logging.error in python.

The field actually appears as : "ERROR:root:the custom log"

I have checked about Alert policies but i don't understand the queries / filters and the "time between notification", since i only want to push for example to a Slack channel + email only once a notification when an error is logged.

How can i simply do that for each of my Functions ? or having : policies number = Alert policies x function number, in the worst case scenario..

Thanks in advance

Tom3652
  • 2,540
  • 3
  • 19
  • 45
  • You can create an alerting policy with a filter on the log entries to alert on: for example `severity>=ERROR`. The "time between notification" is the minimum amount of time between receiving notifications for logs that match this filter: if you set it to 5 min for examples, after the first occurence of an alert you are not going to receive subsequent alerts during 5 minutes. – Renaud Tarnec Aug 07 '23 at 12:33
  • Hi thanks for your reply ! The problem with `severity>=ERROR` is that it's actually a field in the log entries (i have tried it) and doesn't filter on the `ERROR` from `logging.error` which is simply considered as a safe log entry (simply tagged by myself as error). For the 5min, do you mean i will therefore receive every 5 min a notification for the same alert ? (i want a one shot notification) – Tom3652 Aug 07 '23 at 12:39
  • 1
    For the 5 minutes, it means that if a new error occurs within 5 minutes after a first alert you will not receive an alert. – Renaud Tarnec Aug 07 '23 at 13:13
  • Oh okay thank you – Tom3652 Aug 07 '23 at 13:59

2 Answers2

1

You can do something like :

(resource.type=“cloud_function” resource.labels.function_name=(“function_name”) resource.labels.region=“europe-west1”)) AND
textPayload:ERROR AND timestamp > “2023-08-01"
nicover
  • 2,213
  • 10
  • 24
1

To get notified when there's an "ERROR" log in Google Cloud Logging for each running Cloud Function, you can set up a log-based metric and then create a notification using Cloud Monitoring. Here's how:

Create a Log-Based Metric:

Go to the Google Cloud Console. Navigate to "Logging" under "Operations". In the left navigation pane, click on "Logs Viewer". Enter your log query, such as resource.type="cloud_function" severity="ERROR". Click "Create Metric" and give it a name. Create an Alerting Policy:

Go to "Monitoring" in the Google Cloud Console. Click "Alerting" in the left navigation pane. Click "Create Policy". In the "Add Condition" section, select "Logs-based metric" and then select the metric you created earlier. Configure the condition based on your requirements (e.g., condition threshold). In the "Notifications" section, add the appropriate notification channels (email, SMS, etc.). Save the policy. Test the Alert:

Introduce an error in one of your Cloud Functions. Wait for the error to trigger the log entry. Cloud Monitoring will evaluate the condition and send notifications if the condition is met. This setup ensures you receive notifications whenever there's an "ERROR" log in the Google Cloud Logging for your running Cloud Functions.

  • 1
    Thanks for the answer, it is indeed useful for people because you explain how to get notified on Function error. However, my question is about getting notified when the function is not in error, but when there is an `Error` printed inside the logs of my function. the severity doesn't work, the `textPayload` does – Tom3652 Aug 08 '23 at 12:29