首页 > 解决方案 > 如何从 Prometheus 查询 API 延迟错误预算

问题描述

我有一个 Prometheus 直方图,api_response_duration_seconds其中我有一个 SLO 定义为

histogram_quantile(0.95, sum(increase(api_response_duration_seconds_bucket[1m])) by (le)) <= 0.5

有没有一种简单的方法可以查询过去 28 天中有多少(百分比)此查询失败?也就是说,我希望能够回答“在过去 28 天内,此查询是否有超过 0.1% 的时间失败?”。

标签: prometheus

解决方案


所以这里的秘密是我想将范围向量转换为范围向量。这在 Prometheus 中是不可能的,但解决方法是使用录制规则

所以,需要做的是这样的:

groups:
  - name: SLOs
  - rules:
    - record: slo:api_response_duration_seconds:failing
      expr: histogram_quantile(0.95, sum(increase(api_response_duration_seconds_bucket[1m])) by (le)) > 0.5
    - record: slo:api_response_duration_seconds:all
      expr: histogram_quantile(0.95, sum(increase(api_response_duration_seconds_bucket[1m])) by (le))

然后查询错误预算为

count_over_time(slo:api_response_duration_seconds:failing[28d])
/
count_over_time(slo:api_response_duration_seconds:all[28d])

推荐阅读