【问题标题】:Sending custom fields in prometheus alerts to pagerduty将 prometheus 警报中的自定义字段发送到 pagerduty
【发布时间】:2018-11-26 11:21:08
【问题描述】:

我有一个场景,我需要将当前时间戳从 Prometheus 发送到 Pagerduty。我需要从 Pagerduty 中的 Custom details 访问当前时间戳,以根据当前时间编写 Event Rules。

目前我可以在 Pagerduty ["firing", "num_firing", "num_resolved"] 中看到 Custom Details 内的三个字段。所有与警报相关的详细信息都在 "firing" 字段中,但作为单个字符串。

我曾想过将时间戳添加到 Prometheus 事件规则中的 Labels,但问题是我无法从 Prometheus 事件规则中检索该值strong>“触发” 字段,因为它不是结构化格式。

Pagerduty 中的事件详情来自 Prometheus:

{
 "client": "AlertManager",
 "client_url": "http://********/",
 "description": "[FIRING:1] **************",
 "event_type": "trigger",
 "incident_key": "********",
 "service_key": "********",
 "details": {
  "firing": "Labels:\n - alertname = ******\n - datacenter = *****\n - instance = ******\n - instance_id = ******\n - instance_type = ******\n - job = ******\n - metrics = ******\n - node = ******\n - pod = ******\n - private_ip = ******\n - public_ip = ******\n - service = ******\n - severity = critical\nAnnotations:\n - description = ******\n - summary = ******\nSource: ******\n",
  "num_firing": "1",
  "num_resolved": "0",
  "resolved": ""
 }
}

我需要在 “触发” 字段的同一级别添加当前时间戳。有没有办法通过 Prometheus 警报管理器 配置或通过 警报规则 做到这一点?

【问题讨论】:

    标签: prometheus prometheus-alertmanager pagerduty


    【解决方案1】:

    向警报添加额外元数据的标准 prometheus 方法是通过注释。

    - alert: Alert
      for: 5m
      expr: ...
      annotations:
        timestamp: >
          time: {{ query "time()" }}
    

    这应该将时间戳注释添加到正在触发的警报中。

    【讨论】:

    • 您好,感谢您的回复。我试过这个(时间戳:{{ time() }}),我收到一个错误,比如“无法解组 !!map 成字符串”。我用双引号括起来,例如 (timestamp: "{{ time() }}" ),但仍然收到类似 "function "time" not defined" 的错误
    • @SujaiSivasamy 更新了答案,其中缺少在注释中评估 PromQL 表达式所需的 query 函数。
    • 在运行 promtool 检查规则命令时,我仍然收到与“无法解组 !!map 到字符串”相同的错误。我们使用的版本有什么关系吗?
    • 它现在通过了promtool验证。但是我得到的查询值是“timestamp = time: [0xc46b3c7ec0]”。关于出了什么问题的任何想法?
    • 嘿,我发现它是一个十六进制值。当我通过纪元转换器将其转换为十进制并转换为人类可读的时间时,它给了我“格林威治标准时间:1996 年 9 月 15 日星期日 5:51:24.928 PM”的时间,这是错误的。我不确定我在这里错过了什么。
    【解决方案2】:
    - alert: Alert
      for: 5m
      expr: ...
      annotations:
        timestamp: >
          time: {{ with query "time()" }}{{ . | first | value | humanizeTimestamp }} {{ end }}
    

    我已经解决了同样的问题,如上所述。 干杯。!

    【讨论】:

      【解决方案3】:

      您可以直接将此作为自定义详细信息添加到 alertmanager.yaml 中。

      details:
        time: {{ query "time()" }}
      

      请注意,从 alertmanager 的角度来看,这会增加时间,因此可能包含一小段延迟。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-25
        • 2020-04-15
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 2022-07-13
        相关资源
        最近更新 更多