From c4303c361db848603ac58176611ff346c842aab5 Mon Sep 17 00:00:00 2001 From: Rohit Gaur Date: Mon, 29 Jan 2024 23:52:17 +0530 Subject: [PATCH] feat: add health check route (#87) * feat: add health check route * fix: remove unnecessary code --- skill-tree/pom.xml | 10 +++++ .../Health/HealthCheckController.java | 29 ++++++++++++++ .../RDS/skilltree/metrics/MetricService.java | 24 ++++++++++++ .../src/main/resources/application.properties | 2 + .../RDS/skilltree/unit/HealthCheckTest.java | 39 +++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java create mode 100644 skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java create mode 100644 skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java diff --git a/skill-tree/pom.xml b/skill-tree/pom.xml index 76cb6a90..b33f4826 100644 --- a/skill-tree/pom.xml +++ b/skill-tree/pom.xml @@ -85,6 +85,16 @@ junit-jupiter test + + org.springframework.boot + spring-boot-starter-actuator + + + org.mockito + mockito-junit-jupiter + 5.3.1 + test + diff --git a/skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java b/skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java new file mode 100644 index 00000000..6b91599b --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java @@ -0,0 +1,29 @@ +package com.RDS.skilltree.Health; + +import com.RDS.skilltree.metrics.MetricService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Map; + +@RestController +@RequestMapping("/v1/health") +@Slf4j +public class HealthCheckController { + + private final MetricService metricService; + + @Autowired + public HealthCheckController(MetricService metricService) { + this.metricService = metricService; + } + + @GetMapping("") + public Map checkHealth() { + double uptime = metricService.getUptime(); + return Map.of("uptimeInSeconds", uptime); + } +} diff --git a/skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java b/skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java new file mode 100644 index 00000000..e0f00e41 --- /dev/null +++ b/skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java @@ -0,0 +1,24 @@ +package com.RDS.skilltree.metrics; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.metrics.MetricsEndpoint; +import org.springframework.stereotype.Service; + +@Service +public class MetricService { + + private final MetricsEndpoint metricsEndpoint; + + @Autowired + public MetricService(MetricsEndpoint metricsEndpoint) { + this.metricsEndpoint = metricsEndpoint; + } + + public double getUptime() { + return metricsEndpoint.metric("process.uptime", null).getMeasurements() + .stream() + .findFirst() + .map(MetricsEndpoint.Sample::getValue) + .orElse(0.0); + } +} diff --git a/skill-tree/src/main/resources/application.properties b/skill-tree/src/main/resources/application.properties index 66a1f87c..693cd87d 100644 --- a/skill-tree/src/main/resources/application.properties +++ b/skill-tree/src/main/resources/application.properties @@ -7,3 +7,5 @@ spring.jpa.hibernate.ddl-auto=${DB_DDL_POLICY} jwt.rds.public.key=${RDS_PUBLIC_KEY} API_V1_PREFIX=/api/v1 spring.datasource.version=8.1.0 +management.endpoints.web.exposure.include=health,info,metrics + diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java new file mode 100644 index 00000000..d312375d --- /dev/null +++ b/skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java @@ -0,0 +1,39 @@ +package com.RDS.skilltree.unit; + +import com.RDS.skilltree.Health.HealthCheckController; +import com.RDS.skilltree.metrics.MetricService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.*; + +class HealthCheckTest { + + @Mock + private MetricService metricService; + + private HealthCheckController healthCheckController; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + healthCheckController = new HealthCheckController(metricService); + } + + @Test + void checkHealth() { + // Setup + when(metricService.getUptime()).thenReturn(123.0); + + // Execute + var result = healthCheckController.checkHealth(); + + // Assert + assertNotNull(result); + assertTrue(result.containsKey("uptimeInSeconds")); + assertEquals(123.0, result.get("uptimeInSeconds")); + } +}