Google Cloud · lesson 9 of 10
Also part of: Multi-Cloud
Cloud Monitoring Basics
Read a Compute Engine metric with gcloud, turn a log pattern into a log-based metric, and wire both into a real alerting policy.
By SkillByExample · Published
Cloud Monitoring collects metrics automatically for most GCP resources, but the two things
that actually make it useful — turning application log lines into metrics, and getting paged
when something crosses a threshold — both require deliberate setup. This lesson does both
against the web-1 VM from the Compute Engine lesson.
Reading a metric that already exists
gcloud monitoring time-series list \
--filter='metric.type="compute.googleapis.com/instance/cpu/utilization" AND resource.labels.instance_id="4123456789012345678"' \
--format="table(points[0].value.doubleValue, points[0].interval.endTime)"
DOUBLE_VALUE END_TIME
0.0412 2026-09-13T10:59:47Z
0.0398 2026-09-13T10:58:47Z
0.0405 2026-09-13T10:57:47Z
CPU utilization here is a fraction (0.0412 = 4.12%), collected every 60 seconds with zero setup — this is the baseline every GCP resource ships with, before any custom instrumentation.
Creating a log-based metric
Suppose the app on web-1 writes a line like ERROR: payment declined to its logs on
failure. Turn occurrences of that string into a counted metric:
gcloud logging metrics create payment-declines \
--description="Count of declined payment attempts" \
--log-filter='resource.type="gce_instance" AND textPayload:"payment declined"'
Created metric [payment-declines].
gcloud logging metrics list --format="table(name,filter)"
NAME FILTER
payment-declines resource.type="gce_instance" AND textPayload:"payment declined"
From this point forward, every matching log line increments a real Monitoring metric named
logging.googleapis.com/user/payment-declines — visible in the same time-series API as any
built-in metric.
Generating log entries to see the metric move
gcloud compute ssh web-1 --zone=us-central1-a \
--command='logger "ERROR: payment declined for order 4471"'
Updating project ssh metadata...done.
gcloud monitoring time-series list \
--filter='metric.type="logging.googleapis.com/user/payment-declines"' \
--format="table(points[0].value.int64Value, points[0].interval.endTime)"
INT64_VALUE END_TIME
1 2026-09-13T11:04:12Z
Creating an alerting policy
cat > policy.json <<'EOF'
{
"displayName": "High payment decline rate",
"combiner": "OR",
"conditions": [
{
"displayName": "More than 5 declines in 5 minutes",
"conditionThreshold": {
"filter": "metric.type=\"logging.googleapis.com/user/payment-declines\" AND resource.type=\"gce_instance\"",
"comparison": "COMPARISON_GT",
"thresholdValue": 5,
"duration": "300s",
"aggregations": [{"alignmentPeriod": "300s", "perSeriesAligner": "ALIGN_SUM"}]
}
}
],
"notificationChannels": []
}
EOF
gcloud alpha monitoring policies create --policy-from-file=policy.json
Created alertPolicies/12345678901234567890.
duration: 300s means five declines have to accumulate and stay over the threshold for five
minutes — a single burst that clears immediately won’t page anyone, by design.
Practice
1. Attach an email notification channel to the policy created above.
gcloud alpha monitoring channels create \
--display-name="On-call email" \
--type=email \
--channel-labels=email_address=you@gmail.com
Created notification channel [projects/skillbyexample-437219/notificationChannels/9988776655].
gcloud alpha monitoring policies update alertPolicies/12345678901234567890 \
--add-notification-channels=projects/skillbyexample-437219/notificationChannels/9988776655
Updated.
A policy with an empty notificationChannels list, as created above, will fire silently in the
Console but never actually notify anyone — this step is what makes it page.
2. List all existing alerting policies on the project.
gcloud alpha monitoring policies list --format="table(displayName,enabled)"
DISPLAY_NAME ENABLED
High payment decline rate True
3. Generate enough log entries to actually trigger the alert, and check its incident state.
for i in $(seq 1 6); do
gcloud compute ssh web-1 --zone=us-central1-a \
--command="logger \"ERROR: payment declined for order 44$i\""
done
gcloud alpha monitoring policies describe alertPolicies/12345678901234567890 \
--format="value(name)" | xargs -I{} gcloud logging read \
'resource.type="cloud_monitoring_condition"' --limit=1
timestamp: '2026-09-13T11:12:04Z'
severity: WARNING
textPayload: 'Incident opened for policy "High payment decline rate"'
Six declines cleared the threshold of five, and after the five-minute sustained window an incident opened — visible in the Console under Monitoring → Alerting → Incidents.
4. Create a dashboard widget-free check: query the metric over the last hour with a chart-friendly aggregation.
gcloud monitoring time-series list \
--filter='metric.type="logging.googleapis.com/user/payment-declines"' \
--interval-start-time="$(date -u -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ)" \
--interval-end-time="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
points:
- interval:
endTime: '2026-09-13T11:12:00Z'
value:
int64Value: '6'
- interval:
endTime: '2026-09-13T11:04:00Z'
value:
int64Value: '1'
5. Delete the alerting policy and the log-based metric.
gcloud alpha monitoring policies delete alertPolicies/12345678901234567890 --quiet
gcloud logging metrics delete payment-declines --quiet
Deleted.
Deleted [payment-declines].
The underlying log entries in Cloud Logging aren’t affected by either deletion — only the metric definition and the policy watching it are removed.
Next: GCP cost and billing — reading a billing report, the architecture framework pillars, and a real On-Demand vs Committed Use vs Spot price comparison.