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

如何用PromQL计算各API的503错误占比并拆分指标标签

Prometheus 指标计算与标签拆分问题

原始指标数据

app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myItemService.200"} -> 1
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myItemService.400"} -> 4
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myItemService.404"} -> 1
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myItemService.500"} -> 3
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myItemService.503"} -> 3
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myShopService.200"} -> 2
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myShopService.400"} -> 4
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myShopService.404"} -> 1
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myShopService.500"} -> 2
app_interface_statusCode{instance="localhost:5555", job="prometheus", metricType="Count", service="myShopService.503"} -> 6

问题描述

现有myItemService和myShopService两个API的指标数据,需实现以下需求:

  1. 按API维度计算503错误占比,占比超过30%时触发告警。当前编写的PromQL仅返回全局占比,无法按API拆分,求正确的PromQL;
  2. 能否将现有service标签(如"myItemService.503")拆分为service="myItemService"和statusCode="503"两个标签?若可以,如何实现?

解决方案

1. 按API维度计算503错误占比的PromQL

要实现按API维度拆分计算,需先提取每个API的名称,再分别统计对应API的503请求数与总请求数的比值,可使用label_replace结合sum by实现:

sum by (api) (
  label_replace(app_interface_statusCode{metricType="Count", service=~".*503"}, "api", "$1", "service", "(.*)\\.503")
)
/
sum by (api) (
  label_replace(app_interface_statusCode{metricType="Count"}, "api", "$1", "service", "(.*)\\..*")
)
* 100

说明:

  • 第一个label_replace从匹配xxx.503的service标签中提取API名称,生成api标签后按api分组求和,得到各API的503请求数;
  • 第二个label_replace从所有service标签中提取API名称,按api分组求和得到各API的总请求数;
  • 两者相除后乘以100转换为百分比,最终结果会按api维度返回每个API的503错误占比,可基于此配置> 30的告警规则。

2. 拆分service标签为service和statusCode

可以实现,推荐两种方式:

方式一:采集阶段直接修改(最优)

如果是自定义Exporter采集的指标,直接在采集代码中拆分service字段,生成带有service和statusCode两个标签的指标,从源头规范数据格式,减少后续查询复杂度。

方式二:通过Prometheus的relabel_configs处理

在Prometheus配置文件的scrape_configs中添加relabel规则,对指标进行标签拆分:

scrape_configs:
  - job_name: 'prometheus'
    # 原有采集配置...
    relabel_configs:
      - source_labels: [service]
        regex: '(.*)\.(\d+)'
        target_label: service_new
        replacement: '$1'
      - source_labels: [service]
        regex: '(.*)\.(\d+)'
        target_label: statusCode
        replacement: '$2'
      - action: labeldrop
        regex: 'service'
      - action: labelmap
        regex: 'service_new'
        replacement: 'service'

说明:

  1. 从service标签中提取API名称存入临时标签service_new;
  2. 从service标签中提取状态码存入statusCode标签;
  3. 删除原有service标签;
  4. 将临时标签service_new重命名为service,完成标签替换。

配置完成后重启Prometheus,新采集的指标就会带有拆分后的service和statusCode标签。

内容的提问来源于stack exchange,提问作者Tanmoy Roy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 21:35:36