【问题标题】:How to configure Terraform to trigger alerts based on log errors for GCP?如何配置 Terraform 以根据 GCP 的日志错误触发警报?
【发布时间】:2021-12-21 08:29:03
【问题描述】:

我是 Terraform 的新手,浏览它的文档听起来有点困惑,也就是说,我想知道是否可以创建一个 resource 来触发基于在GCP 上指定的日志文件。规则应该类似于“如果我们在 1 小时内收到超过 100 个错误,则触发它。”。

作为一个起点,我正在考虑这样的事情,但我不太确定我在消息本身和阈值(100 个错误/小时)中具体指定“错误”类型的位置。

resource "google_monitoring_alert_policy" "too_many_errors_alerts" {
display_name = "TERRAFORM -- Too many errors alerts"
  enabled      = true

  combiner = "OR"
  conditions {
    display_name = "Too many errors alerts"
    condition_threshold {
      filter          = "metric.type=\"logging.googleapis.com/location/to/logs????" resource.type=\"cloud_run_revision\""
      duration        = "1h"
      comparison      = "COMPARISON_GT"
      threshold_value = 300
      trigger {
        count = 1
      }
    }
  }
}

如果我走的是正确的道路,我想考虑一下。

【问题讨论】:

标签: google-cloud-platform terraform terraform-provider-gcp


【解决方案1】:

作为提示,请先在 GCP 控制台上尝试,因为它可以帮助您了解预期指标并指向正确的资源。 监控>警报>创建策略>添加条件

您将看到一个输入框,您可以在其中找到资源类型和指标。键入您要为其创建警报的资源名称,然后将显示可能的指标及其描述的下拉列表。

如果您单击 查询编辑器 按钮,您将看到 Terraform 预期的指标和资源名称

所以您可以在此处添加该信息

  filter                = "metric.type=\"pubsub.googleapis.com/subscription/num_undelivered_messages\" resource.type=\"pubsub_subscription\" metadata.system_labels.name=\"dead-letter-subscription\""

关于条件阈值,duration 必须是下一个格式,threshold_value 是指定要监控的错误数

  duration              = "3600s"
  threshold_value       = 100

请记住,持续时间仅支持分钟的倍数 - 例如60 秒、120 秒或 300 秒

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2021-01-29
    • 2022-08-02
    • 2021-10-30
    • 2021-10-18
    • 2020-11-20
    相关资源
    最近更新 更多