Skip to main content

Command Palette

Search for a command to run...

Creating and Alerting on Logs-based Metrics (GSP091)

Updated
โ€ข4 min read

Creating and Alerting on Logs-based Metrics (GSP091)


๐Ÿ”Ž What You Learn in This Lab

You learn how to:

  • โœ… Create a Log-based Alert

  • โœ… Create a Log-based Metric

  • โœ… Create a Metrics-based Alert

  • โœ… Deploy GKE + Application

  • โœ… Generate errors to trigger alerts

  • โœ… Monitor incidents in Cloud Monitoring


๐Ÿง  Important Concepts (Very Clear Understanding)

1๏ธโƒฃ Cloud Logging

Collects logs from:

  • VM instances

  • GKE clusters

  • Applications

  • Google services


2๏ธโƒฃ Log-Based Metrics

Two types:

Type Who Creates Description
System-defined Google Auto-created from logs
User-defined You Custom filter-based metrics

3๏ธโƒฃ Alerts

Two types used in lab:

Alert Type Based On
Log-based Alert Directly from log query
Metrics-based Alert From a log-based metric

๐Ÿ› ๏ธ LAB PRACTICAL STEPS (Step-by-Step)


โœ… Task 1: Deploy GKE Cluster

Set Zone

gcloud config set compute/zone us-central1-a

Set Project

export PROJECT_ID=$(gcloud info --format='value(config.project)')

Create Cluster

gcloud container clusters create gmp-cluster --num-nodes=1 --zone us-central1-a

Wait until:

STATUS: RUNNING

โœ… Task 2: Create Log-Based Alert (VM Stop Alert)

Go to:

Search โ†’ Logs Explorer

Use Query:

resource.type="gce_instance"
protoPayload.methodName="v1.compute.instances.stop"

Click Create Log Alert

Fill:

  • Alert name โ†’ stopped vm

  • Notification frequency โ†’ 5 minutes

  • Auto close โ†’ 1 hour

  • Add Email notification

Save.


๐Ÿงช Test It

Go to:

Navigation โ†’ Compute Engine โ†’ VM Instances

Stop instance1

Then go to:

Monitoring โ†’ Alerting โ†’ See incident

You should see alert triggered.


โœ… Task 3: Create Docker Repository

Create Artifact Registry Repository

gcloud artifacts repositories create docker-repo \
--repository-format=docker \
--location=us-central1 \
--description="Docker repository" \
--project=$PROJECT_ID

Load Pre-built Image

wget https://storage.googleapis.com/spls/gsp1024/flask_telemetry.zip
unzip flask_telemetry.zip
docker load -i flask_telemetry.tar

Tag Image

docker tag gcr.io/ops-demo-330920/flask_telemetry:61a2a7aabc7077ef474eb24f4b69faeab47deed9 \
us-central1-docker.pkg.dev/$PROJECT_ID/docker-repo/flask-telemetry:v1

Push Image

docker push us-central1-docker.pkg.dev/$PROJECT_ID/docker-repo/flask-telemetry:v1

โœ… Task 4: Deploy App That Emits Metrics

Authenticate Cluster

gcloud container clusters get-credentials gmp-cluster

Create Namespace

kubectl create ns gmp-test

Download Setup

wget https://storage.googleapis.com/spls/gsp1024/gmp_prom_setup.zip
unzip gmp_prom_setup.zip
cd gmp_prom_setup

Edit Image Name

Open file:

nano flask_deployment.yaml

Replace:

<ARTIFACT REGISTRY IMAGE NAME>

With:

us-central1-docker.pkg.dev/$PROJECT_ID/docker-repo/flask-telemetry:v1

Save.


Deploy App

kubectl -n gmp-test apply -f flask_deployment.yaml
kubectl -n gmp-test apply -f flask_service.yaml

Get External IP

kubectl get services -n gmp-test

Check Metrics Endpoint

curl $(kubectl get services -n gmp-test -o jsonpath='{.items[*].status.loadBalancer.ingress[0].ip}')/metrics

You should see:

flask_exporter_info

โœ… Task 5: Create Log-Based Metric

Go to:

Logs Explorer โ†’ Create Metric

Settings:

  • Metric Type โ†’ Counter

  • Name โ†’ hello-app-error

Filter:

severity=ERROR
resource.labels.container_name="hello-app"
textPayload:"ERROR: 404 Error page not found"

Click Create.


โœ… Task 6: Create Metrics-Based Alert

Go to:

Logging โ†’ Log-based Metrics

Find:

hello-app-error

Click:

More Actions โ†’ Create Alert

Change:

  • Rolling window โ†’ 2 min

  • Add notification

  • Name โ†’ log based metric alert

Create Policy.


โœ… Task 7: Generate Errors (Trigger Alert)

Run:

timeout 120 bash -c -- 'while true; do curl \((kubectl get services -n gmp-test -o jsonpath='{.items[*].status.loadBalancer.ingress[0].ip}')/error; sleep \)((RANDOM % 4)) ; done'

Check:

Logs:

Logs Explorer โ†’ Severity โ†’ Error

Monitoring:

Monitoring โ†’ Alerting โ†’ Incidents

You should see 2 alerts triggered.


๐ŸŽฏ What You Achieved

โœ” Created GKE cluster

โœ” Created log-based alert

โœ” Created user-defined metric

โœ” Created metric-based alert

โœ” Generated error logs

โœ” Triggered alert

โœ” Viewed incident details


๐Ÿงพ Interview Notes (Very Important)

What is Log-based Metric?

A metric created from log filters to count occurrences of specific log entries.


Difference Between Log Alert & Metric Alert?

Log Alert Metric Alert
Direct log match Based on metric value
Instant detection Aggregated window-based

Why Use Log-based Metrics?

  • Track error frequency

  • Monitor application health

  • Trigger automated response

  • Detect security issues