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

如何用正则简化Prometheus中Jenkins任务失败率录制规则?

Answer

Absolutely! Prometheus gives you exactly the flexibility you need here to avoid writing duplicate hardcoded rules for every Jenkins job. You can use grouping operators and label matching (including regular expressions) to calculate failure rates for all jobs in a single rule.

First, let's fix a small typo in your original expression: you had MyJenkinJobs_1 (extra 's') in the absent() clause for total tests—we'll make sure that's consistent in the generalized version.

Generalized Expression for All Jobs

Instead of filtering for a specific jobname, we'll remove the hardcoded value and use by(jobname) to group calculations per Jenkins task. Here's the updated, reusable PromQL:

(
  sum(jenkins_job_last_build_failed_tests) by (jobname) 
  or 
  absent(jenkins_job_last_build_failed_tests) by (jobname) * 0
) 
/ 
(
  sum(jenkins_job_last_build_total_tests) by (jobname) 
  or 
  absent(jenkins_job_last_build_total_tests) by (jobname) * 0
)

Breakdown of How This Works

  • No hardcoded jobname: By removing {jobname="MyJenkinsJob_1"}, Prometheus automatically processes all time series with the jobname label.
  • by(jobname) grouping: This tells Prometheus to compute the sum of failed tests and total tests separately for each unique jobname, giving you a failure rate per Jenkins task.
  • or absent()*0 logic: This ensures that if a job has no failed tests (or no total tests) recorded, it returns 0 instead of NaN, preventing invalid division results.

Using Regex to Target Specific Jobs

If you don't want to calculate rates for all Jenkins jobs—only those matching a pattern (like jobs starting with MyJenkinsJob_)—you can use a regex label matcher with =~:

(
  sum(jenkins_job_last_build_failed_tests{jobname=~"MyJenkinsJob_.*"}) by (jobname) 
  or 
  absent(jenkins_job_last_build_failed_tests{jobname=~"MyJenkinsJob_.*"}) by (jobname) * 0
) 
/ 
(
  sum(jenkins_job_last_build_total_tests{jobname=~"MyJenkinsJob_.*"}) by (jobname) 
  or 
  absent(jenkins_job_last_build_total_tests{jobname=~"MyJenkinsJob_.*"}) by (jobname) * 0
)

Recording Rule Configuration

Add this to your Prometheus recording rules file to persist the calculated failure rates:

groups:
- name: jenkins_job_metrics
  rules:
  - record: jenkins_job_test_failure_rate
    expr: |
      (
        sum(jenkins_job_last_build_failed_tests) by (jobname) 
        or 
        absent(jenkins_job_last_build_failed_tests) by (jobname) * 0
      ) 
      / 
      (
        sum(jenkins_job_last_build_total_tests) by (jobname) 
        or 
        absent(jenkins_job_last_build_total_tests) by (jobname) * 0
      )
    labels:
      unit: "ratio"

This will create a new metric jenkins_job_test_failure_rate with a jobname label for each Jenkins task, holding its test failure rate as a ratio (0 to 1).

内容的提问来源于stack exchange,提问作者Vaisakh Rajagopal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:21:33