Google Cloud (Stackdriver) Monitoring Metric Descriptor

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

google_monitoring_metric_descriptor (Terraform)

The Metric Descriptor in Cloud (Stackdriver) Monitoring can be configured in Terraform with the resource name google_monitoring_metric_descriptor. The following sections describe 4 examples of how to use the resource and its parameters.

Example Usage from GitHub

main.tf#L17
resource "google_monitoring_metric_descriptor" "dentry_cache_active_objects" {
  description  = "The number of active objects in the Linux Dentry Cache"
  display_name = "Dentry Cache Active Objects"
  type         = "custom.googleapis.com/knfsd/dentry_cache_active_objects"
  metric_kind  = "GAUGE"
  value_type   = "DOUBLE"
monitoring_metric_descriptor_test.tf#L6
resource "google_monitoring_metric_descriptor" "non_usage" {
  description  = "Daily sales records from all branch stores."
  display_name = "metric-descriptor"
  type         = "custom.googleapis.com/stores/daily_sales"
  metric_kind  = "GAUGE"
  value_type   = "DOUBLE"
main-2.tf#L7
resource "google_monitoring_metric_descriptor" "session_count" {
  description = "Current Session Status per server"
  display_name = "Current Sessions"
  type = "custom.googleapis.com/sessions/session_count"
  metric_kind = "GAUGE"
  value_type = "DOUBLE"
monitoring_metric_descriptor_test.tf#L6
resource "google_monitoring_metric_descriptor" "non_usage" {
  description  = "Daily sales records from all branch stores."
  display_name = "metric-descriptor"
  type         = "custom.googleapis.com/stores/daily_sales"
  metric_kind  = "GAUGE"
  value_type   = "DOUBLE"

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 detailed description of the metric, which can be used in documentation.

A concise name for the metric, which can be displayed in user interfaces. Use sentence case without an ending period, for example "Request count".

The launch stage of the metric definition. Possible values: ["LAUNCH_STAGE_UNSPECIFIED", "UNIMPLEMENTED", "PRELAUNCH", "EARLY_ACCESS", "ALPHA", "BETA", "GA", "DEPRECATED"]

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

If present, then a time series, which is identified partially by a metric type and a MonitoredResourceDescriptor, that is associated with this metric type can only be associated with one of the monitored resource types listed here. This field allows time series to be associated with the intersection of this metric type and the monitored resource types in this list.

  • name optional computed - string

The resource name of the metric descriptor.

The metric type, including its DNS name prefix. The type is not URL-encoded. All service defined metrics must be prefixed with the service name, in the format of [service name]/[relative metric name], such as cloudsql.googleapis.com/database/cpu/utilization. The relative metric name must have only upper and lower-case letters, digits, '/' and underscores '_' are allowed. Additionally, the maximum number of characters allowed for the relative_metric_name is 100. All user-defined metric types have the DNS name custom.googleapis.com, external.googleapis.com, or logging.googleapis.com/user/.

The units in which the metric value is reported. It is only applicable if the valueType is INT64, DOUBLE, or DISTRIBUTION. The unit defines the representation of the stored metric values. Different systems may scale the values to be more easily displayed (so a value of 0.02KBy might be displayed as 20By, and a value of 3523KBy might be displayed as 3.5MBy). However, if the unit is KBy, then the value of the metric is always in thousands of bytes, no matter how it may be displayed. If you want a custom metric to record the exact number of CPU-seconds used by a job, you can create an INT64 CUMULATIVE metric whose unit is s[CPU] (or equivalently 1s[CPU] or just s). If the job uses 12,005 CPU-seconds, then the value is written as 12005. Alternatively, if you want a custom metric to record data in a more granular way, you can create a DOUBLE CUMULATIVE metric whose unit is ks[CPU], and then write the value 12.005 (which is 12005/1000), or use Kis[CPU] and write 11.723 (which is 12005/1024). The supported units are a subset of The Unified Code for Units of Measure standard. More info can be found in the API documentation (https://cloud.google.com/monitoring/api/ref_v3/rest/v3/projects.metricDescriptors).

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

  • labels set block

    A human-readable description for the label.

    • key required - string

    The key for this label. The key must not exceed 100 characters. The first character of the key must be an upper- or lower-case letter, the remaining characters must be letters, digits or underscores, and the key must match the regular expression [a-zA-Z][a-za-z0-9_]*

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

  • metadata list block

    The delay of data points caused by ingestion. Data points older than this age are guaranteed to be ingested and available to be read, excluding data loss due to errors. In 'duration format'.

    The sampling period of metric data points. For metrics which are written periodically, consecutive data points are stored at this time interval, excluding data loss due to errors. Metrics with a higher granularity have a smaller sampling period. In 'duration format'.

  • timeouts single block

Explanation in Terraform Registry

Defines a metric type and its schema. Once a metric descriptor is created, deleting or altering it stops data collection and makes the metric type's existing data unusable. To get more information about MetricDescriptor, see:

Frequently asked questions

What is Google Cloud (Stackdriver) Monitoring Metric Descriptor?

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

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

For Terraform, the GoogleCloudPlatform/knfsd-cache-utils, gilyas/infracost and greavr/gcp-custom-autoscaling 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.