arrow_back

Monitoring Applications in Google Cloud

Sign in Join
Get access to 700+ labs and courses

Monitoring Applications in Google Cloud

Lab 1 hour 30 minutes universal_currency_alt 5 Credits show_chart Introductory
info This lab may incorporate AI tools to support your learning.
Get access to 700+ labs and courses

Overview

In this lab, you will deploy an application to Google Cloud and then use the tools provided by Google Cloud to monitor it. You will use Cloud Logging, Trace, Profiler, and dashboards and create uptime checks and alerting policies.

Objectives

In this lab, you will learn how to perform the following tasks:

  • Download a sample app from Github
  • Deploy an application to App Engine
  • Examine the Cloud logs
  • View Profiler information
  • Explore Cloud Trace
  • Monitor resources using dashboards
  • Create uptime checks and alerts

Setup

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left, or type the service or product name in the Search field.

Task 1. Download a sample app from Github

Download a sample application from GitHub and preview it in Cloud Shell.

  1. In the Cloud Console, click Activate Cloud Shell ().

  2. If prompted, click Continue. Once connected to Cloud Shell, you should see that you are already authenticated and that the project is already set to your project ID.

  3. Run the following command in Cloud Shell to confirm that you are authenticated. If prompted, click Authorize:

gcloud auth list

Command output:

Credentialed Accounts ACTIVE ACCOUNT * [my_account]@[my_domain.com] To set the active account, run: $ gcloud config set account `ACCOUNT` Note: The gcloud command-line tool is the powerful and unified command-line tool in Google Cloud. It comes preinstalled in Cloud Shell. Among its features, gcloud offers tab completion in the shell. For more information, see gcloud command-line tool overview.
  1. Run the following command to confirm that you are using the correct project for this lab:
gcloud config list project

Command output

[core] project = [PROJECT_ID]
  1. If the correct project is not listed, you can set it with this command:
gcloud config set project [PROJECT_ID]

Command output:

Updated property [core/project].
  1. To create a folder called gcp-logging, run the following command:
mkdir gcp-logging
  1. Change to the folder you just created:
cd gcp-logging
  1. Downlaod a simple Python Flask app from Cloud storage:
gcloud storage cp gs://cloud-training/CBL175/design-process.zip .
  1. Unzip design-process.zip folder
unzip design-process.zip
  1. Change to the deploying-apps-to-gcp folder:
cd design-process/deploying-apps-to-gcp
  1. In Cloud Shell, click Open Editor ().

  2. Expand the gcp-logging/design-process/deploying-apps-to-gcp folder in the navigation pane, and then click main.py to open it.

  3. Add the following import statement at the top of the file (line 2):

import googlecloudprofiler Note: Profiler allows you to monitor the resources your applications use. For more information, refer to the Google Cloud Profiler documentation.
  1. After the main() function, add the following code snippet to start Profiler (after line 11):
try: googlecloudprofiler.start(verbose=3) except (ValueError, NotImplementedError) as exc: print(exc)

Profiler will continuously report application metrics. Your code should look like this:

Note: This code simply turns Profiler on. Once on, Profiler starts reporting application metrics to Google Cloud.
  1. You also have to add the Profiler library to your requirements.txt file. Open that file in the code editor and add the following:
google-cloud-profiler==3.0.6 protobuf==3.20.1

The file should look like this:

  1. Profiler has to be enabled in the project. In Cloud Shell, enter the following command:
gcloud services enable cloudprofiler.googleapis.com
  1. To test the program, enter the following command to build a Docker container of the image:
docker build -t test-python .
  1. To run the Docker image, enter the following command:
docker run --rm -p 8080:8080 test-python
  1. To see the program running, click Web Preview () in the Google Cloud Shell toolbar. Then select Preview on port 8080.

The program should be displayed in a new browser tab.

  1. In Cloud Shell, type Ctrl+C to stop the program.

Click Check my progress to verify the objective. Enable the Profiler

Task 2. Deploy an application to App Engine and examine the Cloud logs

