プロジェクトは GitHub にホストされています:y0ngb1n/spring-boot-samples、Star や Fork を歓迎します 😘
準備作業#
- Prometheus
v2.14.0
- Grafana
v6.5.0
Docker を使用して上記の基本環境をデプロイします。設定ファイルは docker-compose.yml
で確認できます。以下のコマンドを入力してワンクリックデプロイを行います:
# 設定を確認
docker-compose config
# サービスを起動(-d はバックグラウンド起動)
docker-compose up -d
# サービスを停止してクリーンアップ
docker-compose down
依存関係の追加#
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Micrometer を使用して Prometheus 監視システムと接続 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
</dependencies>
application.yml
spring:
application:
name: monitoring-prometheus-grafana
management:
endpoints:
web:
exposure:
# Actuator の /actuator/prometheus エンドポイントを公開
include: 'prometheus'
metrics:
tags:
application: ${spring.application.name}
logging:
level:
io.github.y0ngb1n.*: debug
使用方法#
ステップ 1: サービスを起動
$ mvn spring-boot:run
...
2019-12-08 22:28:11.916 INFO 36157 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : base path '/actuator' の下に 1 エンドポイントを公開
2019-12-08 22:28:12.045 INFO 36157 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat がポート(s): 8080 (http) で起動しました。コンテキストパスは ''
2019-12-08 22:28:12.050 INFO 36157 --- [ main] i.g.y.m.p.PrometheusGrafanaApplication : 20.638 秒で PrometheusGrafanaApplication を起動しました (JVM は 27.154 秒間実行中)
ステップ 2: /actuator/prometheus
ポートにアクセス
$ curl -sS http://127.0.0.1:8080/actuator/prometheus
# HELP jvm_gc_memory_promoted_bytes_total GC 前後の古い世代メモリプールのサイズの正の増加のカウント
# TYPE jvm_gc_memory_promoted_bytes_total counter
jvm_gc_memory_promoted_bytes_total{application="monitoring-prometheus-grafana",} 9986992.0
# HELP jvm_threads_daemon_threads 現在の生存デーモンスレッドの数
# TYPE jvm_threads_daemon_threads gauge
jvm_threads_daemon_threads{application="monitoring-prometheus-grafana",} 30.0
# HELP process_uptime_seconds Java 仮想マシンの稼働時間
# TYPE process_uptime_seconds gauge
process_uptime_seconds{application="monitoring-prometheus-grafana",} 75.133
...
アプリケーションを Prometheus に接続#
Prometheus の設定ファイルを追加します:prometheus.yml
scrape_configs:
# 任意の名前を記入、英語を推奨し、特殊文字を含めない
- job_name: 'spring-boot-app'
# データをどのくらいの頻度で収集するか
scrape_interval: 15s
# 収集時のタイムアウト時間
scrape_timeout: 10s
# 収集するパス
metrics_path: '/actuator/prometheus'
# 収集するサービスのアドレス、Spring Boot アプリがあるサーバーの具体的なアドレスに設定
static_configs:
- targets: ['192.168.31.44:8080']
ifconfig
を使用してホストの IP アドレスを確認できます。Prometheus サーバーは自動的に 15 秒ごとに http://your-ip:8080/actuator/prometheus
にリクエストを送信します。詳細な設定については 👉公式ドキュメント を参照してください。
Prometheus へのアクセスをテスト#
ステップ 1:ブラウザで http://localhost:9090
にアクセス
ステップ 2:監視データを確認
Insert metric at cursor
をクリックすると、監視指標を選択できます;Graph
をクリックすると、指標をグラフ形式で表示できます;Execute
ボタンをクリックすると、下の図のような結果が表示されます。
Grafana ビジュアライゼーションの統合#
前述の通り、docker-compose
を使用して Grafana を起動しました。次に設定を行います。
ステップ 1:ブラウザで http://localhost:3000
にアクセスし、初期アカウント admin:admin
でログインします。
ステップ 2:Add Data Source
をクリックして Prometheus データソースを追加します。
ステップ 3:ダッシュボードを作成
- 監視指標を設定
- ダッシュボード情報を設定
- ダッシュボードを保存
異なる指標を追加できます。指標の値は Spring Boot アプリの /actuator/prometheus
エンドポイントで確認できます。上記の手順を繰り返すことで、ダッシュボードに新しいグラフを追加できます。
もし自分でダッシュボードをカスタマイズするのが難しいと感じた場合は、Grafana のダッシュボードマーケット(https://grafana.com/grafana/dashboards)を見て回ると、他の人が作成した美しいダッシュボードを簡単に再利用できます。
参考資料#
- https://www.callicoder.com/spring-boot-actuator-metrics-monitoring-dashboard-prometheus-grafana/
- http://www.itmuch.com/spring-boot/actuator-prometheus-grafana/,by 周立
- https://www.baeldung.com/spring-boot-self-hosted-monitoring
- https://yunlzheng.gitbook.io/prometheus-book/
- https://micrometer.io/docs/registry/prometheus
- https://prometheus.io/docs/introduction/overview/
- https://hub.docker.com/r/prom/prometheus/
- https://grafana.com/docs/grafana/latest/