OpenMetrics エクスプローラー
OpenMetrics エクスプローラー は WhaTap が提供するメトリクスデータの照会と可視化ツールです。 収集したメトリクスを PromQL で照会し、結果をチャート(Graph) とテーブル(Table) で可視化します。
主な機能
- PromQL クエリによるメトリクス照会
- Graph と Table ビューでのデータ可視化
- 時間範囲を選択すると PromQL に時間が自動入力
- リアルタイムのメトリクス監視
画面構成
- 上部: 時間範囲セレクター、クエリ入力欄
- 中央: Graph、Table、Stacked Bar のビュー切替ボタン
- 下部: 照会結果の表示(グラフまたはテーブル)
なぜ PromQL が必要か
OpenMetrics は Prometheus のメトリクス形式に基づき、さまざまなシステムやアプリケーションの時系列指標を収集します。Prometheus はオープンソースの監視ツールで、CPU 使用率、メモリ使用量、HTTP リクエスト数、エラー率などの時間ベース数値データをメトリクス指標として保存します。
これらの指標は多くが数秒間隔で継続的に収集され、時間とともに絶えず変化する時系列データの形を取ります。
単にデータを集めるだけでは、今サービスが正常か、エラー率が通常より高くなったか、どの API が遅くなったかといった意味ある分析は難しいです。
そのため OpenMetrics エクスプローラー には収集メトリクスを分析するためのクエリ言語が必要で、その役割を担うのが PromQL(Prometheus Query Language) です。
PromQL を使うと次の分析が可能です。
- リアルタイム監視: 現在のシステム状態を即時に確認(現在の CPU 使用率、アクティブユーザー数)
- トレンド分析: 時間に伴う変化パターンの把握(直近1時間の毎秒リクエスト増加率)
- 比較分析: サーバーやサービス間の指標比較(サーバー別エラー率、地域別応答時間)
- 集計と計算: 複雑な数式や統計計算(全体エラー率、平均応答時間、遅い API 上位10件)
- アラート条件設定: しきい値超過時の自動通知(エラー率5%以上、メモリ使用率90%以上)
PromQL ユーザーガイド
クエリ実行方式
| クエリ | 説明 |
|---|---|
| Instant Query | 特定時点のデータを取得。Table タブで使用 |
| Range Query | 開始時間と終了時間の間を一定間 隔で取得 |
データ型
PromQL の式は 4 種類の型に評価されます。
| 型 | 説明 |
|---|---|
| Instant Vector | 各時系列が単一サンプルを含む集合(すべて同じタイムスタンプ) |
| Range Vector | 各時系列が時間範囲のデータポイントを含む集合 |
| Scalar | 単一の数値 |
| String | 単一の文字列 |
基本的な使い方
1. メトリクス選択(Instant Vector セレクター)
基本メトリクス照会
nginx_http_response_count_total
ラベルフィルタリング
nginx_http_response_count_total{status="200"}
複数ラベル条件
nginx_http_response_count_total{method="GET", status="200"}
2. ラベルマッチ演算子
| 演算子 | 説明 |
|---|---|
| = | ラベル値が完全一致 |
| != | ラベル値が不一致 |
| =~ | ラベル値が正規表現に一致 |
| !~ | ラベル値が正規表現に不 一致 |
正規表現例
# staging, testing, development 環境のみ取得
nginx_http_response_count_total{environment=~"staging|testing|development"}
# GET メソッドを除外
nginx_http_response_count_total{method!="GET"}
# "rep" で始まる replica を取得
nginx_http_response_count_total{replica=~"rep.*"}
3. Range Vector セレクター
現在から過去の特定期間のデータを選択します。
# 直近5分のデータ
nginx_http_response_count_total[5m]
# 直近1時間のデータ
nginx_http_response_count_total{status="200"}[1h]
4. Offset モディファイア
過去の特定時点のデータを取得します。
# 5分前の値
nginx_http_response_count_total offset 5m
# 1週間前の5分レート
rate(nginx_http_response_count_total[5m] offset 1w)
# 未来比較(負の offset)
rate(nginx_http_response_count_total[5m] offset -1w)
実用例
例1: ステータスコード別リクエスト数
nginx_http_response_count_total{instance="http://192.168.100.122:4040/metrics"}
例2: 特定ステータスコードのみ取得
# 200 応答のみ
nginx_http_response_count_total{status="200"}
# 4xx エラーのみ(正規表現)
nginx_http_response_count_total{status=~"4.."}
# 5xx エラーのみ
nginx_http_response_count_total{status=~"5.."}
例3: メソッドとステータスコードの組み合わせ
# GET リクエストで 200 応答
nginx_http_response_count_total{method="GET", status="200"}
# POST, PUT, DELETE リクエストの中で 404 応答
nginx_http_response_count_total{method=~"POST|PUT|DELETE", status="404"}
例4: メトリクス名で検索
# "job:" で始まるすべてのメトリクス
{__name__=~"job:.*"}
# "nginx" で始まるすべてのメトリクス
{__name__=~"nginx.*"}
例5: 直近の時間範囲データ
# 直近5分間の nginx 応答
nginx_http_response_count_total{status="200"}[5m]
# 直近1時間の 404 エラー
nginx_http_response_count_total{status="404"}[1h]
例6: 時間帯比較
# 現在値と1時間前の値を比較
nginx_http_response_count_total{status="200"}
nginx_http_response_count_total{status="200"} offset 1h
# 昨日の同時刻のデータ
nginx_http_response_count_total offset 1d
集計関数
集計関数(Aggregation Functions)は、複数の時系列データを一つに結合して計算します。
sum()
すべての時系列 の値を合計します。
# 全インスタンスのリクエスト数の合計
sum(nginx_http_response_count_total)
# ステータスコード別にグループ化して合計
sum by (status) (nginx_http_response_count_total)
# インスタンス別にグループ化して合計
sum by (instance) (nginx_http_response_count_total)
avg()
すべての時系列の 平均 値を計算します。
# 全インスタンスの平均応答数
avg(nginx_http_response_count_total)
# ステータスコード別の平均
avg by (status) (nginx_http_response_count_total)
count()
時系列の 件数 を数えます。
# 全体の時系列件数
count(nginx_http_response_count_total)
# ステータスコード別の時系列件数
count by (status) (nginx_http_response_count_total)
max() / min()
時系列の 最大値と最小値 を計算します。
# 最大値
max(nginx_http_response_count_total)
# 最小値
min(nginx_http_response_count_total)
# ステータスコード別最大値
max by (status) (nginx_http_response_count_total)
topk() / bottomk()
指定した数 (K) の 上位 または 下位 の時系列を返します。
# 上位5件の時系列
topk(5, nginx_http_response_count_total)
# 下位3件の時系列
bottomk(3, nginx_http_response_count_total)
# ステータスコード別上位3件
topk(3, sum by (status) (nginx_http_response_count_total))
時系列関数
rate()
最もよく使用される関数 であり、Counter メトリクスの 毎秒平均増 加率 を計算します。
# 直近5分間の毎秒平均リクエスト数
rate(nginx_http_response_count_total[5m])
# ステータスコード200の毎秒平均リクエスト数
rate(nginx_http_response_count_total{status="200"}[5m])
# 全インスタンスの毎秒総リクエスト数
sum(rate(nginx_http_response_count_total[5m]))
注意事項
- Counter メトリクス専用
- 時間範囲はスクレイプ間隔の少なくとも2倍を推奨(例: スクレイプ間隔30秒 → [1m] 以上)
- カウンターリセット(サーバー再起動など)を自動的に処理
increase()
指定した時間範囲内の 総増加量 を計算します。
# 直近5分間の総リクエスト数
increase(nginx_http_response_count_total[5m])
# 直近1時間のエラー数
increase(nginx_http_response_count_total{status=~"5.."}[1h])
rate() と increase() の関係
increase(v[5m]) = rate(v[5m]) × 300秒
- rate(): 毎秒レート
- increase(): 絶対増加量
irate()
最後の2つのデータポイントに基づいて 瞬間毎秒増加率 を計算します。
# 瞬間増加率
irate(nginx_http_response_count_total[5m])
irate() vs rate()
- irate(): 変化が速いカウンターに適し、短いスパイクを検出するのに有効
- rate(): アラートや緩やかに変化するカウンターに適し、平均値を提供
時間範囲関数
特定の時間範囲内の値を集計します。
avg_over_time()
時間範囲の平均 を計算する関数です。
# 直近10分間の平均値
avg_over_time(nginx_http_response_count_total[10m])
max_over_time() / min_over_time()
指定した時間範囲内の 最大値/最小値 を計算します。
# 直近1時間の最大値
max_over_time(nginx_http_response_count_total[1h])
# 直近1時間の最小値
min_over_time(nginx_http_response_count_total[1h])
sum_over_time()
指定した期間の値を合計する 時間範囲合計 関数です。
# 直近5分間のすべての値の合計
sum_over_time(nginx_http_response_count_total[5m])
重要ルール: Rate then Sum
このルールは、Counter メトリクスで rate()、increase()、irate() などの時系列関数を使用する際に、すべての集計関数(sum、avg、max、min、count など)に適用されます。
常に rate() を先に、その後 sum()
Counter メトリクスを集計する際は必ずこの順序を守ってください。
🔵 正しい方法
# 先に rate() を適用し、その後 sum()
sum(rate(nginx_http_response_count_total[5m]))
❌ 誤った方法
# 先に sum() を適用するとカウンターリセット時に問題が発生
rate(sum(nginx_http_response_count_total[5m]))
sum() を先に適用すると、各サーバーのカウンターリセット(再起動)を検出できなくなります。サーバーが1台再起動すると合計値が急減し、rate() はそれを負の増加率や異常なスパイクとして誤って計算します。一方、rate() を先に適用すれば、サーバーごとのカウンターリセットを正確に検出し、正しく処理できます。