Prometheus使用Recoding Rules优化性能

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

定义Recoding rules

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

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

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

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

一个简单的规则文件:

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

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

csharp 复制代码
# 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

csharp 复制代码
# 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定义:

csharp 复制代码
global:
[ evaluation_interval: <duration> | default = 1m ]
相关推荐
学Linux的语莫13 小时前
prometheus、grafana的docker搭建
docker·容器·prometheus
为什么不问问神奇的海螺呢丶14 小时前
服务器巡检报告-基于categraf 采集数据-存入Prometheus-写入mysql后生成报告
服务器·mysql·prometheus
一枚正在学习的小白21 小时前
prometheus监控mysql服务
linux·运维·mysql·prometheus
chen_note2 天前
监控——普罗米修斯
grafana·prometheus
闲人编程2 天前
Prometheus监控指标集成指南
prometheus·监控·promql·仪表盘··cncf·codecapsule
木子欢儿3 天前
Prometheus Blackbox域名SSL证书监控并设置AlertManager告警
网络·网络协议·ssl·prometheus
wxjlkh4 天前
docker 搭建 grafana+prometheus 监控主机资源之node_exporter
docker·grafana·prometheus
yunson_Liu4 天前
kube-prometheus-stack基础上部署domain-exporter监控域名注册过期时间
prometheus
是垚不是土5 天前
基于Blackbox Exporter的网络服务黑盒监控体系实践
网络·数据库·安全·http·微服务·prometheus
阿拉斯攀登5 天前
SkyWalking 与 Zipkin、Prometheus 深度对比分析
prometheus·skywalking·可观测性·zipkin