This tutorial shows how to set up an uptime alert for the Python Hello World app running on App Engine flexible environment using Cloud Monitoring . Uptime alerts let you know when your app is not serving traffic. You can also set uptime alerts for apps running on Compute Engine or Google Kubernetes Engine (GKE) .
Objectives
- Run a basic Hello World app.
- Create an uptime check that monitors whether the app returns an HTTP '200' status code.
- Create an alert that sends an email message to you when the uptime check fails.
- Restart the app to trigger the alert.
Costs
To generate a cost estimate based on your projected usage, use the pricing calculator .
Monitoring is currently offered to beta users at no charge.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project
: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles .
-
Verify that billing is enabled for your Google Cloud project .
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project : Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- Create a project
: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles .
-
Verify that billing is enabled for your Google Cloud project .
Cloning the sample app
The sample app is available on GitHub at GoogleCloudPlatform/getting-started-python
.
-
Clone the repository.
git clone https : // github . com / GoogleCloudPlatform / getting - started - python . git -
Go to the sample directory.
cd getting - started - python / gce -
Because the app only returns "Hello World!", it requires no configuration, and you can run it right away.
gcloud app deploy -
To see the returned message, enter the following URL in your browser:
https:// PROJECT_ID . REGION_ID .r.appspot.comReplace the following:
-
PROJECT_ID: Your Google Cloud project ID -
REGION_ID: A code that App Engine assigns to your app
-
Creating an uptime check
After you deploy your app, you can use Monitoring to create an uptime check. The check continually pings your deployed app to ensure that it's returning a healthy response.
-
In the Google Cloud console, go to the
Uptime checks
page:If you use the search bar to find this page, then select the result whose subheading is Monitoring .
- Click Create Uptime Check.
-
Give your check a title, such as
Check Hello World, and then click Next. -
In the Targetsection, you specify what the uptime check is to monitor:
- Because you deployed to App Engine, change Resource Typeto App Engineinstead of URL. ( URLis for generating a custom URL on a Compute Engine instance.)
- Select the Serviceto be monitored by the uptime check.
- Leave Pathblank to default to the main index page.
- Leave Check frequencyat the default of 1minute.
- Click Next.
-
Leave the Response Validationfields at their default values and click Next.
-
In the Alert & Notificationsection, you specify how you are notified if an alert occurs:
- Ensure that the toggle's label is Alerting is enabled.
- Leave the name and duration fields at their default values.
-
To add a notification channel to the alerting policy, in the text box labeled Notification channels, click Menu. Select the channels to add and click OK. The notifications are grouped alphabetically for each channel type.
To add an entry to the checkbox list, click Manage notification channelsand follow the instructions. When you return to this dialog, click Refresh refresh .
-
Click Create. When the create action is successful, the message Check and alert createdis displayed and then the Uptime checksdashboard page is displayed.
In the uptime checks dashboard, your new uptime check is listed. If you click the check name, then you open the detail view for that uptime check. This view displays several charts, shows the uptime percentage and the configuration information, and lists the configured alert policies. To view a policy, click its name.
You can also view the alert policy by starting at the Alertingpage. From the alerting page, the Policiespane lists a subset of policies. To view a list of all policies, click See all policies.
Simulating an outage
Now that the uptime check is created, you can simulate an outage by changing
your app to respond with an HTTP 404 Sorry, we can't find that page
error
rather than an HTTP 200 OK
response.
-
The following code shows where the Hello World app returns only a 'Hello World!' message, and that the status code of the response defaults to
200 OK. To view this code in the Hello World app, use the view function. -
To cause the Hello World app to return an HTTP
404error code, change the return line by adding a404value to the second part of the return value.return 'Hello World' , 404
-
Deploy the new, intentionally broken app.
gcloud app deploy
Within half an hour, you'll receive an email message that states that your uptime check is failing.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete .
- In the dialog, type the project ID, and then click Shut down to delete the project.

