|
| 1 | +locals { |
| 2 | + alert_policies = { |
| 3 | + for pair in setproduct(var.location, keys(var.quota_metrics)) : |
| 4 | + "${pair[0]}-${pair[1]}" => { |
| 5 | + location = pair[0] |
| 6 | + quota_metric = pair[1] |
| 7 | + limit_name = var.quota_metrics[pair[1]] |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +resource "google_monitoring_alert_policy" "cloud_kms_quota_usage" { |
| 13 | + for_each = local.alert_policies |
| 14 | + project = var.project_id |
| 15 | + display_name = "Quota usage - Cloud KMS API - Token usage per minute per region - ${each.value.quota_metric} (${each.value.location})" |
| 16 | + combiner = "OR" |
| 17 | + enabled = true |
| 18 | + |
| 19 | + conditions { |
| 20 | + display_name = "Quota usage reached defined threshold" |
| 21 | + condition_prometheus_query_language { |
| 22 | + duration = var.alert_duration |
| 23 | + evaluation_interval = var.alert_evaluation_interval |
| 24 | + query = <<-EOT |
| 25 | + max_over_time( |
| 26 | + sum( |
| 27 | + label_replace( |
| 28 | + increase( |
| 29 | + serviceruntime_googleapis_com:quota_rate_net_usage{ |
| 30 | + project_id='${var.project_id}', |
| 31 | + monitored_resource='consumer_quota', |
| 32 | + quota_metric='${each.value.quota_metric}', |
| 33 | + location='${each.value.location}' |
| 34 | + }[1m] |
| 35 | + ), |
| 36 | + 'limit_name', '${each.value.limit_name}', '', '' |
| 37 | + ) |
| 38 | + ) by (service, limit_name, project_id, quota_metric, location)[4m:1m] |
| 39 | + ) |
| 40 | + / |
| 41 | + min( |
| 42 | + ${each.value.quota_metric == "cloudkms.googleapis.com/external_kms_usage" ? 60 : 1} * last_over_time( |
| 43 | + serviceruntime_googleapis_com:quota_limit{ |
| 44 | + project_id='${var.project_id}', |
| 45 | + monitored_resource='consumer_quota', |
| 46 | + quota_metric='${each.value.quota_metric}', |
| 47 | + location='${each.value.location}', |
| 48 | + limit_name='${each.value.limit_name}' |
| 49 | + }[1d] |
| 50 | + ) |
| 51 | + ) by (service, limit_name, project_id, quota_metric, location) |
| 52 | + > ${var.alert_threshold} |
| 53 | + EOT |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + documentation { |
| 58 | + content = "To resolve this issue, click [here](https://console.cloud.google.com/iam-admin/quotas?service=$${resource.label.service}&metric=$${metric.label.quota_metric}&limit=$${metric.label.limit_name}&dimensions=%7B%22region%22%3A%22$${resource.label.location}%22%7D&project=${var.project_id}&fromNotifications=1) to review quota usage and optionally request a quota increase." |
| 59 | + mime_type = "text/markdown" |
| 60 | + } |
| 61 | + |
| 62 | + alert_strategy { |
| 63 | + auto_close = var.alert_auto_close |
| 64 | + notification_prompts = ["OPENED"] |
| 65 | + } |
| 66 | + |
| 67 | + notification_channels = var.notification_channels |
| 68 | +} |
0 commit comments