Claude Code のメトリクスを Prometheus/Grafana を利用してローカル上で可視化してみた
こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。
Claude Code のドキュメントを読んでいると、なんと利用量をモニタリングできることがわかりました。
今回はこの機能を使って、ローカル上で Claude Code の監視を行ってみたいと思います。
アーキテクチャ
今回は Claude Code ← Prometheus ← Grafana と、右から左のコンポーネントへ Pull していくようなアーキテクチャにしました。
環境変数の設定
Claude Code が Prometheus から、スクレイピングされるようにするには、以下の環境変数を設定します。
# Prometheus
export CLAUDE_CODE_ENABLE_TELEMETRY=1
export OTEL_METRICS_EXPORTER=prometheus
この環境変数が設定された状態で claude
コマンドを実行すると、http://localhost:9464/metrics
からメトリクスをスクレイピングできるようになります。
Prometheus の設定
Claude Code が解放しているエンドポイントがわかったため、Prometheus 側の設定に移ります。
scrape_configs で host.docker.internal:9464
を指定し、Claude Code のエンドポイントへ到達するように設定します。
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs:
# Prometheus self-monitoring
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# Claude Code metrics
- job_name: 'claude-code'
static_configs:
- targets: ['host.docker.internal:9464']
Docker Compose の設定
docker-compose.yml
は以下を設定しました。Grafana も合わせて起動します。
services:
# Prometheus for metrics storage
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090" # Prometheus UI
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--web.enable-lifecycle'
# Grafana for metrics visualization
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000" # Grafana UI
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- grafana-storage:/var/lib/grafana
depends_on:
- prometheus
volumes:
grafana-storage:
http://localhost:9090/targets
にアクセスして、ターゲットの確認を行います。ステータスが up になっていますね。
Grafana の設定
最後に Grafana の設定を行います。 admin/admin
でログインして、Data sources
から Prometheus
を選択します。
Connections で http://prometheus:9090
を設定します。
問題なく設定できれば claude_code_
から始まるメトリクスが出力されています。
ブレイクダウンすると、さまざまな側面でメトリクスが出力されていますね。
まとめ
以上、「Claude Code のメトリクスを Prometheus/Grafana を利用してローカル上で可視化してみた」でした。
難しい設定なく、さくっと終わりました。非常に簡単でしたね。効果測定の参考になれば幸いです。
クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!