arrow_back

Monitoring and Logging for Cloud Functions

Gabung Login

Monitoring and Logging for Cloud Functions

45 menit 1 Kredit

GSP092

Google Cloud self-paced labs logo

You can view your Cloud Functions with their execution times, execution counts, and memory usage in the Cloud Console using Cloud Monitoring, where you can set up custom alerting on these metrics.

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: 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.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  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 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 Console. 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 from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. 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 Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:

gcloud auth list

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Viewing Cloud Function logs & metrics in Cloud monitoring

Before you collect logs and alerts, you need something to monitor. In this section, you create a Hello World cloud function to monitor.

  1. In the Cloud Console, select Navigation menu > Cloud Functions, and then CREATE FUNCTION.

cloud_nav_1.png

  1. Set the following:

  • Function Name: helloWorld
  • Region:
  • Trigger type: HTTP
  • Authentication: check the box next to Allow unauthenticated invocations

Click SAVE.

  1. Expand Runtime, build, connections and security settings. Under Autoscaling, set the Maximum number of instances to 5. Click NEXT.

  2. You will see the following:

cloudfunction_ui.png

  1. Click DEPLOY.

The cloud function automatically deploys and is listed on the Cloud Function page. This takes a few minutes. When you see a green check mark next to the name, the cloud function is complete.

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted with an assessment score.

Creating a Cloud Function
  1. In Cloud Shell, run the following to get a tool called vegeta that will let you send some test traffic to your cloud function:

curl -LO 'https://github.com/tsenart/vegeta/releases/download/v6.3.0/vegeta-v6.3.0-linux-386.tar.gz'
  1. Unpack the vegeta tool by running the following:

tar xvzf vegeta-v6.3.0-linux-386.tar.gz
  1. Still in the Cloud Functions page, click the name of your function, and then click on the Trigger tab. Click the Trigger URL for your function.

trigger_tab.png

If you see Hello World! in the new browser tab that opens, you're up and running!

output.png

  1. Now send traffic to your cloud function. Run the following in Cloud Shell.
echo "GET https://{{{ project_0.default_region }}}-{{{ project_0.project_id }}}.cloudfunctions.net/helloWorld" | ./vegeta attack -duration=300s > results.bin

Task 2. Create logs-based metric

Now you'll create a Distribution type logs based metric using a regular expression to extract the value of latency from the log entries textPayload field.

  1. In the Console, select Navigation menu > Logging > Logs Explorer. The Cloud Logging opens in the Console.

  2. To look at just the logs from your Cloud Function, in the Resource dropdown, select Cloud Function > helloWorld then click Add. In the Log name dropdown, select cloud-functions checkbox then click Add.

  3. Click Run query.

cl-function_CF-Logs.png

  1. Click Create metric.

function-logs.png

  1. In the Create logs metric:

  • Change the Metric Type to Distribution.

  • Name your metric CloudFunctionLatency-Logs.

  • Enter textPayload for Field name.

  • Enter the following in the Regular Expression field.

execution took (\d+)

The Create logs metric should look like this:

metric_editor.png

  1. Click CREATE METRIC.

Now you'll see your user-defined metric added to your Logs-based Metrics page.

user_defined_metric_1.png

Test Completed Task

Click Check my progress to verify your performed task. If you have completed the task successfully you will be granted with an assessment score.

Create logs-based metric

Task 3. Metrics Explorer

Next, use Metrics Explorer to look at the data for your cloud function.

Create a Monitoring Metrics Scope

Set up a Monitoring Metrics Scope that's tied to your Google Cloud Project. The following steps create a new account that has a free trial of Monitoring.

  • In the Cloud Console, click Navigation menu Navigation menu icon > Monitoring.

When the Monitoring Overview page opens, your metrics scope project is ready.

  1. In the left menu, click Metrics explorer.

  2. Start typing executions into the Select a Metric dropdown, and then select Cloud Function > Function > Executions from the suggested metrics and click Apply.

executions_1.png

  1. Change the graph type to Stacked bar chart using the dropdown menu above the graph.

  2. Explore other graph options, try a different metric. For example, click your current CLOUD FUNCTION - EXECUTIONS metric to open the dropdown, select Execution times, and change the graph type to Heatmap chart.

execution_times.png

  1. Continue to explore and experiment. For example, go back to the Executions metric and change the aggregator to the 95th percentile. Select the graph type Line.

execution_95percen.png

Task 4. Create charts on the Monitoring Overview window

Creating charts on the Monitoring Overview window is a great way to track metrics that are important to you. In this section, you set up the same charts you created in the previous section, but now they'll be saved into the Monitoring Overview window.

  1. In the left menu, click Dashboard.

  2. Click on + CREATE DASHBOARD.

  3. Click on + ADD CHART.

  4. In dropdown menu, select Stacked bar

Note: If the Add chart option is greyed out then delete existing charts from the overview page.
  1. In the Resource & Metric section, select the default VM INSTANCE - CPU UTILIZATION metric to open the dropdown and change the metric.

  2. Start typing executions into the Select a Metric dropdown, and then select Cloud Function > Function > Executions from the suggested metrics and click Apply.

  3. After you create the first chart, click + ADD CHART > Heatmap to create the next one.

  4. In the Resource & Metric section, select the default VM INSTANCE - RTT LATENCIES metric to open the dropdown and change the metric.

  5. Start typing execution times into the Select a Metric dropdown, and then select Cloud Function > Function > Execution times from the suggested metrics and click Apply.

By default, the charts name themselves after the metric you're using, but you can rename them.

For a quick reference, you can see these charts in the Monitoring Dashboard section.

charts_section.png

Task 5. Test your Understanding

Below are multiple-choice questions to reinforce your understanding of this lab's concepts. Answer them to the best of your abilities.

Congratulations!

Google Cloud's Operations Suite Quest badge Cloud Logging Quest

Finish Your Quest

This self-paced lab is part of the Google Cloud's Operations Suite and Cloud Logging Quests. A Quest is a series of related labs that form a learning path. Completing a Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in either Quest and get immediate completion credit if you've taken this lab. See other available Quests.

Take Your Next Lab

Continue your Quest with Autoscaling an Instance Group with Custom Cloud Monitoring Metrics, or check out these suggestions:

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated Aug 4, 2022
Lab Last Tested Aug 4, 2022

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.