Recording rules の定義
ルールの設定
Prometheus は、定期的に評価できる2種類のルールをサポートしています: recording rules と alerting rules です。ルールを Prometheus に含めるには、必要なルールステートメントを含むファイルを作成し、Prometheus の Prometheus 設定 の rule_files フィールドを介して Prometheus にそのファイルをロードさせます。ルールファイルは YAML を使用します。
ルールファイルは、Prometheus プロセスに SIGHUP を送信することで実行時にリロードできます。変更は、すべてのルールファイルが正しくフォーマットされている場合にのみ適用されます。
ルールの構文チェック
Prometheus の promtool コマンドラインユーティリティを使用して、Prometheus サーバーを起動せずにルールの構文が正しいかどうかを迅速に確認できます。
promtool check rules /path/to/example.rules.yml
promtool バイナリは、プロジェクトの ダウンロードページ で提供されている prometheus アーカイブの一部です。
ファイルが構文的に有効な場合、チェッカーは解析されたルールのテキスト表現を標準出力に出力し、0 の戻りステータスで終了します。
構文エラーまたは無効な入力引数がある場合、標準エラーにエラーメッセージを出力し、1 の戻りステータスで終了します。
Recording rules
Recording rules を使用すると、頻繁に必要な、または計算コストの高い式を事前に計算し、その結果を新しい時系列セットとして保存できます。事前に計算された結果をクエリすると、必要となるたびに元の式を実行するよりもはるかに高速になることがよくあります。これは、ダッシュボードがリフレッシュされるたびに同じ式を繰り返しクエリする必要がある場合に特に役立ちます。
Recording rules と alerting rules は、ルールグループ内に存在します。グループ内のルールは、同じ評価時間で、定期的な間隔で順次実行されます。recording rules の名前は、有効なメトリック名 でなければなりません。alerting rules の名前は、有効なラベル値 でなければなりません。
ルールファイルの構文は次のとおりです。
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>
recording rules の構文は次のとおりです。
# 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> ]
alerting rules の構文は次のとおりです。
# 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> ]
Recording rules によって作成されるメトリックの名前付けに関する ベストプラクティス も参照してください。
アラートと時系列の制限
alerting rules によって生成されるアラートと recording rules によって生成される時系列の制限は、グループごとに設定できます。制限を超えた場合、すべて のルールによって生成された時系列は破棄され、alerting rule の場合は、アクティブ、保留中、または非アクティブなすべてのアラートがクリアされます。このイベントは、評価のエラーとして記録され、そのため、古いマーカーは書き込まれません。
ルールクエリのオフセット
これは、基になるメトリックが Prometheus で受信および保存されていることを確認するのに役立ちます。分散システムの性質上、Prometheus が remote write ターゲットとして実行されている場合、メトリックの利用可能性の遅延が発生する可能性が高くなりますが、スクレイピングや/または短い評価間隔に異常がある場合にも発生する可能性があります。
評価が遅すぎるためのルール評価の失敗
(evaluation_interval で定義されている) 次の評価が開始される前にルールグループの評価が完了しなかった場合、次の評価はスキップされます。ルールグループの subsequent evaluation は、最初の評価が完了またはタイムアウトするまでスキップされ続けます。この場合、recording rule によって生成されるメトリックにギャップが発生します。rule_group_iterations_missed_total メトリックは、ルールグループの各スキップされたイテレーションでインクリメントされます。