Terraform Google 提供商,创建基于日志的警报策略
Terraform Google provider, create log-based alerting policy
我需要通过 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
我没有在该文档中找到如何创建基于日志的警报策略。
我可以创建类型为 'Metrics' 但类型为 'Logs'
的警报策略
我使用最新版本的 Terraform Google 云提供商:https://registry.terraform.io/providers/hashicorp/google/latest
请问我如何使用 Terraform Google 提供商创建基于日志的警报策略?
在此先感谢您的帮助。
格式为logging.googleapis.com/user/<user metrics name>
看这个例子(没有通知,只有警报策略)
resource "google_monitoring_alert_policy" "alert_policy" {
display_name = "My Alert Policy"
combiner = "OR"
conditions {
display_name = "test condition"
condition_threshold {
filter = "metric.type=\"logging.googleapis.com/user/test-metrics\" AND resource.type=\"cloud_run_revision\""
duration = "600s"
comparison = "COMPARISON_GT"
threshold_value = 1
}
}
user_labels = {
foo = "bar"
}
}
谢谢纪尧姆。
是的,这就是我解决问题的方法。
现在无法通过 Terraform 直接创建 'log' 类型的警报。
解决这个问题的步骤:
- 使用预期过滤器创建基于日志的指标
- 根据之前创建的基于日志的指标
,创建类型为 '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\""
...
}
}
问题已通过 google 提供程序的 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"
}
}
}
我需要通过 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
我没有在该文档中找到如何创建基于日志的警报策略。
我可以创建类型为 'Metrics' 但类型为 'Logs'
我使用最新版本的 Terraform Google 云提供商:https://registry.terraform.io/providers/hashicorp/google/latest
请问我如何使用 Terraform Google 提供商创建基于日志的警报策略?
在此先感谢您的帮助。
格式为logging.googleapis.com/user/<user metrics name>
看这个例子(没有通知,只有警报策略)
resource "google_monitoring_alert_policy" "alert_policy" {
display_name = "My Alert Policy"
combiner = "OR"
conditions {
display_name = "test condition"
condition_threshold {
filter = "metric.type=\"logging.googleapis.com/user/test-metrics\" AND resource.type=\"cloud_run_revision\""
duration = "600s"
comparison = "COMPARISON_GT"
threshold_value = 1
}
}
user_labels = {
foo = "bar"
}
}
谢谢纪尧姆。
是的,这就是我解决问题的方法。
现在无法通过 Terraform 直接创建 'log' 类型的警报。
解决这个问题的步骤:
- 使用预期过滤器创建基于日志的指标
- 根据之前创建的基于日志的指标 ,创建类型为 '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\""
...
}
}
问题已通过 google 提供程序的 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"
}
}
}