涨薪技术|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 ]
相关推荐
liang_jy18 小时前
内存管理(二)—— 覆盖与交换
面试·操作系统
触底反弹19 小时前
Vibe Coding 不写 Git,等于悬崖边飙车
人工智能·git·面试
liang_jy19 小时前
内存管理(一)—— 内存管理的概念
面试·操作系统
叩码以求索21 小时前
浅谈:算法萌新如何高效刷题应对面试(一)
算法·面试·职场和发展
瞬间&永恒~1 天前
【MySQL】4-6:在同一个主机上使用 systemd 运行多个 MySQL服务器
服务器·数据库·mysql
wdfk_prog1 天前
嵌入式面试真题第 13 题:单硬件 Timer 下的高精度多路软件定时器架构设计
c语言·开发语言·面试·职场和发展·嵌入式
一嘴一个橘子1 天前
MysqL 哪些情况适合创建索引
mysql
时间的拾荒人1 天前
MySQL C语言连接 - 从入门到实战
android·c语言·mysql
苦瓜小生1 天前
AI名词大扫盲!最全的面试ai名词集合
人工智能·面试
龙虾PRO1 天前
能源与股票量化核心差异,AI全链路落地实操手册
java·面试·职场和发展