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"

Parameters

  • description required - string
    • A detailed description of the metric, which can be used in documentation.

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

  • id optionalcomputed - string
  • launch_stage optional - string
    • The launch stage of the metric definition. Possible values: ["LAUNCH_STAGE_UNSPECIFIED", "UNIMPLEMENTED", "PRELAUNCH", "EARLY_ACCESS", "ALPHA", "BETA", "GA", "DEPRECATED"]

  • metric_kind required - string
    • 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"]

  • monitored_resource_types requiredcomputed - list / string
    • 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 requiredcomputed - string
    • The resource name of the metric descriptor.

  • project optionalcomputed - string
  • type required - string
    • 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/.

  • unit optional - string
    • 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).

  • value_type required - string
    • 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"]

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

Scan your IaC problem in 3 minutes for free

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