This tutorial shows how to set up an uptime alert for the
[Python Hello World app](https://docs.cloud.google.com/python/getting-started/hello-world)
running on App Engine flexible environment using
[Cloud Monitoring](https://docs.cloud.google.com/monitoring/docs.md.txt).
Uptime alerts let you know when your app is not serving traffic. You can also set
uptime alerts for apps running on [Compute Engine](https://docs.cloud.google.com/compute)
or [Google Kubernetes Engine (GKE)](https://docs.cloud.google.com/kubernetes-engine).

## 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


> [!IMPORTANT]
> **Important:** In this document, you use the following billable components of Google Cloud:
>
> - [App Engine](https://cloud.google.com/appengine/pricing)
> - [Cloud Monitoring](https://cloud.google.com/stackdriver/pricing)
>
> To generate a cost estimate based on your projected usage, use the [pricing calculator](https://docs.cloud.google.com/products/calculator).

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
[`GoogleCloudPlatform/getting-started-python`](https://github.com/GoogleCloudPlatform/getting-started-python).

1. Clone the repository.

   ```python
   git clone https://github.com/GoogleCloudPlatform/getting-started-python.git
   ```
2. Go to the sample directory.

   ```python
   cd getting-started-python/gce
   ```
3. Because the app only returns "Hello World!", it requires no configuration, and
   you can run it right away.

   ```python
   gcloud app deploy
   ```
4. To see the returned message, enter the following URL in your browser:

   `https://PROJECT_ID.REGION_ID.r.appspot.com`

   Replace the following:
   - `PROJECT_ID`: Your Google Cloud project ID
   - `REGION_ID`: A [code that App Engine assigns
     to your app](https://docs.cloud.google.com/appengine/docs/standard/python3/how-requests-are-routed#region-id)

## 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.

1. In the Google Cloud console, go to the ![](https://docs.cloud.google.com/static/monitoring/images/uptime-check-icon.png) **Uptime checks** page:

   [Go to **Uptime checks**](https://console.cloud.google.com/monitoring/uptime)

   <br />

   If you use the search bar to find this page, then select the result whose subheading is
   **Monitoring**.
2. Click **Create Uptime Check**.
3. Give your check a title, such as `Check Hello World`,
   and then click **Next**.

4. In the **Target** section, you specify what the uptime check is to monitor:

   1. 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.)
   2. Select the **Service** to be monitored by the uptime check.
   3. Leave **Path** blank to default to the main index page.
   4. Leave **Check frequency** at the default of **1** minute.
   5. Click **Next**.
5. Leave the **Response Validation** fields at their default values and
   click **Next**.

6. In the **Alert \& Notification** section, 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 channels** and follow the instructions. When you
     return to this dialog, click
     **Refresh** .
7. Click **Create** . When the create action is successful, the message
   **Check and alert created** is displayed and then the **Uptime checks**
   dashboard 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 **Alerting** page.
   From the alerting page, the **Policies** pane 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.

1. 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.

   ```python
   # Copyright 2019 Google LLC All Rights Reserved.
   #
   # Licensed under the Apache License, Version 2.0 (the "License");
   # you may not use this file except in compliance with the License.
   # You may obtain a copy of the License at
   #
   #     http://www.apache.org/licenses/LICENSE-2.0
   #
   # Unless required by applicable law or agreed to in writing, software
   # distributed under the License is distributed on an "AS IS" BASIS,
   # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   # See the License for the specific language governing permissions and
   # limitations under the License.

   from flask import Flask
   app = Flask(__name__)


   @app.route('/', methods=['GET'])
   def say_hello():
       return "Hello, world!"


   if __name__ == '__main__':
       app.run(host='127.0.0.1', port=8080, debug=True)
   ```
2. To cause the Hello World app to return an HTTP `404` error code, change the
   return line by adding a `404` value to the second part of the return value.

   ```bash
   return 'Hello World', 404
   ```
3. Deploy the new, intentionally broken app.

   ```bash
   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:

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
2. In the project list, select the project that you want to delete, and then click **Delete**.
3. In the dialog, type the project ID, and then click **Shut down** to delete the project.

<br />

<br />

\< Prev [Next \>](https://docs.cloud.google.com/python/monitor-and-debug/reporting-errors.md.txt)