You need to enable JavaScript to run this app. 如何用PromQL统计jobevent故障累计时长及downtime百分比
优惠活动
大模型
产品
解决方案
定价
更多

如何用PromQL统计jobevent故障累计时长及downtime百分比

对应需求的PromQL实现方案

核心计算逻辑

指标取值规则为0=成功、1=故障,配合15s的固定抓取间隔,每个采样值可近似代表对应15s周期内的最终状态,单样本对应周期时长为15s。统计时直接对时间范围内的有效样本做值累加,乘以15即可得到对应状态的累计时长。

具体查询语句

1. 统计当日自然日故障累计时长

(故障累计秒数,需要Prometheus v2.24及以上版本支持@修饰符)

sum_over_time(
  test_jobevent_status{logname="123_abc",instance="job123"} @ start_of_day() [1d:15s]
) * 15

如果需要转换为故障累计分钟数,直接在上述结果基础上除以60即可:

sum_over_time(
  test_jobevent_status{logname="123_abc",instance="job123"} @ start_of_day() [1d:15s]
) * 15 / 60

2. 按给定公式计算状态占比

对应公式:(状态为成功的总秒数/当日总秒数)*100

(
  sum_over_time(
    (test_jobevent_status{logname="123_abc",instance="job123"} == 0) @ start_of_day() [1d:15s]
  ) * 15
) / (time() - start_of_day()) * 100

适配说明

  • 子查询步长固定设置为15s,和实际抓取间隔对齐,避免Prometheus默认全局步长(通常1m及以上)导致的采样重复、计算偏差问题
  • 如果Prometheus版本低于2.24,不支持@修饰符,可以替换为滚动24小时的近似版本:
    故障累计秒数近似版:
    sum_over_time(test_jobevent_status{logname="123_abc",instance="job123"}[1d:15s]) * 15
    
    占比计算近似版(分母固定为24h总秒数86400):
    (sum_over_time((test_jobevent_status{logname="123_abc",instance="job123"} == 0)[1d:15s]) * 15) / 86400 * 100
    
  • 如果需要批量统计多个实例/任务,直接删除或调整logname、instance的标签过滤规则即可
  • 和你之前在Graphite的用法对应:sum_over_time对应summarize、值过滤==0/==1对应分组规则、多序列累加对应sumSeries、标签过滤对应group函数的筛选逻辑

内容的提问来源于stack exchange,提问作者Jansirani Ganesan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.08 00:06:06