-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add health check route * fix: remove unnecessary code
- Loading branch information
1 parent
800237e
commit c4303c3
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
skill-tree/src/main/java/com/RDS/skilltree/Health/HealthCheckController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> checkHealth() { | ||
double uptime = metricService.getUptime(); | ||
return Map.of("uptimeInSeconds", uptime); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
skill-tree/src/main/java/com/RDS/skilltree/metrics/MetricService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
skill-tree/src/test/java/com/RDS/skilltree/unit/HealthCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |