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

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

    If you don't already have one, sign up for a new account.

  2. Select or create a GCP project.

    Go to the Project selector page

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

    Learn how to enable billing

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/1-hello-world
    
  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 address. Replace [YOUR_PROJECT_ID] with your GCP project ID.

    https://[YOUR_PROJECT_ID].appspot.com
    

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.

  1. View your Workspace in Google Cloud Platform Console.

    Go to Workspace

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

    You do not have a Stackdriver account

  3. In the Create a Stackdriver account page, select your project name, and then click Create Account.

    Create a Stackdriver account

  4. In the Add Google Cloud Platform projects to monitor page, click Continue.

  5. In the Monitor AWS accounts page, click Done.

  6. After a few seconds, you see a message that says the initial collection is finished. Click Launch Monitoring.

    Finished initial collection

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

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

  1. Under Alerting, click Uptime Checks, then click Create an Uptime Check.

  2. If a dialog opens asking to automatically create an uptime check, click No thanks.

  3. Give your check a title, such as Check Hello World.

  4. 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.)

  5. Leave Path blank to default to the main index page.

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

  1. To follow the prompt, click Create Alerting Policy. Give your policy a name, such as Hello World Uptime Check Policy.

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

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

  4. Click Save.

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.

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

Cleaning up

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:

  1. In the GCP Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete .
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Trang này có hữu ích không? Hãy cho chúng tôi biết đánh giá của bạn:

Gửi phản hồi về...