Cloud Monitoring and Cloud Logging: Metrics, Dashboards, Alerting Policies, Log Analytics and Sinks
Observability Pillars
| Pillar | Question | GCP tool |
|---|---|---|
| Metrics | How is it performing? | Cloud Monitoring |
| Logs | What happened? | Cloud Logging |
| Traces | Where is the time spent? | Cloud Trace |
| Errors and profiles | What is breaking and which code is slow? | Error Reporting, Cloud Profiler |
Cloud Monitoring
- Collects metrics from Google services automatically (CPU, requests, latency, disk)
- VMs need the Ops Agent for memory, disk usage, process and application logs/metrics
- Metrics scope lets one project view metrics from many projects
- Dashboards (custom or built-in) and Metrics Explorer (build charts with MQL or PromQL)
- Managed Service for Prometheus for Kubernetes and Prometheus metrics
bash
# Install the Ops Agent on a Debian VM
curl -sSO https://dl.google.com/cloudagents/add-google-cloud-ops-agent-repo.sh
sudo bash add-google-cloud-ops-agent-repo.sh --also-install
sudo systemctl status google-cloud-ops-agent"*"
Alerting
- An alerting policy has a condition (for example CPU above 80 percent for 5 minutes), notification channels and documentation for responders
- Channels: email, SMS, mobile app, Slack, PagerDuty, webhooks, Pub/Sub
- Uptime checks probe a URL or TCP port from several locations and alert on failure
- Alert on symptoms users feel (error rate, latency) and on saturation (disk, queue age)
- Use SLO-based alerts (burn rate) for services with objectives
bash
gcloud monitoring channels create --display-name="Ops email" --type=email --channel-labels=email_address=ops@example.com
cat > policy.yaml <<'EOF'
displayName: High CPU on web VMs
combiner: OR
conditions:
- displayName: CPU above 80% for 5 min
conditionThreshold:
filter: 'metric.type="compute.googleapis.com/instance/cpu/utilization" AND resource.type="gce_instance"'
comparison: COMPARISON_GT
thresholdValue: 0.8
duration: 300s
aggregations: [{alignmentPeriod: 60s, perSeriesAligner: ALIGN_MEAN}]
EOF
gcloud alpha monitoring policies create --policy-from-file=policy.yaml --notification-channels=CHANNEL_ID
Cloud Logging
- Collects logs from Google services, VMs (Ops Agent), GKE, Cloud Run and your apps. Write structured JSON logs for easy filtering
- Query in Logs Explorer with the Logging query language
- Log buckets store logs with a retention period (
_Default30 days,_Required400 days for audit logs) - Log Analytics lets you run SQL on logs and link them to BigQuery
bash
gcloud logging read 'resource.type="cloud_run_revision" AND severity>=ERROR' --limit=10 --freshness=1h
gcloud logging write app-log '{"event":"checkout","status":"failed","orderId":42}' --payload-type=json --severity=ERROR
Log Router and Sinks
text
Log entries ─► Log Router ─► _Default bucket (search, 30 days)
├─► Sink: BigQuery (analysis)
├─► Sink: Cloud Storage (cheap long-term archive, compliance)
├─► Sink: Pub/Sub (stream to SIEM or other tools)
└─► Exclusion filters (drop noisy logs to save cost)
bash
gcloud logging sinks create audit-to-bq bigquery.googleapis.com/projects/sec-proj/datasets/audit --log-filter='logName:"cloudaudit.googleapis.com"' --include-children --organization=987654321
gcloud logging metrics create checkout_errors --description="Failed checkouts" --log-filter='jsonPayload.event="checkout" AND jsonPayload.status="failed"'
- Log-based metrics turn log patterns into metrics you can chart and alert on
- Use aggregated sinks at the organization level and send to a central security project
- Control cost with exclusion filters and shorter retention for debug logs
Best Practices
- Install the Ops Agent on every VM
- Use structured logs with severity and request IDs
- Create dashboards per application and one for the whole platform
- Alert on user-facing symptoms, with documentation links
- Centralize audit logs and keep them immutable
- Review costs from logs and metrics monthly
Practice
- Install the Ops Agent and view memory metrics
- Create an alert for high CPU and test it
- Write a log entry and find it in Logs Explorer
- Create a sink that exports errors to BigQuery
- Build a dashboard with CPU, requests and error rate
Conclusion
Cloud Monitoring collects metrics and alerts, Cloud Logging collects and routes logs, and the Ops Agent covers VMs. Use uptime checks, SLO-aware alerts, sinks for retention and analysis, and structured logs.
Quick Summary: pillars; Monitoring; alerting; Logging; router; practices.

