Google Cloud (Stackdriver) Logging Metric

This page shows how to write Terraform for Cloud (Stackdriver) Logging Metric and write them securely.

google_logging_metric (Terraform)

The Metric in Cloud (Stackdriver) Logging can be configured in Terraform with the resource name google_logging_metric. The following sections describe 5 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L1
resource "google_logging_metric" "logging_metric_httpd_200" {
  name   = "athene2-httpd-container-200/metric"
  filter = "resource.type=k8s_container AND resource.labels.container_name=athene2-httpd-container AND jsonPayload.status=200"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
monitoring-metrics.tf#L1
resource "google_logging_metric" "log_metric_route" {
  project = var.google_project_id
  name    = "route_monitoring/metric"
  filter  = "resource.type=\"gce_route\" AND jsonPayload.event_subtype=\"compute.routes.delete\" OR jsonPayload.event_subtype=\"compute.routes.insert\""
  metric_descriptor {
    metric_kind  = "DELTA"
main.tf#L1
resource "google_logging_metric" "logging_metric_httpd_200" {
  name   = "athene2-httpd-container-200/metric"
  filter = "resource.type=k8s_container AND resource.labels.container_name=athene2-httpd-container AND jsonPayload.status=200"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
metrics_from_logs.tf#L1
resource "google_logging_metric" "error_lines_metric" {
  name   = "gcf_error_lines"
  filter = "resource.type=cloud_function severity=(EMERGENCY OR ALERT OR CRITICAL OR ERROR)"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"
alerting.tf#L1
resource "google_logging_metric" "logging_metric_project_ownership" {
  name   = "project-ownership"
  filter = "protoPayload.@type=type.googleapis.com/google.cloud.audit.AuditLog AND protoPayload.methodName=SetIamPolicy AND protoPayload.serviceData.policyDelta.bindingDeltas.role=roles/owner"
  metric_descriptor {
    metric_kind = "DELTA"
    value_type  = "INT64"

Parameters

  • description optional - string
    • A description of this metric, which is used in documentation. The maximum length of the description is 8000 characters.

  • filter required - string
  • id optionalcomputed - string
  • label_extractors optional - map / string
    • A map from a label key string to an extractor expression which is used to extract data from a log entry field and assign as the label value. Each label key specified in the LabelDescriptor must have an associated extractor expression in this map. The syntax of the extractor expression is the same as for the valueExtractor field.

  • name required - string
    • The client-assigned metric identifier. Examples - "errorcount", "nginx/requests". Metric identifiers are limited to 100 characters and can include only the following characters A-Z, a-z, 0-9, and the special characters -.,+!*',()%/. The forward-slash character (/) denotes a hierarchy of name pieces, and it cannot be the first character of the name.

  • project optionalcomputed - string
  • value_extractor optional - string
    • A valueExtractor is required when using a distribution logs-based metric to extract the values to record from a log entry. Two functions are supported for value extraction - EXTRACT(field) or REGEXP_EXTRACT(field, regex). The argument are 1. field - The name of the log entry field from which the value is to be extracted. 2. regex - A regular expression using the Google RE2 syntax (https://github.com/google/re2/wiki/Syntax) with a single capture group to extract data from the specified log entry field. The value of the field is converted to a string before applying the regex. It is an error to specify a regex that does not include exactly one capture group.

Explanation in Terraform Registry

Logs-based metric can also be used to extract values from logs and create a a distribution of the values. The distribution records the statistics of the extracted values along with an optional histogram of the values as specified by the bucket options. To get more information about Metric, see:

Frequently asked questions

What is Google Cloud (Stackdriver) Logging Metric?

Google Cloud (Stackdriver) Logging Metric is a resource for Cloud (Stackdriver) Logging of Google Cloud Platform. Settings can be wrote in Terraform.

Where can I find the example code for the Google Cloud (Stackdriver) Logging Metric?

For Terraform, the mar-ma/infra_modules, doitintl/terraform-gcp-templates and serlo/infrastructure-modules-serlo.org source code examples are useful. See the Terraform Example section for further details.

security-icon

Scan your IaC problem in 3 minutes for free

You can keep your IaC security for free. No credit card required.