如何用正则简化Prometheus中Jenkins任务失败率录制规则?
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 thejobnamelabel. by(jobname)grouping: This tells Prometheus to compute the sum of failed tests and total tests separately for each uniquejobname, giving you a failure rate per Jenkins task.or absent()*0logic: This ensures that if a job has no failed tests (or no total tests) recorded, it returns0instead ofNaN, 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

