概要
spring-boot-starter-actuator を使います
環境
- macOS 26.4.1
- openjdk 26.0.1
- SpringBoot 4.0.6
- jasypt-spring-boot 4.0.4
- jackson-databind 2.21.2
- jobrunr 8.6.1
- gradle 9.5.1
- VSCode 1.121.0
- MySQL 9.6.0
- Redis 8.6.3
インストール
dependencies に以下を追記しましょう
- vim build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
有効化
プロパティファイルを編集するだけです
既存の application.properties に以下を追記しましょう
- vim src/main/resources/application.properties
# Spring Boot Actuator
management.endpoints.web.exposure.include=health,info,metrics,env,beans,mappings,loggers,threaddump
management.endpoint.health.show-details=always
動作確認
-
./gradlew clean && ./gradlew bootRun --args='--jasypt.encryptor.password=xxx'
以下のエンドポイントが使えるようになります
# ヘルスチェック(DB/Redis含む接続状態)
curl http://localhost:8080/actuator/health
# メトリクス一覧
curl http://localhost:8080/actuator/metrics
# 個別メトリクス例: JVMヒープ使用量
curl http://localhost:8080/actuator/metrics/jvm.memory.used
# 個別メトリクス例: HTTPリクエスト統計
curl "http://localhost:8080/actuator/metrics/http.server.requests"
# 環境変数・設定値一覧(パスワード等はマスクされる)
curl http://localhost:8080/actuator/env
# 登録済みSpring Bean一覧
curl http://localhost:8080/actuator/beans
# 登録済みHTTPルート一覧
curl http://localhost:8080/actuator/mappings
# ロガーレベル一覧 / 動的変更
curl http://localhost:8080/actuator/loggers
curl -X POST http://localhost:8080/actuator/loggers/com.example.demo \
-H 'Content-Type: application/json' \
-d '{"configuredLevel":"DEBUG"}'
# スレッドダンプ
curl http://localhost:8080/actuator/threaddump
最後に
SpringBoot の actuator を有効にし様々なメトリイックスを取得する方法を紹介しました
Spring コンテナないの Bean 情報なども取得できます
Prometheus 専用のメトリックスはデフォルトではないので https://mvnrepository.com/artifact/io.micrometer/micrometer-registry-prometheus を追加する必要があります
0 件のコメント:
コメントを投稿