You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对Prometheus Blackbox Exporter指标按值分组并返回计数?

按值分组统计Prometheus Blackbox Exporter指标计数

核心实现方式

要实现按指标值分组计数,同时确保指定状态(如0、200、400)即使无对应时间序列也显示0,需要结合实际计数统计和默认状态补0两步操作。

具体PromQL查询

假设你的Blackbox指标为probe_http_status_code(如果是其他指标,替换为你的指标名称即可),执行以下查询:

# 统计各状态的实际出现次数
count by (status) (
  label_replace(probe_http_status_code, "status", "$1", "probe_http_status_code", "(.*)")
)
# 合并目标状态的默认0值,确保所有指定状态都能显示
or on(status) group_left() (
  vector(0) with (status="0") or 
  vector(0) with (status="200") or 
  vector(0) with (status="400")
)

关键细节说明

  • label_replace:将原指标的probe_http_status_code标签值映射为统一的status标签,方便分组统计(如果你的指标本身已有status标签,可直接省略这一步,改用count by (status) (你的指标名))。
  • count by (status):按状态值分组,统计对应时间序列的数量。
  • vector(0) with (status="X"):构造指定状态的默认0值向量,确保即使该状态无数据也能显示计数0。
  • or on(status) group_left():合并实际计数和默认0值,有真实数据的用实际计数,无数据的保留默认0。

输出示例

执行后会得到你预期的格式:

{status="400"}      1
{status="0"}        0
{status="200"}      2

内容的提问来源于stack exchange,提问作者Keet Sugathadasa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 17:57:24