【问题标题】:How can i extract a log that contains a variable with REGEXP_EXTRACT in google_logging_metric?如何在 google_logging_metric 中提取包含 REGEXP_EXTRACT 变量的日志?
【发布时间】:2022-01-03 10:46:16
【问题描述】:

我对 GCP 和创建指标非常陌生。我们使用 Grafana 在创建的“google_logging_metric”的帮助下显示事件日志的计数。

我的用例是假设我们有一个日志

The Number is {variable}"

变量的可能值是一个 5 位数字,每个变量都会出现多次日志。

我通过 terraform 创建 Metric 如下

resource "google_logging_metric" "" {
  name    = ""
  project = var
  filter  = "resource.type=\"k8s_container\" resource.labels.container_name=\"\" jsonPayload.message=~\"(The Number is {something should be added here?})\""
  metric_descriptor {
    metric_kind  = "DELTA"
    value_type   = "INT64"
    display_name = ""
    labels {
      key         = "event"
      value_type  = "STRING"
      description = ""
    }
  }

  label_extractors = {
    event     = "REGEXP_EXTRACT(jsonPayload.message, \"(The Number is {something should be added here?})\")"
  }
}

我喜欢在 grafana 上对日志事件进行分组,例如“编号为 XXXXX”、“编号为 YYYYY”、“编号为 ZZZZZ”。谁能建议我如何做到这一点?我是否必须在 grafana 仪表板上修改指标或其他内容?

【问题讨论】:

    标签: google-cloud-platform grafana google-logging


    【解决方案1】:

    Cloud Logging 支持正则表达式,因此如果您需要过滤的日志条目在 1 到 3 之间变化,您可以尝试以下类似的操作:

    jsonPayload.message =~ "The Number is\s*[1-3]"
    

    这里是有关 Cloud Logging 正则表达式的官方文档:https://cloud.google.com/blog/products/management-tools/cloud-logging-gets-regular-expression-support

    【讨论】:

    • 其实变量是5位数字。但是,让我尝试应用正则表达式来提取日志。
    • 在创建指标时添加此 (log + "\\s\\d{5}"),看到此错误“错误 400:无法解析提取器表达式:字符串文字中不支持的转义序列”。我还尝试了“(log + "\s\d{5}")" 和 "(log + "\\\\s\\\\d{5}")"。没有运气
    • 你能用新代码更新你原来的帖子吗?
    猜你喜欢
    • 2015-04-29
    • 2021-07-21
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多