通過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 ]