LoginSignup
5

More than 5 years have passed since last update.

posted at

updated at

Spring Boot 2で/actuator/prometheusを有効にする

経緯

Spring Boot 2ではメトリクス機能の強化によりPrometheusにも対応をしていますが、公式リファレンス通りにやってもうまくいかなかったので、回避策をメモ。

検証環境
- Spring Boot 2.0.0

手順

依存関係の追加

POMにmicrometer-registry-prometheusの依存関係を追加します。これが公式リファレンスには記載がありませんでした。

pom.xml
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- この依存関係を追加するとうまくいった -->
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

エンドポイントの追加

application.propertiesでprometheusのエンドポイントを追加します。デフォルトではinfoとhealthのみ公開されています。

application.properties
# infoとhealthはデフォルトでexposeされているのでprometheusを追加
management.endpoints.web.exposure.include=info,health,prometheus

起動時のログ確認

起動すると以下のログが出力されます。/actuator/prometheusが出力されていればOKです。POMにmicrometer-registry-prometheusを追加しないとinfoとhealthしか出力されませんでした。

起動時ログ
Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}"
Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}"
Mapped "{[/actuator/prometheus],methods=[GET],produces=[text/plain;version=0.0.4;charset=utf-8]}"

以上です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
5