Skip to content

Commit

Permalink
feat: add health check route (#87)
Browse files Browse the repository at this point in the history
* feat: add health check route

* fix: remove unnecessary code
  • Loading branch information
iRohitGaur authored Jan 29, 2024
1 parent 800237e commit c4303c3
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
10 changes: 10 additions & 0 deletions skill-tree/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
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);
}
}
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);
}
}
2 changes: 2 additions & 0 deletions skill-tree/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

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"));
}
}

0 comments on commit c4303c3

Please sign in to comment.