Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add health check route #87

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("")
iRohitGaur marked this conversation as resolved.
Show resolved Hide resolved
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"));
}
}
Loading