0
0
GcpHow-ToBeginner · 4 min read

How to Create Log-Based Metric in GCP: Step-by-Step Guide

To create a log-based metric in GCP, go to the Cloud Logging console, select Logs-based Metrics, then click Create Metric. Define the metric name, description, and filter to specify which logs to count or measure, then save it to start collecting data.
📐

Syntax

The main parts to create a log-based metric in GCP are:

  • Metric Name: A unique name for your metric.
  • Description: A short explanation of what the metric tracks.
  • Filter: A query that selects which log entries count towards the metric.
  • Metric Type: Choose between Counter (counts matching logs) or Distribution (measures numeric values in logs).
terraform
resource "google_logging_metric" "example_metric" {
  name        = "example_metric"
  description = "Counts error logs from my app"
  filter      = "resource.type=\"gce_instance\" AND severity>=ERROR"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}
💻

Example

This example creates a log-based metric that counts error logs from Compute Engine instances.

terraform
resource "google_logging_metric" "error_count" {
  name        = "error_count"
  description = "Counts error logs from Compute Engine instances"
  filter      = "resource.type=\"gce_instance\" AND severity>=ERROR"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
⚠️

Common Pitfalls

  • Using an incorrect or too broad filter can cause metrics to count unwanted logs or no logs at all.
  • Not setting the correct metric_kind and value_type leads to errors or wrong metric behavior.
  • For distribution metrics, forgetting to extract numeric values from logs will cause the metric to fail.
terraform
resource "google_logging_metric" "wrong_metric" {
  name        = "wrong_metric"
  description = "Incorrect filter example"
  filter      = "severity=ERROR"  # Missing resource type causes too broad matching
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}

# Corrected filter:
resource "google_logging_metric" "correct_metric" {
  name        = "correct_metric"
  description = "Counts error logs from GCE instances"
  filter      = "resource.type=\"gce_instance\" AND severity>=ERROR"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
  }
}
📊

Quick Reference

Remember these key points when creating log-based metrics:

  • Use precise filters to target relevant logs.
  • Choose DELTA for counting logs and DISTRIBUTION for measuring values.
  • Give clear names and descriptions for easy identification.
  • Test your filter in the Logs Explorer before creating the metric.

Key Takeaways

Create log-based metrics in GCP by defining a name, description, and filter in Cloud Logging.
Use precise filters to count or measure only the logs you need.
Choose the correct metric type: DELTA for counts, DISTRIBUTION for numeric values.
Test filters in Logs Explorer before creating metrics to ensure accuracy.
Avoid broad filters and incorrect metric descriptors to prevent errors.