From 78b904f69b9e620c2e114af95fb43e1d3150910a Mon Sep 17 00:00:00 2001 From: donghyuun Date: Sun, 27 Oct 2024 15:10:58 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=8A=A4=ED=94=84=EB=A7=81=20=EC=95=A1?= =?UTF-8?q?=EC=B6=94=EC=97=90=EC=9D=B4=ED=84=B0=EB=A5=BC=20=ED=86=B5?= =?UTF-8?q?=ED=95=9C=20DB=20=ED=85=8C=EC=9D=B4=EB=B8=94=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=83=81=ED=83=9C=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../infra/CustomDatabaseHealthIndicator.java | 27 +++++++++++++++++++ src/main/resources/application.yaml | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/helpmeCookies/global/infra/CustomDatabaseHealthIndicator.java diff --git a/src/main/java/com/helpmeCookies/global/infra/CustomDatabaseHealthIndicator.java b/src/main/java/com/helpmeCookies/global/infra/CustomDatabaseHealthIndicator.java new file mode 100644 index 0000000..fabd642 --- /dev/null +++ b/src/main/java/com/helpmeCookies/global/infra/CustomDatabaseHealthIndicator.java @@ -0,0 +1,27 @@ +package com.helpmeCookies.global.infra; + +import com.helpmeCookies.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.HealthIndicator; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class CustomDatabaseHealthIndicator implements HealthIndicator { + + private final UserRepository userRepository; + @Override + public Health health() { + try { + // 테이블이 존재하는지 확인하는 쿼리 (Users 테이블 사용) + long count = userRepository.count(); + // 테이블 존재 시 0 이상을 반환 + return Health.up().withDetail("Users table exists, count: ", count).build(); + } catch (Exception e) { + // 테이블이 없거나 데이터베이스 연결에 문제가 있는 경우 + return Health.down(e).withDetail("Users table", "Table missing or database issue") + .build(); + } + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 7922dd4..6827d3e 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -21,7 +21,7 @@ management: endpoints: web: exposure: # 외부에 노출할 엔드포인트 - include: prometheus, health, info, swagger-ui + include: prometheus, health, info metrics: tags: application: "katecam" # 메트릭 데이터에 태그를 추가 \ No newline at end of file