【问题标题】:How to create an alert in Prometheus with time units? Like "average response time surpasses 5 seconds in the last 2 minutes"如何在 Prometheus 中使用时间单位创建警报?比如“最近 2 分钟平均响应时间超过 5 秒”
【发布时间】:2020-05-18 08:03:46
【问题描述】:
  - name: app
    rules:
      - alert: ServerHighLatency
        expr: sum by(applicationName) (rate(http_server_requests_seconds_sum{status!~"4..|5.."}[1m]))/sum by(applicationName) (rate(http_server_requests_seconds_count{status!~"4..|5.."}[1m])) >= 5s
        for: 5s
        labels:
          severity: critical
        annotations:
          summary: "{{ $labels.applicationName }} is responding with high latency(5s+)"
          description: "*Host*: {{ $labels.node }}\n*Datacenter*: {{ $labels.datacenter }}\n*Value*: {{ humanize $value }}\n"

但是我在表达式中的最后一个 s 周围遇到了解析错误,所以我想这是不允许的。当我删除 s 时,它可以工作,但永远不会被触发。我找不到任何警报示例来检查某个范围内的平均响应时间,而只能按值进行比较,例如“某个百分位数 > 0.8”,而不是时间单位。是否有意义?

旁注:我无法让{{ $labels.datacenter }}{{ $labels.node }} 工作,但{{ $labels.applicationName }} 工作,为什么?我在哪里可以找到关于 Prometheus 中警报语法的一些好的示例和文档?

【问题讨论】:

    标签: prometheus prometheus-alertmanager


    【解决方案1】:

    您不需要(实际上不能)将单位添加到表达式中,只需添加数字即可。如果您的警报从未触发,则可能是表达式有问题,或者结果总是小于 5。您是否在 Prometheus 控制台中测试过该表达式?

    【讨论】:

    • 对不起,昨天我在另一个问题上。今天我可以看看并检查控制台中的exp。谢谢
    • 你是对的;我看到时间单位通常包含在指标名称中。对我来说,它是seconds。我将阈值降低到0.001,它终于起作用了。
    • 关于标签不显示的问题,最后我尝试了 Prometheus 表达式控制台,发现只有sum by(tag1, tag2, tag3)才能看到最终结果中的标签,因此,在@987654324中使用它们@ 部分;最后选择这些值。
    【解决方案2】:

    只是关于“标签未显示”的旁注:

    如果我们使用 Prometheus 表达式控制台(在/graph 下),我们将看到结果中包含的所有标签,以便快速调试。

    如果我sum(rate(foo([1m]))/sum(rate(bar([1m])),我会看到这个:

    但是如果我使用sum by(tag1, tag2, tag3),我可以在结果中看到标签:

    然后,标签的值在最后的annotation 部分不再为空:

        rules:
          - alert: HighLatency
            expr: sum by(productName,instance,datacenter) (rate(http_server_requests_seconds_sum{status!~"4..|5.."}[1m]))/sum by(productName,instance,datacenter) (rate(http_server_requests_seconds_count{status!~"4..|5.."}[1m])) >= 5
            for: 5s
            labels:
              severity: critical
            annotations:
              summary: "{{ $labels.productName }} is responding with high latency(5s+)"
              description: "*Instance*: {{ $labels.instance }}\n*Datacenter*: {{ $labels.datacenter }}\n*Value*: {{ humanize $value }}\n"
    

    我可以在 Slack 频道中看到它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-21
      • 1970-01-01
      相关资源
      最近更新 更多