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
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 GCP 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.
View your Workspace in Google Cloud Platform Console.
Because your project isn't in a Workspace, you see a message that says your GCP project ID is not in a Stackdriver account. Click Create new Stackdriver account, and then Continue.
In the Create a Stackdriver account page, select your project name, and then click Create Account.
In the Add Google Cloud Platform projects to monitor page, click Continue.
In the Monitor AWS accounts page, click Done.
After a few seconds, you see a message that says the initial collection is finished. Click Launch Monitoring.
In the Get reports by email page, click No reports and Continue. You see your Workspace dashboard.
Close the Welcome to Stackdriver banner if you don't need an introduction to Stackdriver.
Under Stackdriver click Monitoring. The Stackdriver Monitoring dashboard opens.
You are now in the Stackdriver console and can create an uptime check.
Creating an uptime check
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 GCP Console, go to the Projects 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.