在 gcp 监控过滤器中使用正则表达式

jeu*_*nii 4 terraform terraform-provider-gcp

我正在尝试监视实例组并拥有基于正则表达式的过滤器。查询看起来像这样

fetch instance_group
| metric 'compute.googleapis.com/instance_group/size'
| filter (resource.instance_group_name =~ 'nat-ecmp-[1|2]')
| group_by 1m, [value_size_mean: mean(value.size)]
| every 1m
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试在 terraform 中做同样的事情。

这就是我到目前为止所想到的

condition_threshold {
      filter   = <<-EOF
                resource.type=\"instance_group\"
                metric.type=\"compute.googleapis.com/instance_group/size\"
                resource.label."instance_group_name"=~"nat-ecmp-[1|2]"
               EOF
      duration        = "300s"
      comparison      = "COMPARISON_LT"
      threshold_value = "1"
Run Code Online (Sandbox Code Playgroud)

但这会产生错误

Error creating AlertPolicy: googleapi: Error 400: Field alert_policy.conditions[0].condition_threshold.filter had an invalid value of "resource.type="instance_group"
metric.type="compute.googleapis.com/instance_group/size"
resource.label."instance_group_name"=~"nat-ecmp-[1|2]"
": Could not parse filter "resource.type=\"instance_group\"\nmetric.type=\"compute.googleapis.com/instance_group/size\"\nresource.label.\"instance_group_name\"=~\"nat-ecmp-[1|2]\"\n"; syntax error at line 3, column 37, token '=~'
Run Code Online (Sandbox Code Playgroud)

我也尝试过这个

    condition_threshold {
      filter   = <<-EOF
                resource.type=\"instance_group\"
                metric.type=\"compute.googleapis.com/instance_group/size\"
                resource.label.\"instance_group_name\"=~\"nat-ecmp-[1|2]\"
               EOF
      duration        = "300s"
      comparison      = "COMPARISON_LT"
Run Code Online (Sandbox Code Playgroud)

但我得到了同样的结果。我在这里错过了什么?

Ioa*_*zea 5

我在多次阅读 Google 文档后找到了解决方案。

您需要使用:

metric.labels.instance_name = monitoring.regex.full_match("^gke-(hipster|nginx).*")
Run Code Online (Sandbox Code Playgroud)

我的用例的完整示例是:

resource "google_monitoring_alert_policy" "alert_policy" {
  display_name = "Dataflow fail alert policy"
  combiner     = "AND"
  conditions {
    display_name = "test condition"
    condition_threshold {
      trigger {
        percent = null
        count   = 1
      }
      aggregations {
        per_series_aligner   = "ALIGN_MAX"
        group_by_fields      = []
        alignment_period     = "3600s"
        cross_series_reducer = "REDUCE_NONE"
      }
      threshold_value      = 0
      duration             = "300s"
      comparison           = "COMPARISON_GT"
      filter               = "metric.type=\"logging.googleapis.com/user/dev_dataproc_job_error\" AND resource.type=\"cloud_dataproc_job\" AND resource.label.job_id!=monitoring.regex.full_match(\"foo_bar.*\")"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)