Skip to content

Add Prometheus + Grafana monitoring stack#1437

Merged
TinyKitten merged 4 commits into
devfrom
feature/prometheus-grafana-stack
Mar 20, 2026
Merged

Add Prometheus + Grafana monitoring stack#1437
TinyKitten merged 4 commits into
devfrom
feature/prometheus-grafana-stack

Conversation

@TinyKitten

@TinyKitten TinyKitten commented Mar 20, 2026

Copy link
Copy Markdown
Member

Summary

  • Prometheus + Grafana を docker-compose に追加し、gRPC メトリクス (localhost:50052/metrics) を可視化できるようにした
  • リクエストレート、エラーレート、レイテンシ (p50/p90/p99)、ヒートマップ、エラー率ゲージ等を含むプリセットダッシュボードを同梱
  • README に Monitoring セクションを追加

詳細

  • compose.yml: prometheus, grafana サービス追加。api に METRICS_HOST と ポート 50052 を追加
  • docker/prometheus/prometheus.yml: api:50052 を 15 秒間隔でスクレイプ
  • docker/grafana/: データソース・ダッシュボードの自動プロビジョニング設定
  • Grafana は localhost:3001 (admin/admin)、Prometheus は localhost:9090

Test plan

  • docker compose up -d で全サービスが起動することを確認
  • localhost:9090/targets で Prometheus が api ターゲットを正常にスクレイプしていることを確認
  • localhost:3001 で Grafana にログインし、「StationAPI gRPC Metrics」ダッシュボードが表示されることを確認
  • gRPC リクエストを送信後、ダッシュボードにメトリクスが反映されることを確認

🤖 Generated with Claude Code

Summary by CodeRabbit

リリースノート

  • 新機能

    • ローカルで動作する監視環境(Prometheus + Grafana)を同梱し、StationAPIのgRPCメトリクスを可視化するダッシュボードを追加。
  • ドキュメント

    • READMEに起動手順(docker composeでの起動コマンド)とGrafana/Prometheusのローカルアクセス情報、及び自動プロビジョニングされる「StationAPI gRPC Metrics」ダッシュボードと表示される主な指標(リクエスト率、エラー率、レイテンシ百分位など)を追記。

TinyKitten and others added 2 commits March 20, 2026 09:40
Set up docker-compose services for Prometheus and Grafana to visualize
gRPC metrics from the /metrics endpoint. Includes a pre-provisioned
dashboard with request rate, error rate, latency percentiles, heatmap,
and error ratio panels.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions Bot added the feature 要望対応や課題解決 label Mar 20, 2026
@TinyKitten TinyKitten self-assigned this Mar 20, 2026
@coderabbitai

coderabbitai Bot commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 87867b58-5399-488d-ad45-e2bb0f281a4b

📥 Commits

Reviewing files that changed from the base of the PR and between 94ee6a0 and 16b098d.

📒 Files selected for processing (3)
  • README.md
  • compose.yml
  • docker/grafana/dashboards/stationapi-grpc.json
✅ Files skipped from review due to trivial changes (2)
  • README.md
  • docker/grafana/dashboards/stationapi-grpc.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • compose.yml

📝 Walkthrough

ウォークスルー

Prometheus と Grafana を組み込む Docker Compose 構成、Prometheus のスクレイプ設定、Grafana のプロビジョニング(データソース・ダッシュボード)および「StationAPI gRPC Metrics」ダッシュボード JSON を追加し、ローカルで gRPC メトリクスの可視化を可能にします。

変更内容

コホート / ファイル(s) 概要
ドキュメンテーション
README.md
Prometheus + Grafana の起動コマンド、アクセス URL、ダッシュボードの自動プロビジョニング等を説明する「Monitoring」セクションを追加。
Docker Compose 構成
compose.yml
apiMETRICS_HOST=0.0.0.0 環境変数と 50052:50052 ポートを追加。prometheus9090)と grafana3001)サービスを追加し、prometheus_data/grafana_data 名付きボリュームを定義。
Prometheus 設定
docker/prometheus/prometheus.yml
グローバル scrape/evaluation を 15s に設定。stationapi ジョブで api:50052/metrics をスクレイプ、ラベル service: "stationapi" を付与。
Grafana プロビジョニング
docker/grafana/provisioning/dashboards/dashboards.yml, docker/grafana/provisioning/datasources/prometheus.yml
Prometheus データソースとファイルベースのダッシュボードプロバイダをプロビジョニングする設定を追加(自動読み込み・編集可)。
Grafana ダッシュボード
docker/grafana/dashboards/stationapi-grpc.json
「StationAPI gRPC Metrics」ダッシュボードを追加:メソッド別リクエストレート、エラーレート、p50/p90/p99、ヒートマップ、エラー比率、合計/開始リクエスト等のパネルを含む。

シーケンス図

sequenceDiagram
    participant Dev as 開発者 (docker compose)
    participant Compose as Docker Compose
    participant API as StationAPI (gRPC)
    participant Prom as Prometheus
    participant Graf as Grafana

    Dev->>Compose: docker compose up -d api prometheus grafana
    Compose->>API: 起動 (METRICS_HOST=0.0.0.0)
    API-->>Prom: /metrics を 50052 で公開
    Prom->>API: 定期スクレイプ (api:50052/metrics)
    Compose->>Graf: プロビジョニング読み込み (datasource, dashboards)
    Graf->>Prom: クエリ (Prometheus HTTP API)
    Dev->>Graf: ブラウザでダッシュボード閲覧
    Graf->>Dev: 可視化 (StationAPI gRPC Metrics)
