Terraform Google 提供商,创建基于日志的警报策略

Maz*_*sun 4 google-cloud-platform terraform google-cloud-monitoring terraform-provider-gcp

我需要通过 Terraform Google 云提供商创建基于日志的警报策略: https://cloud.google.com/logging/docs/alerting/monitoring-logs#lba

我从 Terraform 官方文档中查看,看到了“google_monitoring_alert_policy”资源:https ://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/monitoring_alert_policy

我没有在该文档中找到如何创建基于日志的警报策略。
我可以创建类型为“指标”的警报策略,但不能创建类型为“日志”的警报策略

在此输入图像描述

我使用最新版本的 Terraform Google 云提供商:https://registry.terraform.io/providers/hashicorp/google/latest

我如何使用 Terraform Google 提供商创建基于日志的警报策略?

在此先感谢您的帮助。

小智 9

谷歌提供商的 4.7.0 版本解决了问题,该版本添加了condition_matched_log。这是一个工作示例:

resource "google_monitoring_notification_channel" "email-me" {
  display_name = "Email Me"
  type = "email"
  labels = {
    email_address = "me@mycompany.com"
  }
  
}

resource "google_monitoring_alert_policy" "workflows" {
  display_name = "Workflows alert policy"
  combiner     = "OR"
  conditions {
    display_name = "Error condition"
    condition_matched_log {
      filter = "resource.type=\"workflows.googleapis.com/Workflow\" severity=ERROR"
    }
  }

  notification_channels = [ google_monitoring_notification_channel.email-me.name ]
  alert_strategy {
    notification_rate_limit {
      period = "300s"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Maz*_*sun 6

谢谢纪尧姆。

是的,这就是我解决问题的方式。

现在无法log通过 Terraform 直接创建类型警报。

解决此问题的步骤:

  • 使用预期过滤器创建基于日志的指标
  • 创建警报策略,其类型metric基于之前创建的基于日志的指标
resource "google_logging_metric" "my_log_metrics" {
  project = var.project_id
  name = "my-log-metric"
  filter = "..."
  description = "..."
  metric_descriptor {
    metric_kind = "..."
    value_type = "..."
  }
}

resource "google_monitoring_alert_policy" "my_policy" {
  project = var.project_id
  display_name = "my-policy"
  combiner = "OR"
  conditions {
    display_name = "my-policy"
    condition_threshold {
      filter = "metric.type=\"logging.googleapis.com/user/my-log-metric\" AND resource.type=\"cloud_composer_environment\""
    ...
    }
}

Run Code Online (Sandbox Code Playgroud)