Now you will deploy the program to App Engine and use Google Cloud tools to monitor it.

  1. In the Cloud Shell code editor, in the Explorer pane, select the gcp-logging/design-process/deploying-apps-to-gcp folder.
  2. Click New File, and then name the file app.yaml.
  3. Paste the following into the file you just created:
runtime: python39
  1. Save your changes.

  2. In a project, an App Engine application has to be created. This is done just once using the gcloud app create command and specifying the region where you want the app to be created. In Cloud Shell, type the following command:

    gcloud app create --region={{{project_0.startup_script.app_region | REGION }}}
  3. Now deploy your app with the following command:

gcloud app deploy --version=one --quiet Note: This command will take a couple of minutes to complete. Wait for it to complete before continuing.
  1. In the Google Cloud console title bar, type App Engine in the Search field, then click App Engine from the search result.

  2. Click App Engine > Dashboard. The upper-right corner of the dashboard should display a link to your application similar to this:

Note: By default, the URL to an App Engine instance is in the form of https://project-id/appspot.com.
  1. Click on the link to test your program.

  2. Refresh your browser a few times to make some requests.

  3. Return to the Console and click the App Engine > Versions.

  4. In Diagnose column of the table click Logs.

  5. The logs should indicate that Profiler has started and profiles are being generated. If you get to this point too quickly, wait a minute and click Refresh.

Click Check my progress to verify the objective. Deploy an application to App Engine and examine the Cloud logs

Task 3. View Profiler information

  1. In the Google Cloud console title bar, type Profiler in the Search field, then click Profiler from the search result. The screen should look similar to this:

Note: The gray bar at the top represents the total amount of CPU time used by the program. The bars below represent the amount of CPU time used by the program's functions relative to the total. At this point, there is no traffic, so the chart is not very interesting. Throw some load at the application.
  1. In the Navigation menu, click Compute Engine.
  2. Click Create Instance to create a virtual machine.
  3. Select the region to

(Since it is different from the region of the App Engine app).

Accept all the rest of the defaults and click Create.

Click Check my progress to verify the objective. Create an instance

  1. When the VM is ready, click SSH to log in to it.

  2. If prompted allow SSH-in-browser to connect to VMs, click Authorize.

  3. You will generate some traffic to your App Engine app using the web testing tool called Apache Bench. Enter the following commands to install it:

sudo apt update sudo apt install apache2-utils -y
  1. Replace <your-project-id> with your PROJECT_ID from connection details panel and enter the following command to generate some traffic to your App Engine application:
ab -n 1000 -c 10 https://<your-project-id>.appspot.com/

The command will make a thousand requests, 10 at a time, to your application.

Note: You have to change the URL to point to your application. Recall that you can find the URL in the App Engine Dashboard. It is also on the browser tab you used to test your app, if you haven't closed it. Also, make sure you insert a slash (/) at the end of the URL.
  1. When the requests are finished, in the Google Cloud console title bar, type Profiler in the Search field, then click Profiler from the search result.

Now there is a more interesting chart. Each bar represents a function. The width of the bars represents how much CPU time each function consumed.

The Profiler is a way developers can track down parts of a program that are consuming too many resources.

Task 4. Explore Cloud Trace

  1. Every request to your application is added to the Trace list. In the Google Cloud console title bar, type Trace Explorer in the Search field, then click Trace Explorer from the search result.

This shows a history of requests and their latency. Again, it's not very exciting because the application hasn't been running for very long. The chart in the upper-left plots requests and how long they took. The table to the right shows a list of requests. If you select a request, more detail will be displayed at the bottom of the screen.

  1. Return to the SSH window where you entered the Apache Bench command previously.

  2. Replace <your-project-id> with your PROJECT_ID from connection details panel and enter the following ab command:

ab -n 1000 -c 10 https://<your-project-id>.appspot.com/

You can also experiment with different values for the -n and -c parameters.

  1. Repeat this a couple of times, and then return to the Trace Explorer page.