Loading

推定コードレビュー工数

🎯 3 (Moderate) | ⏱️ ~20 minutes

関連の可能性がある PR

ポエム

🐰🌿 指でトランポリン、docker を弾く、
メトリクスが跳ねて Prometheus に届く、
Grafana で踊る光の波、
小さな足で監視の道を耕す、
ぴょんと祝おう、グラフの朝!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title clearly and specifically summarizes the main change: adding a Prometheus and Grafana monitoring stack to the project.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/prometheus-grafana-stack
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can get early access to new features in CodeRabbit.

Enable the early_access setting to enable early access features such as new models, tools, and more.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
compose.yml (1)

36-46: :latest タグの使用は将来のメジャーバージョン更新で破壊的変更を招くため、具体バージョンへの固定を推奨します。

Line 36 の prom/prometheus:latest と Line 45 の grafana/grafana:latest は、現在はそれぞれ v3、v12.4 を指していますが、以下のリスクがあります:

  • Prometheus: メジャーバージョンが v4 に上がった際、破壊的変更が入り得ます。また、過去に latest が v2.53.5 を指していた時期があり、環境の再現性が損なわれる可能性があります。
  • Grafana: バージョン更新時に起動時の自動マイグレーション(DB スキーマ移行など)が実行され、予期しない挙動変更が発生する可能性があります。

${PROMETHEUS_VERSION}${GRAFANA_VERSION} などの環境変数経由でバージョン固定することで、開発環境の再現性と制御可能性が向上します。

🔧 提案差分
-    image: prom/prometheus:latest
+    image: prom/prometheus:v3.10.0

-    image: grafana/grafana:latest
+    image: grafana/grafana:12.4

あるいは環境変数で制御する場合:

-    image: prom/prometheus:latest
+    image: prom/prometheus:${PROMETHEUS_VERSION:-v3.10.0}

-    image: grafana/grafana:latest
+    image: grafana/grafana:${GRAFANA_VERSION:-12.4}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@compose.yml` around lines 36 - 46, Replace the floating :latest image tags to
pinned versions or environment variables to ensure reproducible deployments:
change prom/prometheus:latest and grafana/grafana:latest to either explicit
versions (e.g. prom/prometheus:v3.x.x, grafana/grafana:v12.4.x) or use env vars
like ${PROMETHEUS_VERSION} and ${GRAFANA_VERSION} and reference those in the
image fields so the Compose file uses fixed, controllable versions instead of
:latest.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docker/grafana/dashboards/stationapi-grpc.json`:
- Line 207: The current PromQL expression using grpc_requests_total divides
directly and produces NaN/Inf when the denominator is zero; compute the
numerator (sum of rate for status="error") and the denominator (sum of rate for
all grpc_requests_total) as separate aggregates with identical label dimensions,
then replace the direct division with a safe expression that yields the ratio
only when the denominator is non‑zero and falls back to zero otherwise (i.e.,
evaluate N and D separately, compute N/D only when D != 0, and return 0 for the
undefined intervals); update the expression that references grpc_requests_total
accordingly so labels align between the two aggregates and the fallback avoids
producing Inf/NaN.

In `@README.md`:
- Around line 57-59: The README's startup command only starts prometheus and
grafana (the line with "docker compose up -d prometheus grafana") which leaves
the api service down and causes scrape targets to be marked down; update the
instruction to either include the api service in the command (e.g., run the same
docker compose command with api added) or explicitly state the prerequisite that
the api must be running before starting prometheus/grafana so users won't
encounter down scrape targets.

---

Nitpick comments:
In `@compose.yml`:
- Around line 36-46: Replace the floating :latest image tags to pinned versions
or environment variables to ensure reproducible deployments: change
prom/prometheus:latest and grafana/grafana:latest to either explicit versions
(e.g. prom/prometheus:v3.x.x, grafana/grafana:v12.4.x) or use env vars like
${PROMETHEUS_VERSION} and ${GRAFANA_VERSION} and reference those in the image
fields so the Compose file uses fixed, controllable versions instead of :latest.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 65241a09-7414-48cf-80cf-86e6eb9285a8

📥 Commits

Reviewing files that changed from the base of the PR and between 55bdb62 and 94ee6a0.

📒 Files selected for processing (6)
  • README.md
  • compose.yml
  • docker/grafana/dashboards/stationapi-grpc.json
  • docker/grafana/provisioning/dashboards/dashboards.yml
  • docker/grafana/provisioning/datasources/prometheus.yml
  • docker/prometheus/prometheus.yml

Comment thread docker/grafana/dashboards/stationapi-grpc.json Outdated
Comment thread README.md
TinyKitten and others added 2 commits March 20, 2026 09:49
…sions

- Use safe division in Grafana error rate panel to avoid NaN/Inf
- Include api service in README startup command so scrape targets are up
- Pin Prometheus and Grafana image versions for reproducible deployments

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@TinyKitten TinyKitten merged commit 70ca307 into dev Mar 20, 2026
3 checks passed
@TinyKitten TinyKitten deleted the feature/prometheus-grafana-stack branch March 20, 2026 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature 要望対応や課題解決

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant