記録ルールの定義

ルールの設定

Prometheusは、定期的に設定され評価される2種類のルールをサポートしています。記録ルールとアラートルールです。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メトリックは、ルールグループのスキップされたイテレーションごとにインクリメントされます。

このページの内容