1
votes

We have a monitoring alert policy in GCP which we configured in Terraform. We also want to create a documentation using Terraform.

We are creating documentation using following command in GCP.

gcloud alpha monitoring policies update projects/project_name/alertPolicies/5861347861929375791 \
--documentation-format="text/markdown" \
--documentation="API has exceeded its quota limit. The systems load has increased beyond capacity, consider increasing your Global and Regional quotas. If this is unexpected behaviour, validate that this is not a bug within your platform."

Is there any way we can create the same in Terraform?

Configuration for Monitoring Policy:

# Alerts when API seeing errors
resource "google_monitoring_alert_policy" "dlp_api_see_errors" {
  project      = PROJECT_NAME
  display_name = "API is seeing errors"
  combiner     = "OR"
  
  conditions {
    display_name = "API is seeing errors"

    condition_threshold {
      filter          = "metric.type=\"serviceruntime.googleapis.com/api/request_count\" resource.type=\"consumed_api\" metric.label.\"response_code\"!=\"200\" resource.label.\"service\"=\"dlp.googleapis.com\""
      duration        = "60s"
      comparison      = "COMPARISON_GT"
      aggregations {
        alignment_period     = "60s"
        per_series_aligner   = "ALIGN_SUM"
        cross_series_reducer = "REDUCE_SUM"
      }

      trigger {
        count   = 1
      }
    }
  }

  notification_channels = "${concat(google_monitoring_notification_channel.ndw_alerting_email.*.id, google_monitoring_notification_channel.ndw_alerting_phone.*.id)}"
}
2

2 Answers

2
votes

The google_monitoring_alert_policy resource has the documentation block parameter which allows you to set Markdown documentation for the alert.

Your resource should then look like the following:

# Alerts when API seeing errors
resource "google_monitoring_alert_policy" "dlp_api_see_errors" {
  project      = PROJECT_NAME
  display_name = "API is seeing errors"
  combiner     = "OR"
  
  conditions {
    display_name = "API is seeing errors"

    condition_threshold {
      filter          = "metric.type=\"serviceruntime.googleapis.com/api/request_count\" resource.type=\"consumed_api\" metric.label.\"response_code\"!=\"200\" resource.label.\"service\"=\"dlp.googleapis.com\""
      duration        = "60s"
      comparison      = "COMPARISON_GT"
      aggregations {
        alignment_period     = "60s"
        per_series_aligner   = "ALIGN_SUM"
        cross_series_reducer = "REDUCE_SUM"
      }

      trigger {
        count   = 1
      }
    }
  }

  notification_channels = "${concat(google_monitoring_notification_channel.ndw_alerting_email.*.id, google_monitoring_notification_channel.ndw_alerting_phone.*.id)}"

  documentation {
    mime_type = "text/markdown"
    content   = "API has exceeded its quota limit. The systems load has increased beyond capacity, consider increasing your Global and Regional quotas. If this is unexpected behaviour, validate that this is not a bug within your platform."
  }
}
0
votes

You can use Terraform to create/update "documentation block" with some limitations.

The documentation block supports:

content - (Optional) The text of the documentation, interpreted according to mimeType. The content may not exceed 8,192 Unicode characters and may not exceed more than 10,240 bytes when encoded in UTF-8 format, whichever is smaller.

mime_type - (Optional) The format of the content field. Presently, only the value "text/markdown" is supported.

You can however use "mime/text" format and include all the information directly.

Also it may be usefull to have a look at GCP's official documentation regarding documentation block in this feature but loading content from external file is also not documented there.

Only mention about this feature in the official docs is in Modyfying Policy's Documentation.

But since you can do it with gcloud alpha then I'd play around with Terraform and maybe you will find a solution how to put "file" type in the documentation block...