定義記錄規則

配置規則

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

另請參閱 為記錄規則建立的指標命名的最佳實踐

限制告警和時間序列

可以針對每個規則組配置告警規則產生的告警限制和記錄規則產生的時間序列限制。當超過限制時,該規則產生的所有 時間序列 都會被丟棄;如果它是告警規則,該規則的所有 告警(無論是活躍、掛起還是非活躍狀態)也都會被清除。該事件將被記錄為評估中的錯誤,因此不會寫入陳舊標記。

規則查詢偏移量

這有助於確保底層指標已被 Prometheus 接收並存儲。由於分散式系統的性質,當 Prometheus 作為遠端寫入目標執行時,更有可能出現指標可用性延遲,但也可能在抓取異常和/或評估間隔較短時發生。

因評估緩慢導致規則評估失敗

如果規則組在下一次評估應該開始之前(由 evaluation_interval 定義)尚未完成評估,則將跳過下一次評估。在初始評估完成或超時之前,將繼續跳過該規則組的後續評估。發生這種情況時,記錄規則產生的指標將出現空隙。每次錯過規則組的迭代,rule_group_iterations_missed_total 指標都會遞增。

本頁內容