请参与 Prometheus 用户调研(2026 年 3 月版) ,帮助社区确定未来开发工作的优先级!

定义记录规则

配置规则

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

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

语法检查规则

要快速检查规则文件在语法上是否正确,而无需启动 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> ]

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

限制报警和序列

报警规则产生的报警和记录规则产生的序列,其限制可以按组进行配置。当超过限制时,该规则产生的所有序列都将被丢弃;如果是报警规则,该规则的所有报警(无论是活跃的、待处理的还是非活跃的)也都会被清除。该事件将被记录为评估错误,因此不会写入陈旧标记 (stale markers)。

规则查询偏移量

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

因评估缓慢导致的规则评估失败

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

本页内容