This tutorial shows how to set up an uptime alert for the Python Hello World app running on App Engine flexible environment using Stackdriver 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).
- 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.
Monitoring is currently offered to beta users at no charge.
Before you begin
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
In the Cloud Console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.
Cloning the sample app
The sample app is available on GitHub at
Clone the repository.
git clone https://github.com/GoogleCloudPlatform/getting-started-python.git
Go to the sample directory.
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 address. Replace
[YOUR_PROJECT_ID]with your Google Cloud project ID.
Adding your project to a Workspace
After your app is deployed, 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. To use Stackdriver, your project must be in a Workspace.
From to the Cloud Console, go to Monitoring.
If the Add your project to a Workspace dialog is displayed, create a new Workspace by selecting your Google Cloud project under New Workspace and then clicking Add. In the following image, the Google Cloud project name is
The Add your project to a Workspace dialog is displayed only when you have at least one existing Workspace available to you. The Workspaces listed under Existing Workspace are Workspaces you've created or Workspaces for Google Cloud projects where you have editorial permission. Using this dialog, you can choose between creating a new Workspace and adding your project to an existing Workspace.
Creating an uptime check
Go to Monitoring.
Under Alerting, click Uptime Checks, then click Create an Uptime Check.
If a dialog opens asking to automatically create an uptime check, click No thanks.
Give your check a title, such as
Check Hello World.
Because you deployed to App Engine, change Resource Type to App Engine instead of URL. (URL is for generating a custom URL on a Compute Engine instance.)
Leave Path blank to default to the main index page.
Change Check every to 1 minute.
Creating an alerting policy
After you create the check, you are automatically prompted to create an associated alerting policy. An alerting policy lets you manage how Stackdriver informs you of any incidents, such as a failed uptime check.
To follow the prompt, click Create Alerting Policy. Give your policy a name, such as
Hello World Uptime Check Policy.
The Conditions of the alert are prepopulated with your uptime check. To manually populate the Conditions instead, you can click Add Another Condition > Uptime Check Health.
To receive an email message from this alerting policy, under Notifications, make sure that Email is selected, and then click Add Notification to enter your email address.
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.
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): """Return a friendly HTTP greeting.""" return 'Hello World!' if __name__ == '__main__': app.run(host='127.0.0.1', port=8080)
To cause the Hello World app to return an HTTP
404error code, change the return line by adding a
404value 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.
To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project you want to delete and click Delete delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.