SpringbootのメトリクスをPrometheusにいれる その2
この記事は古いです。 Sprint Boot Micrometer 使えばもっと楽に実装できます。
前の記事から日数たってしまいましたが続きをメモです。
Dockerイメージの作成
まず、SpringBootアプリのDockerイメージを作る準備をします。 といってもDockerfileを以下のような感じで作成し、プロジェクトフォルダ直下に置けばOKです。
FROM openjdk:8-jdk-alpine VOLUME /tmp ADD target/app.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
↑ではSpringBootのアプリをapp.jar
でパッケージングしていることを前提としています。
PrometheusとGrafanaのセットアップ
docker-composeを使いPrometheusとGrafanaを起動します。 また前に作ったDockerfileを使ってSpringBootアプリも同じdokcer-composeのファイルから起動するようにします。 docker-compose.ymlを以下のように作成し、プロジェクトフォルダ直下に置きます。
version: '2' services: springboot-app: build: . ports: - 8080:8080 prometheus-server: image: prom/prometheus:latest ports: - 9090:9090 links: - springboot-app:app volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml grafana: image: grafana/grafana:latest ports: - 3000:3000 environment: - GF_SECURITY_ADMIN_PASSWORD=admin links: - prometheus-server:prometheus
Prometheusの定義部分を見るとわかりますが、ローカルの設定ファイルprometheus.yml
を指定しています。
これはSpringBootアプリからmetricsを取得するように設定する必要があるからです。
Prometheusの設定は以下のようにし、作成したファイルをプロジェクトフォルダ直下におきます。
(作成したSpringbootアプリの設定は`job_name: 'springboot-app'``配下の部分が該当します)
global: scrape_interval: 15s evaluation_interval: 15s external_labels: monitor: 'codelab-monitor' rule_files: scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'springboot-app' metrics_path: /prometheus static_configs: - targets: ['app:8080']
起動
以下のコマンドを実行しアプリのビルドと各コンポーネントの起動を行います。
mvn clean package docker-compose up -d
起動したら以下のエンドポイントにアクセスし、各コンポーネントの起動を確認します。
- springboot app : http://localhost:8080/prometheus
- Prometheus : http://localhost:9090/targets
- Grafana : http://localhost:3000
さらにPrometheusでは表示された画面にSpringBootに追加したPrometheusのエンドポイントが表示されState
がUP
になっていることを確認してください。ここがUPになっていなければ、Prometheusの設定がおかしいとかアプリが上手く起動していない等の原因が考えられます。
Grafanaの設定
最後にPrometheusに集めたメトリクスを可視化しますが、、、ここは単純にGrafanaの使い方を書くだけになるのではしょります。 設定すれば以下のような感じで見れるようになります。
ここまでさっとやってみました。簡単にSpringBootアプリのメトリクスを集めてモニタリングできるようになるのはいいですね。 会社で使えそうだったら使ってみよう。
ちなみに今回のソースはここです。
SpringbootのメトリクスをPrometheusにいれる その1
この記事は古いです。 Sprint Boot Micrometer 使えばもっと楽に実装できます。
SpringBootではSpring Actuatorを利用すればエンドポイントにアクセスし様々なメトリクスを参照することができます。
本記事ではSpringBootが提供するメトリクスをPrometheusに入れGrafanaで可視化するところまでやろうと思います。
といってもPrometheus向けにエンドポイントを公開するようにライブラリを使うようにするだけなのでやることは全然たいしたことないです。
追記:↓exporterは今はmicrometer使う方がいいですね。 とりあえずその1の今回は、Prometheus向けエンドポイントの公開まで書いています。
SpringBootのプロジェクトを用意し、Prometheus向けにエンドポイントを公開するためのライブラリをpom.xml(今回はMavenを利用)に定義します。 現時点のバージョンは0.1.0でした。
<dependency> <groupId>io.prometheus</groupId> <artifactId>simpleclient_spring_boot</artifactId> <version>0.1.0</version> </dependency>
次にPrometheus向けにエンドポイントを公開するアノテーション(@EnablePrometheusEndpoint)、またこのエンドポイント向けにメトリクスを収集するためのアノテーション(@EnableSpringBootMetricsCollector)をメインクラスに追加します。
@SpringBootApplication @EnablePrometheusEndpoint @EnableSpringBootMetricsCollector public class PrometheusIntegrationApplication { public static void main(String[] args) { SpringApplication.run(PrometheusIntegrationApplication.class, args); } }
最後にプロパティファイルにエンドポイントを公開するための設定を追加します。 以下の設定はactuatorのエンドポイントを全て公開するものなので当然プロダクション環境では普通しないです。
management.security.enabled=false
ここまで終わったらあとはアプリケーションを起動して以下のエンドポイントにアクセスしてください。
エンドポイント:http://localhost:8080/prometheus
以下のような出力が得られます。
# HELP httpsessions_max httpsessions_max # TYPE httpsessions_max gauge httpsessions_max -1.0 # HELP httpsessions_active httpsessions_active # TYPE httpsessions_active gauge httpsessions_active 0.0 # HELP mem mem # TYPE mem gauge mem 329153.0 # HELP mem_free mem_free # TYPE mem_free gauge mem_free 251064.0 # HELP processors processors # TYPE processors gauge processors 4.0 # HELP instance_uptime instance_uptime # TYPE instance_uptime gauge instance_uptime 10536.0 # HELP uptime uptime # TYPE uptime gauge uptime 27668.0 # HELP systemload_average systemload_average # TYPE systemload_average gauge systemload_average 3.68115234375 # HELP heap_committed heap_committed # TYPE heap_committed gauge heap_committed 281088.0 # HELP heap_init heap_init # TYPE heap_init gauge heap_init 131072.0 # HELP heap_used heap_used # TYPE heap_used gauge heap_used 30023.0 # HELP heap heap # TYPE heap gauge heap 1864192.0 # HELP nonheap_committed nonheap_committed # TYPE nonheap_committed gauge nonheap_committed 49240.0 # HELP nonheap_init nonheap_init # TYPE nonheap_init gauge nonheap_init 2496.0 # HELP nonheap_used nonheap_used # TYPE nonheap_used gauge nonheap_used 48066.0 # HELP nonheap nonheap # TYPE nonheap gauge nonheap 0.0 # HELP threads_peak threads_peak # TYPE threads_peak gauge threads_peak 42.0 # HELP threads_daemon threads_daemon # TYPE threads_daemon gauge threads_daemon 34.0 # HELP threads_totalStarted threads_totalStarted # TYPE threads_totalStarted gauge threads_totalStarted 84.0 # HELP threads threads # TYPE threads gauge threads 36.0 # HELP classes classes # TYPE classes gauge classes 7110.0 # HELP classes_loaded classes_loaded # TYPE classes_loaded gauge classes_loaded 7113.0 # HELP classes_unloaded classes_unloaded # TYPE classes_unloaded gauge classes_unloaded 3.0 # HELP gc_ps_scavenge_count gc_ps_scavenge_count # TYPE gc_ps_scavenge_count gauge gc_ps_scavenge_count 11.0 # HELP gc_ps_scavenge_time gc_ps_scavenge_time # TYPE gc_ps_scavenge_time gauge gc_ps_scavenge_time 99.0 # HELP gc_ps_marksweep_count gc_ps_marksweep_count # TYPE gc_ps_marksweep_count gauge gc_ps_marksweep_count 2.0 # HELP gc_ps_marksweep_time gc_ps_marksweep_time # TYPE gc_ps_marksweep_time gauge gc_ps_marksweep_time 126.0
その2では、PrometheusとGrafanaをDockerを利用して構築するのとSpringBootアプリのメトリクスの可視化までやろうと思います。 とりあえず今回はここまでで。