🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# **定义记录规则** <br /> ## **配置规则** Prometheus支持两种类型的规则,并可以对其进行配置,然后定期进行评估:记录规则和警报规则。 要将规则包含在Prometheus中,请创建一个包含必要规则语句的文件,并使Prometheus通过Prometheus配置中的`rule_files`字段加载该文件。 规则文件使用YAML。 通过将`SIGHUP`发送到Prometheus进程,可以在运行时重新加载规则文件。 仅当所有规则文件格式正确时,更改才会生效。 ## **语法检查规则** 在不启动Prometheus服务器的情况下快速检查规则文件在语法上是否正确,请安装并运行Prometheus的promtool命令行工具: ~~~ go get github.com/prometheus/prometheus/cmd/promtool promtool check rules /path/to/example.rules.yml ~~~ 当该文件中语法有效时,检查器将已解析规则的文本打印到标准输出,然后以返回状态为0而退出。 如果存在任何语法错误或无效的输入参数,它将打印一条错误消息到标准错误,并以返回状态为1而退出。 ## **记录规则** 记录规则(Recording rules)允许您可以预先计算经常需要的或计算量大的表达式,并将其结果保存为一组新的时间序列。 这样,查询预先计算的结果通常比每次需要原始表达式都要快得多。 这对于dashboard特别有用,dashboard每次刷新时都需要重复查询相同的表达式。 记录和警报规则存在于规则组(a rule group)中。 组中的规则以定期的时间间隔顺序运行。 规则文件的语法为: ~~~ 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> ... ] ~~~ ### **<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> ] ~~~ 报警规则的语法如下 ~~~ # The name of the alert. Must be a valid metric name. alert: <string> # The PromQL expression to evaluate. Every evaluation cycle this is # evaluated at the current time, and all resultant time series become # pending/firing alerts. expr: <string> # Alerts are considered firing once they have been returned for this long. # Alerts which have not yet fired for long enough are considered pending. [ for: <duration> | default = 0s ] # Labels to add or overwrite for each alert. labels: [ <labelname>: <tmpl_string> ] # Annotations to add to each alert. annotations: [ <labelname>: <tmpl_string> ] ~~~