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
メトリックが増加します。
このドキュメントはオープンソースです。問題の報告やプルリクエストの送信により、改善にご協力ください。