This tutorial shows how to set up an uptime alert for the Python Hello World Application using Google Stackdriver. Uptime alerts allow you to receive notifications whenever your application is not serving traffic. This tutorial demonstrates setting an uptime alert for the Hello World application on App Engine Flexible. You can also set uptime alerts for applications deployed on Google Compute Engine or Google Kubernetes Engine.
- Deploy a basic "Hello World" app.
- Create an uptime check that monitors that the application returns a 200 HTTP response.
- Create an alert policy that emails you when the uptime check fails.
- Redeploy the app so it returns a 404 response and triggers the alert so that you receive an email.
This tutorial uses billable components of Cloud Platform, including:
- Google App Engine
- Google Stackdriver Monitoring
Stackdriver Monitoring is currently offered to Beta users free of charge.
Before you begin
Cloning the sample application
The sample application is available on GitHub at GoogleCloudPlatform/getting-started-python.
Clone the repository:
git clone https://github.com/GoogleCloudPlatform/getting-started-python.git
Navigate to the sample directory:
Because the application simply returns "Hello World", no configuration is required and you can deploy right away.
gcloud app deploy
You should now be able to see the Hello World message:
Creating an uptime check
With your app deployed, you can use Google Stackdriver Monitoring to create an uptime check that will continually ping your deployed application and ensures it's returning a healthy response. To use Stackdriver, your project must be in a Stackdriver Account.
To use Stackdriver, your project must be in a Workspace:
In the Google Cloud Platform Console, select Stackdriver > Monitoring:
Because your project is not in a Workspace, you see the following message:
Click Create new Stackdriver account and then Continue. You see the following page:
With your project name shown, click Create account.
In the "Add Google Cloud Platform projects to monitor" page, click Continue to skip ahead.
In the "Monitor AWS accounts" page, click Done to skip ahead.
In a few seconds you see the following message:
Click Launch Monitoring.
In the "Get reports by email" page, click No reports and Continue.
You will see your Workspace dashboard. Close the "Welcome to Stackdriver" banner if you don't need it.
You should now see the Stackdriver console and be able to create an Uptime Check. Under Alerting, click Uptime Checks, then click Create an Uptime Check.
- A window might pop up to automatically create an uptime check. In order to manually configure it, 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, which you would use to target a custom URL on a Compute Engine instance.
- Leave Path blank, which defaults the main index page.
- Change Check every to 1 minute, so that the demonstration of the alert will work faster.
Creating an alerting policy
After you create the check, you are automatically prompted to create an associated alerting policy. Alerting policies allow you to manage how Stackdriver informs you of any incidents, such as a failed uptime check.
Click Create Alerting Policy. Give your policy a name like "Hello World Uptime Check Policy".
If you followed the prompt, the Conditions of the alert should be preopopulated with your uptime check. Otherwise, you can click Add Another Condition > Uptime Check Health and re-create it.
In order to receive an email for this alerting policy, under Notifications, keep the Email method, and click Add Notification in order to enter your email address.
Click Save to save this policy.
Simulating an outage
Now that the check is created, you can simulate an outage by changing your app to respond with an HTTP 404 Not Found error rather then a 200 OK response. Looking at the view function in our Hello World application, it currently returns only a 'Hello World' message, and the status code of the response defaults to 200 OK.
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)
In order to have it return a 404 HTTP error code, you can change the return line to add a 404 value 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 should see an email stating 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 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.