定义记录规则

配置规则

Prometheus 支持两种类型的规则,可以配置并在固定时间间隔进行评估:记录规则和告警规则。 要在 Prometheus 中包含规则,请创建一个包含必要规则语句的文件,并通过Prometheus 配置中的 rule_files 字段让 Prometheus 加载该文件。规则文件使用 YAML。

可以通过向 Prometheus 进程发送 SIGHUP 信号来在运行时重新加载规则文件。只有当所有规则文件格式正确时,才会应用更改。

关于原生直方图的说明(实验性功能):原生直方图始终记录为仪表直方图(目前)。大多数情况会自然创建仪表直方图,例如在 rate() 之后。

语法检查规则

要快速检查规则文件在语法上是否正确,而无需启动 Prometheus 服务器,可以使用 Prometheus 的 promtool 命令行实用程序工具

promtool check rules /path/to/example.rules.yml

promtool 二进制文件是项目下载页面上提供的 prometheus 存档的一部分。

当文件语法有效时,检查器会将解析后的规则的文本表示形式打印到标准输出,然后以 0 返回状态退出。

如果存在任何语法错误或无效的输入参数,它会向标准错误打印错误消息,并以 1 返回状态退出。

记录规则

记录规则允许您预先计算频繁需要或计算量大的表达式,并将结果保存为一组新的时间序列。然后,查询预先计算的结果通常会比每次需要时都执行原始表达式快得多。这对于需要每次刷新都重复查询相同表达式的仪表盘尤其有用。

记录规则和告警规则存在于一个规则组中。组内的规则以固定时间间隔按顺序运行,具有相同的评估时间。记录规则的名称必须是有效的指标名称。告警规则的名称必须是有效的标签值

规则文件的语法是

groups:
  [ - <rule_group> ]

一个简单的规则文件示例将是

groups:
  - name: example
    rules:
    - record: code:prometheus_http_requests_total:sum
      expr: sum by (code) (prometheus_http_requests_total)

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

# Limit the number of alerts an alerting rule and series a recording
# rule can produce. 0 is no limit.
[ limit: <int> | default = 0 ]

# Offset the rule evaluation timestamp of this particular group by the specified duration into the past.
[ query_offset: <duration> | default = global.rule_query_offset ]

# Labels to add or overwrite before storing the result for its rules.
# Labels defined in <rule> will override the key if it has a collision.
labels:
  [ <labelname>: <labelvalue> ]

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 label value.
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 ]

# How long an alert will continue firing after the condition that triggered it
# has cleared.
[ keep_firing_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> ]

另请参阅命名记录规则创建的指标的最佳实践

限制告警和序列

可以为每个组配置告警规则产生的告警和记录规则产生的序列的限制。当超出限制时,该规则产生的所有序列都会被丢弃,如果是告警规则,则该规则的所有告警(活动的、挂起的或不活动的)也会被清除。该事件将作为评估中的错误记录,因此不会写入过时标记。

规则查询偏移

这对于确保已在 Prometheus 中接收和存储了基础指标非常有用。由于分布式系统的性质,当 Prometheus 作为远程写入目标运行时,指标可用性延迟更有可能发生,但也可能在抓取和/或评估间隔较短时发生异常。

由于评估缓慢导致的规则评估失败

如果规则组在下一次评估应该开始之前(由 evaluation_interval 定义)未完成评估,则将跳过下一次评估。将继续跳过规则组的后续评估,直到初始评估完成或超时。发生这种情况时,记录规则产生的指标中会出现一个缺口。对于规则组的每次错过迭代,rule_group_iterations_missed_total 指标将递增。

此文档是开源的。请提交问题或拉取请求以帮助改进它。