Setting an uptime alert


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

Monitoring is currently offered to beta users at no charge.

Before you begin

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure 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.

  1. Clone the repository.

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

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

    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:

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 navigation panel of the Google Cloud console, select Monitoring, and then select  Uptime checks:

    Go to Uptime checks

  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.

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

    return 'Hello World', 404
  3. 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:

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

    Go to Manage resources

  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.