雑記帳的なやつ

勉強したことの備忘録や考えたことをここにアウトプットしていこうと思います。

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アプリのメトリクスの可視化までやろうと思います。 とりあえず今回はここまでで。