# **定义记录规则**
<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> ]
~~~
- 介绍(Introduction)
- 概览(Overview)
- First steps
- 方案比较
- FAQ
- Roadmap
- Media
- 术语表(Glossary)
- 概念(Concepts)
- 数据模型(Data model)
- 指标类型(Metric types)
- 作业和实例(Jobs and instances)
- Prometheus
- Getting started
- 安装
- 配置
- 配置
- 记录规则(Recording Rules)
- 报警规则(Alerting Rules)
- 模版示例
- 模版参考
- Rules的单元测试
- Querying
- Basics
- Operators
- Functions
- Examples
- HTTP API
- 存储(Storage)
- 联邦(Federation)
- 管理API(Management API)
- 迁移(Migration)
- API稳定性
- 可视化(Virsualization)
- Instrumenting
- 客户端库
- 开发客户端库
- 推送metrics
- exporters & 集成
- 开发exporters
- 格式一览(Exposition formats)
- Operating
- 安全
- 集成
- 报警(Alerting)
- 报警概览
- Alertmanager
- 配置
- 客户端
- 通知模版参考
- 通知模版样例
- 管理API
- Best Practices
- Metric & label 名称
- Instrumentation
- console & dashboard
- Histogram & summary
- 报警
- 记录规则(Recording rules)
- 何时使用Pushgateway
- Remote write tuning
- Guides
- 使用cAdvisor监控Docker容器
- 使用基于文件的服务发现来发现抓取target
- Basic auth
- 使用node exporter来监控Linux宿主机metrics
- Instrumenting一个Go应用程序
- TLS加密