Task 5. Monitor resources using Dashboards

  1. In the Navigation menu, click Monitoring.

  2. In the left pane, click Dashboards. Cloud Monitoring analyzes the resources used in your projects and generates some default dashboards for you. In this exercise you have used App Engine and Compute Engine virtual machines, so a table similar to the one shown below should be displayed:

  1. Click on the App Engine dashboard, and then select your project name from the dropdown. A dashboard of pertinent information for your App Engine application will appear.

  2. In the left pane, click Dashboards.

  3. Click on the VM Instances dashboard, and then select your instance. A dashboard for your VM will appear.

Note: If you don't see VM Instances right away, wait a minute and refresh your browser.
  1. Alternatively, return to the Dashboards page and click the Create Custom Dashboard. Try to create a custom dashboard.
  2. For New Dashboard Name, type the custom dashboard name you have chosen. You can continue with your custom dashboard by adding the charts.

Task 6. Create uptime checks and alerts

  1. In the left pane, click Uptime checks, and then click the + Create Uptime Check link at the top. Fill out the form as follows:
Property Value
Protocol HTTPS
Resource Type URL
Hostname <your-project-id>.appspot.com
Path /
Check Frequency 1 minute
  1. Click Continue.
  2. For Alert & Notification click on the drop down arrow next to Notification Channels, then click on Manage Notification Channels. A Notification channels page will open in a new tab.
  3. Scroll down the page and click on ADD NEW for Email.
  4. In the Create Email Channel dialog box, enter your personal email address in the Email Address field and a Display name.
  5. Click on Save.
  6. Go back to the previous tab. Click on Notification Channels again, then click on the Refresh icon to get the display name you mentioned in the previous step.
  7. Now, select your Display name and click OK.
  8. Name the alert policy as Uptime Check Alert.
  9. Click Continue.
  10. In the Review section enter App Engine Uptime Check in Title.
  11. Click Test to verify that your uptime check can connect to the resource. When you see a green check mark everything can connect. Click Create.

Click Check my progress to verify the objective. Create uptime checks and alerts

  1. Return to the open App Engine tab in order to disable the application to see whether your uptime check and alerting policy work.

  2. Click Settings.

  3. Click Disable application. Follow the instructions to disable the application.

  4. Return to the App Engine Dashboard and test the URL. It shouldn't work anymore.

  5. Return to the tab that contains Monitoring, and then click Uptime checks. Your uptime check should be failing. If you get there too fast, wait a minute and click refresh.

  6. Click Alerting. An incident should have been fired.

  7. Check your email. You should get a message from Cloud Monitoring.

  8. Return to App Engine Settings and re-enable your application.Then return to the Uptime checks page. The uptime check should be working again. If not, wait a minute and then click refresh.

  9. Return to the Alerting page. Your incident should be resolved. As before, you might have to wait a minute and then click refresh.

  10. Check your email again. You should get a second email indicating that the alert recovered.

  11. To make sure you don't get any emails after the project is deleted, delete your alerting policy and then delete your notification channel. At the top of the Alerting page, click Edit Notification Channels.

  12. Find your email address and click the trash can icon to delete it.

  13. Now click Uptime checks and delete your App Engine Uptime check.

Congratulations!

In this lab, you deployed an application to Google Cloud and then used the tools provided by Google Cloud to monitor it. You used Cloud Logging, Trace, Profiler, and dashboards and created uptime checks and alerting policies.

End your lab

When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.

You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

The number of stars indicates the following:

  • 1 star = Very dissatisfied
  • 2 stars = Dissatisfied
  • 3 stars = Neutral
  • 4 stars = Satisfied
  • 5 stars = Very satisfied

You can close the dialog box if you don't want to provide feedback.

For feedback, suggestions, or corrections, please use the Support tab.

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Before you begin

  1. Labs create a Google Cloud project and resources for a fixed time
  2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
  3. On the top left of your screen, click Start lab to begin

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available

One lab at a time

Confirm to end all existing labs and start this one

Use private browsing to run the lab

Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.