When your Google Cloud Function invocations fail, you want to know about it as soon as possible. There is a built-in monitoring tool in GCP called Stackdriver, which is easily configurable to send out emails whenever this happens. This short guide will show you exactly how to set this up.
Navigate To Stackdriver
- Go to console.cloud.google.com
- In the hamburger menu in the top left, scroll down until you find the STACKDRIVER section
- Click on Monitoring
This will open up a new window containing the Stackdriver console. If this is the first time you are doing this, it will automatically set up a new Stackdriver workspace for your GCP project.
Create And Configure New Alert Policy
The page you see will read ‘Welcome to Stackdriver Monitoring!’. In the side panel on the left, click on Alerting > Create a Policy.
Click the Add Condition button.
Use the following settings:
- Title: Give the alerting policy a descriptive name, like ‘Cloud function error notification’
- Resource type: Cloud Functions
- Metric: Log entries
- Filter: scroll down to find ‘severity’. In the resulting input box, type ‘ERROR’ in the Value field. This is not given as one of the clickable options in the list of values, so you must explicitly type it in.
- Condition triggers if: Any time series violates
- Condition: is above
- Threshold: 0
- For: most recent value
- Click Save
- Notifications: Email
- Email address: Enter your email address
- Click Add Notification Channel
- Once you have done this, you will see your email appear in the list below.
- It is also a good idea to write a description of why this alert was created and what it does.
- Give this alert policy a descriptive name, like ‘Cloud Functions Error Notification’. Each alerting policy is made up of (one or many) policy conditions. Earlier in this process, you gave this alerting policy’s condition a name ('Cloud function error notification'). In this step you are giving the alerting policy a name. For the sake of simplicity, we suggest to use the same name.
- Click Save
You will now be able to see your new alert policy in the list of active policies.
If you ever want to add or remove people in your alerting policy, you can do this by clicking into your policy and clicking the ‘Edit’ button in the top right. Simply amend the details in the Notifications box as necessary.
That’s it! You will now receive emails whenever a cloud function fails, with some details around which function failed and why. The email will look like the below example.