# **定義記錄規則**
<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加密