Setting an uptime alert

This tutorial shows how to set up an uptime alert for thePython Hello World app running on App Engine flexible environment usingCloud Monitoring.Uptime alerts let you know when your app is not serving traffic. You can also setuptime alerts for apps running onCompute Engine orGoogle 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 uptimecheck fails.
  • Restart the app to trigger thealert.

Costs

Important: In this document, you use the following billable components of Google Cloud:

To generate a cost estimate based on your projected usage, use thepricing calculator.
New Google Cloud users might be eligible for afree trial.

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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  3. Verify 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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission.Learn how to grant roles.
    Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to project selector

  5. Verify that billing is enabled for your Google Cloud project.

Cloning the sample app

The sample app is available on GitHub atGoogleCloudPlatform/getting-started-python.

  1. Clone the repository.

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

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

    gcloudappdeploy
  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 checkcontinually pings your deployed app to ensure that it's returning a healthyresponse.

  1. In the Google Cloud console, go to the Uptime checks page:

    Go toUptime checks

    If you use the search bar to find this page, then select the result whose subheading isMonitoring.

  2. ClickCreate Uptime Check.
  3. Give your check a title, such asCheck Hello World,and then clickNext.

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

    1. Because you deployed to App Engine, changeResource Type toApp Engine instead ofURL. (URL is for generating a customURL on a Compute Engine instance.)
    2. Select theService to be monitored by the uptime check.
    3. LeavePath blank to default to the main index page.
    4. LeaveCheck frequency at the default of1 minute.
    5. ClickNext.
  5. Leave theResponse Validation fields at their default values andclickNext.

  6. In theAlert & Notification section, you specify how you are notifiedif an alert occurs:

    • Ensure that the toggle's label isAlerting is enabled.
    • Leave the name and duration fields at their default values.
    • To add a notification channel to the alerting policy, in thetext box labeledNotification channels, clickMenu.Select the channels to add and clickOK.The notifications are grouped alphabetically for each channel type.

      To add an entry to the checkbox list, clickManage notification channels and follow the instructions. When youreturn to this dialog, clickRefresh.

  7. ClickCreate. When the create action is successful, the messageCheck and alert created is displayed and then theUptime checksdashboard page is displayed.

    In the uptime checks dashboard, your new uptime check is listed. If youclick the check name, then you open the detail view for that uptime check.This view displays several charts, shows the uptime percentage and theconfiguration information, and lists the configured alert policies.To view a policy, click its name.

    You can also view the alert policy by starting at theAlerting page.From the alerting page, thePolicies pane lists a subset of policies.To view a list of all policies, clickSee all policies.

Simulating an outage

Now that the uptime check is created, you can simulate an outage by changingyour app to respond with an HTTP404 Sorry, we can't find that page errorrather than an HTTP200 OK response.

  1. The following code shows where the Hello World app returns only a 'HelloWorld!' message, and that the status code of the response defaults to200 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.fromflaskimportFlaskapp=Flask(__name__)@app.route('/',methods=['GET'])defsay_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 HTTP404 error code, change thereturn line by adding a404 value to the second part of the return value.

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

    gcloudappdeploy

    Within half an hour, you'll receive an email message that states that youruptime 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: 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 anappspot.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 theManage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then clickDelete.
  3. In the dialog, type the project ID, and then clickShut down to delete the project.

< PrevNext >

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.