Set up a query-based alert by using MQL
This Quickstart shows you how to use Monitoring Query Language (MQL) to explore metrics, create charts, and set up a query-based alert. In this Quickstart, you do the following:
Create a Compute Engine instance.
Navigate to the Query Editor.
Perform a query and create a chart.
Set up a query-based alert.
Before you begin
To use MQL, you need to create a Google Cloud project and enable billing.
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
Create a Compute Engine instance
To have a Google Cloud resource to collect metrics from, create a Compute Engine instance by completing the following steps:
From the Google Cloud console menu, select Compute Engine.
Select Create instance, and then click Create.
Navigate to the Query Editor
To begin using MQL, do the following:
From the Google Cloud console menu, select Monitoring.
From the Monitoring menu, select Metrics Explorer.
Select the MQL tab.
The interface changes to the MQL Query Editor:
Use the Query Editor to construct, test, and run MQL queries.
Perform a simple query and create a chart
Create a simple query that charts the CPU utilization of your instance:
Paste the following query into the Query Editor:
fetch gce_instance::compute.googleapis.com/instance/cpu/utilization | { top 1, max(val()) ; bottom 1, min(val()) } | union
This query selects the time series with the maximum and minimum CPU utilization for charting. For more information on this query, see Combining selections with
union
.Click Run Query. The chart displays two lines, showing your instance's maximum and minimum CPU utilization:
To save the chart, click Save Chart. Give the chart a name and add it to either an existing dashboard or a new one. Then click Save.
The chart can be accessed by going to the Monitoring overview page, or if you saved it to a dashboard, by going to that dashboard's page.
Set up a query-based alert
You can create an alerting policy that monitors the results of a query. To create such an alerting policy, complete the following steps:
From the Google Cloud console menu, select Monitoring and then select Alerting.
From the Alerting page, click Edit Notification Channels. In this step, you configure an email address to receive alert notifications.
On the Notification channels page, scroll down to Email and click Add.
Enter the email address to which you want alert notifications sent in the Email Address field.
Enter a brief description of this channel, like "Alert on-call", in the Display name field.
Click Save.
Go back to the Alerting page by clicking arrow_back Back on the page header.
From the Alerting page, click Create Policy.
This page leads you through the steps to create an alerting policy:
- Describing what you want to alert on.
- Specifying a notification channel for the alert.
- Providing information about how to address this issue.
To describe what you want to alert on, click Add Condition. This action brings up the form-based condtion editor.
Change to the Query Editor view of the condition editor by clicking code Query Editor. This version of the condition editor includes the Query Editor and a Configuration pane:
Enter a name for your condition, like "MQL Quickstart condition".
To be alerted when your instance's CPU usage time stays above 2 seconds, enter the following query:
fetch gce_instance::compute.googleapis.com/instance/cpu/usage_time | window 1m | condition val() > 2 's'
The
condition
operation establishes the performance threshold you want to alert on. In the example, the alerting policy tracks CPU usage time above 2 seconds.Click Run Query to see the selected data and a threshold line:
To be alerted when the your instance's CPU utilization exceeds the the threshold and stays there for 5 minutes, use the Condition pane as follows:
- Change the value of the For field to
5 minutes
. - Don't change the value of the Condition triggers if field.
- Change the value of the For field to
To save your condition in your alerting policy, click Add.
Continue by clicking Save. This action returns you to the Create alerting policy page.
Click Next to move to the next step, setting up notifications.
Select the notification channel you created previously, and click OK.
Click Next to move to the next step, providing steps to fix the issue.
Enter a name for the alerting policy in the Alert name field. This value lets you distinguish among notifications from different alerting policies.
Enter some text to be sent with any alert notification in the Documentation field. This text typically provides responders with information to help them address the problem. For this exercise, enter "Your CPU usage has exceeded 2 seconds."
Save your policy by clicking Save.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Go to the Identity and Access Management page in the Google Cloud console:
Click Shut down delete and follow the instructions.
What's next
- Learn more about using the Query Editor.
- Explore a set of MQL examples.
- Learn more about MQL-based alerting policies.
- Learn how to invoke MQL queries from the Cloud Monitoring API.
- Explore the details of MQL in the MQL Reference.
- Read our resources about DevOps and explore DORA's research program.