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"

Review your Terraform file for Google best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

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

An advanced logs filter (https://cloud.google.com/logging/docs/view/advanced-filters) which is used to match log entries.

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.

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.

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.

  • bucket_options list block
  • metric_descriptor list block

    A concise name for the metric, which can be displayed in user interfaces. Use sentence case without an ending period, for example "Request count". This field is optional but it is recommended to be set for any metrics associated with user-visible concepts, such as Quota.

    Whether the metric records instantaneous values, changes to a value, etc. Some combinations of metricKind and valueType might not be supported. For counter metrics, set this to DELTA. Possible values: ["DELTA", "GAUGE", "CUMULATIVE"]

    The unit in which the metric value is reported. It is only applicable if the valueType is 'INT64', 'DOUBLE', or 'DISTRIBUTION'. The supported units are a subset of The Unified Code for Units of Measure standard

    Whether the measurement is an integer, a floating-point number, etc. Some combinations of metricKind and valueType might not be supported. For counter metrics, set this to INT64. Possible values: ["BOOL", "INT64", "DOUBLE", "STRING", "DISTRIBUTION", "MONEY"]

    • labels set block

      A human-readable description for the label.

      • key required - string

      The label key.

      The type of data that can be assigned to the label. Default value: "STRING" Possible values: ["BOOL", "INT64", "STRING"]

  • timeouts single block

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

Automate config file reviews on your commits

Fix issues in your infrastructure as code with auto-generated patches.