LoginSignup
0
0

GCP PubSub Oldest Unacked Message AgeにTerraformでアラートを設定する

Posted at

背景

PubSubのSubscription側で問題が起きると subscription/oldest_unacked_message_age というメトリクスが上昇するので、これにアラートを設定する

terraform example

data "google_monitoring_notification_channel" "alert_channel" {
  display_name = "Alert Channel"
}

resource "google_monitoring_alert_policy" "oldest_unacked_message_age" {
  display_name = "Pubsub oldest unacked message age"
  combiner     = "AND"
  conditions {
    display_name = "Oldest unacked message age"
    condition_threshold {
      filter = "resource.type = \"pubsub_subscription\" AND metric.type = \"pubsub.googleapis.com/subscription/oldest_unacked_message_age\""
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_MAX"
        group_by_fields = [
          "resource.labels.subscription_id",
        ]
      }
      threshold_value = "300"
      duration        = "0s"
      comparison      = "COMPARISON_GT"
      trigger {
        count = 1
      }
    }
  }
  enabled = true
  notification_channels = [
    data.google_monitoring_notification_channel.alert_channel.name,
  ]
}

Tips

意外と条件をterraformにするのが難しいので、Cloud Monitoring - Create Alert PolicyScreenshot 2024-04-18 at 21.38.28.pngからポチポチして右上のView Codeを見てFilter条件やAggregationの中身の書き方を見る

詳細

condition_thresholdに付いて

  • filter: "resource.type = \"pubsub_subscription\" AND metric.type = \"pubsub.googleapis.com/subscription/oldest_unacked_message_age\"" などのmetricsの場合は対象となるmetricsの条件を書く
  • aggregations: 以下の例では60s内の最大値をsubscription_idごとに取得
      aggregations {
        alignment_period   = "60s"
        per_series_aligner = "ALIGN_MAX"
        group_by_fields = [
          "resource.labels.subscription_id",
        ]
      }
    
  • threshold_value: 閾値 threshold_value = "300"の場合5分
  • duration: 継続時間 (例. oldest unacked message ageが5分を超えた状態がどのくらい続いているか) 0, 60, 120, ... 60の倍数の数字を入れる費強がある
  • comparison: "COMPARISON_GT" ←より大きい場合は
  • trigger: count or ratioで選ぶことができて、今までの条件が何回、またはどのくらいの比率で起きたらalertをトリガーするか条件がかける

すべてを合計すると結構複雑になるので、一つずつブレイクダウンして考える必要がある

Ref

  1. https://cloud.google.com/monitoring/api/metrics_gcp#pubsub/subscription/oldest_unacked_message_age
  2. https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0