涨薪技术|Prometheus使用Recoding Rules优化性能

通过PromQL可以实时对Prometheus中采集到的样本数据进行查询,聚合以及其它各种运算操作。而在某些PromQL较为复杂且计算量较大时,直接使用PromQL可能会导致Prometheus响应超时的情况。这时需要一种能够类似于后台批处理的机制能够在后台完成这些复杂运算的计算,对于使用者而言只需要查询这些运算结果即可。Prometheus通过Recoding Rule规则支持这种后台计算的方式,可以实现对复杂查询的性能优化,提高查询效率。

定义Recoding rules

在Prometheus配置文件中,通过rule_files定义recoding rule规则文件的访问路径。

复制代码
rule_files:
[ - <filepath_glob> ... ]

每一个规则文件通过以下格式进行定义:​​​​​​​

复制代码
groups:
[ - <rule_group> ]

一个简单的规则文件:​​​​​​​

复制代码
groups:
- name: example
rules:
- record: job:http_inprogress_requests:sum
expr: sum(http_inprogress_requests) by (job)

rule_group的具体配置项如下所示:​​​​​​​

复制代码
# The name of the group. Must be unique within a file.
name: <string>
# How often rules in the group are evaluated.
[ interval: <duration> | default = global.evaluation_interval ]
rules:
[ - <rule> ... ]

与告警规则一致,一个group下可以包含多条规则rule​​​​​​​

复制代码
# The name of the time series to output to. Must be a valid metric name.
record: <string>
# The PromQL expression to evaluate. Every evaluation cycle this is
# evaluated at the current time, and the result recorded as a new set of
# time series with the metric name as given by 'record'.
expr: <string>
# Labels to add or overwrite before storing the result.
labels:
[ <labelname>: <labelvalue> ]

根据规则中的定义,Prometheus会在后台完成expr中定义的PromQL表达式计算,并且将计算结果保存到新的时间序列record中。同时还可以通过labels为这些样本添加额外的标签。

这些规则文件的计算频率与告警规则计算频率一致,

都通过global.evaluation_interval定义:​​​​​​​

复制代码
global:
[ evaluation_interval: <duration> | default = 1m ]
相关推荐
凤凰院凶涛QAQ2 小时前
《MySQL内核拆解:从SQL到InnoDB》(四) : 联合查询 & 视图
数据库·sql·mysql
张小姐的猫2 小时前
【MySQL】访问 & 数据流动原理
android·数据库·mysql·adb·1024程序员节
aZhe的全栈知识分享4 小时前
AI 还原 Figma 效果总达不到预期?先别急着让它写代码
前端·后端·面试
这个DBA有点耶6 小时前
信创场景下数据同步软件怎么选?对比开源CDC与原生闭环工具的结构性差异
数据库·mysql·架构
多客:duokelijie6 小时前
代练订单系统选型思路:跨品类游戏接入、订单分层管理、自动结算逻辑拆解!
数据库·mysql·微信小程序·前端框架·uni-app·php·持续部署
Fuyo_11197 小时前
C++ 运行时多态与虚函数机制全面剖析【面试重点】
开发语言·c++·面试
IT规划师7 小时前
一文讲透五种发布机制:滚动、蓝绿、金丝雀、红黑与 AB
后端·面试
FluxDB9 小时前
FluxDB vs DBX vs DBeaver:GPUI、Tauri、Eclipse 三种技术栈实测
数据库·mysql
数据库小学妹10 小时前
MySQL分页结果不稳定:唯一键兜底与游标分页
数据库·mysql·dba
风翼靓崽10 小时前
mysql数据备份/恢复
数据库·mysql